Sha256: 56e31d8aef34baadf1dff8598af7296d8ab1f9cc8c1464cd2b2fd54c5036a09c
Contents?: true
Size: 1.83 KB
Versions: 1
Compression:
Stored size: 1.83 KB
Contents
require 'firebase' require 'yaml' module Milkmaid module FirebaseNotifier class BatchRecord attr_accessor :name, :guid, :duration, :base_temperature, :batch_size, :status, :record CACHE_THRESHOLD = 20 def initialize @firebase = ::Firebase::Client.new(::YAML.load_file(File.join(CONFIG_DIR, 'firebase.yml'))['firebase_url']) end def start(batch_data = {}) batch_data.each { |key, value| send("#{key}=", value) } @status = 'Started' self.create_remote_record end def create_remote_record @response = @firebase.push("batches", { :guid => guid, :name => name, :duration => duration, :base_temperature => base_temperature, :batch_size => batch_size, :status => status }) end def add_event(event_type, data = 0) events << ::Milkmaid::FirebaseNotifier::Event.new(event_name_from_type(event_type), data, timestamp) return if cache_event(event_type) send_events! end def cache_event(event_type) return false unless event_type == :temperature return false if @events.length < CACHE_THRESHOLD true end def close_batch @firebase.update("/batches/#{guid}", :status => 'Completed') end def complete! add_event(:batch_completed) close_batch end def event_name_from_type(event) event.to_s.split(/_/).map(&:capitalize).join(' ') end def events @events ||= [] end def send_events! events.each do |event| @firebase.push("/batches/#{guid}/events", event.to_h) end @events = [] end def timestamp Time.now end end class Event < Struct.new(:name, :data, :timestamp) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
milk_maid-0.1.0 | lib/milkmaid/firebase_notifier/batch_record.rb |