class RorVsWild::Local::Queue

def directoy

def directoy
  dir = File.directory?("tmp") ? "tmp" : Dir.tmpdir
  FileUtils.mkpath(File.join(dir, "rorvswild"))[0]
end

def errors

def errors
  load_data("errors")
end

def jobs

def jobs
  load_data("jobs")
end

def load_data(name)

def load_data(name)
  JSON.load_file(File.join(directoy, "#{name}.json")) rescue []
end

def push_error(data)

def push_error(data)
  push_to(data, "errors")
end

def push_job(data)

def push_job(data)
  push_to(data, "jobs")
  push_error(data[:error]) if data[:error]
end

def push_request(data)

def push_request(data)
  push_to(data, "requests")
  push_error(data[:error]) if data[:error]
end

def push_to(data, name)

def push_to(data, name)
  data[:queued_at] = Time.now
  data[:uuid] = SecureRandom.uuid
  array = load_data(name)
  array.unshift(data)
  array.pop if array.size > 100
  save_data(array, name)
end

def requests

def requests
  load_data("requests")
end

def save_data(data, name)

def save_data(data, name)
  File.open(File.join(directoy, "#{name}.json"), "w") { |file| JSON.dump(data, file) }
end