class Stackeye::Tools::Database

Public Class Methods

get(filepath) click to toggle source
# File lib/stackeye/tools/database.rb, line 51
def get(filepath)
  klass = new(filepath)
  klass.get
end
new(filepath) click to toggle source
# File lib/stackeye/tools/database.rb, line 12
def initialize(filepath)
  @filepath = File.expand_path(filepath)
end
set(filepath, hash) click to toggle source
# File lib/stackeye/tools/database.rb, line 56
def set(filepath, hash)
  klass = new(filepath)
  klass.set(hash)
end
truncate() click to toggle source
# File lib/stackeye/tools/database.rb, line 61
def truncate
  klass = new('')
  klass.truncate
end

Public Instance Methods

get() click to toggle source
# File lib/stackeye/tools/database.rb, line 16
def get
  json = []
  return json unless File.file?(@filepath)

  File.foreach(@filepath).with_index do |line, i|
    json << JSON.parse(line)

    break if i == MAX_DATA
  end
  json
end
set(hash) click to toggle source
# File lib/stackeye/tools/database.rb, line 28
def set(hash)
  Dir.mkdir(DATA_PATH) unless File.directory?(DATA_PATH)

  File.open(@filepath, 'a+') do |outfile|
    outfile.puts JSON.generate(hash)
  end
end
truncate() click to toggle source
# File lib/stackeye/tools/database.rb, line 36
def truncate
  Dir.foreach(DATA_PATH) do |filename|
    next if filename.start_with?('.')

    file = "#{DATA_PATH}/#{filename}"
    temp = IO.readlines(file)[-MAX_DATA..-1]
    next if temp.nil? || (temp.length < MAX_DATA)

    File.open(file, 'w') do |outfile|
      temp.each { |line| outfile.puts line }
    end
  end
end