class Ruboty::Brains::Json

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/ruboty/brains/json.rb, line 11
def initialize
  super
  @thread = Thread.new { sync }
  @thread.abort_on_exception = true
end

Public Instance Methods

data() click to toggle source
# File lib/ruboty/brains/json.rb, line 17
def data
  @data ||= fetch
end

Private Instance Methods

fetch() click to toggle source
# File lib/ruboty/brains/json.rb, line 23
def fetch
  if File.exist?(path)
    JSON.parse(File.read(path))
  else
    {}
  end
end
interval() click to toggle source
# File lib/ruboty/brains/json.rb, line 31
def interval
  ENV["JSON_SAVE_INTERVAL"]&.to_i || 5
end
path() click to toggle source
# File lib/ruboty/brains/json.rb, line 35
def path
  ENV["JSON_PATH"] || "ruboty.json"
end
save() click to toggle source
# File lib/ruboty/brains/json.rb, line 39
def save
  File.write(path, data.to_json)
end
sync() click to toggle source
# File lib/ruboty/brains/json.rb, line 43
def sync
  loop do
    wait
    save
  end
end
wait() click to toggle source
# File lib/ruboty/brains/json.rb, line 50
def wait
  sleep(interval)
end