Class: MilkMaid::FirebaseNotifier::BatchRecord
- Inherits:
-
Object
- Object
- MilkMaid::FirebaseNotifier::BatchRecord
- Defined in:
- lib/milk_maid/firebase_notifier/batch_record.rb
Constant Summary collapse
- CACHE_THRESHOLD =
20
Instance Attribute Summary collapse
-
#base_temperature ⇒ Object
Returns the value of attribute base_temperature.
-
#batch_size ⇒ Object
Returns the value of attribute batch_size.
-
#duration ⇒ Object
Returns the value of attribute duration.
-
#guid ⇒ Object
Returns the value of attribute guid.
-
#id ⇒ Object
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#record ⇒ Object
Returns the value of attribute record.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
- #add_event(event_type, data = 0) ⇒ Object
- #cache_event(event_type) ⇒ Object
- #close_batch ⇒ Object
- #complete! ⇒ Object
- #create_remote_record ⇒ Object
- #event_name_from_type(event) ⇒ Object
- #events ⇒ Object
-
#initialize ⇒ BatchRecord
constructor
A new instance of BatchRecord.
- #send_events! ⇒ Object
- #start(batch_data = {}) ⇒ Object
- #timestamp ⇒ Object
Constructor Details
#initialize ⇒ BatchRecord
Returns a new instance of BatchRecord.
11 12 13 |
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 11 def initialize @firebase = ::Firebase::Client.new(::YAML.load_file(File.join(CONFIG_DIR, 'milk_maid.yml'))['firebase_url']) end |
Instance Attribute Details
#base_temperature ⇒ Object
Returns the value of attribute base_temperature.
7 8 9 |
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 7 def base_temperature @base_temperature end |
#batch_size ⇒ Object
Returns the value of attribute batch_size.
7 8 9 |
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 7 def batch_size @batch_size end |
#duration ⇒ Object
Returns the value of attribute duration.
7 8 9 |
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 7 def duration @duration end |
#guid ⇒ Object
Returns the value of attribute guid.
7 8 9 |
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 7 def guid @guid end |
#id ⇒ Object
Returns the value of attribute id.
7 8 9 |
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 7 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 7 def name @name end |
#record ⇒ Object
Returns the value of attribute record.
7 8 9 |
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 7 def record @record end |
#status ⇒ Object
Returns the value of attribute status.
7 8 9 |
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 7 def status @status end |
Instance Method Details
#add_event(event_type, data = 0) ⇒ Object
35 36 37 38 39 |
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 35 def add_event(event_type, data = 0) events << ::MilkMaid::FirebaseNotifier::Event.new(event_name_from_type(event_type), data, ) return if cache_event(event_type) send_events! end |
#cache_event(event_type) ⇒ Object
41 42 43 44 45 |
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 41 def cache_event(event_type) return false unless event_type == :temperature return false if @events.length < CACHE_THRESHOLD true end |
#close_batch ⇒ Object
47 48 49 |
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 47 def close_batch @firebase.update("/batches/#{id}", :status => 'Completed') end |
#complete! ⇒ Object
51 52 53 54 |
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 51 def complete! add_event(:batch_completed) close_batch end |
#create_remote_record ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 22 def create_remote_record @response = @firebase.push("batches", { :name => name, :guid => guid, :duration => duration, :base_temperature => base_temperature, :batch_size => batch_size, :status => status }) @response.body['name'] end |
#event_name_from_type(event) ⇒ Object
56 57 58 |
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 56 def event_name_from_type(event) event.to_s.split(/_/).map(&:capitalize).join(' ') end |
#events ⇒ Object
60 61 62 |
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 60 def events @events ||= [] end |
#send_events! ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 64 def send_events! events.each do |event| @firebase.push("/batches/#{id}/events", event.to_h) end @events = [] end |
#start(batch_data = {}) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 15 def start(batch_data = {}) batch_data.each { |key, value| send("#{key}=", value) } @status = 'Started' @id = create_remote_record end |
#timestamp ⇒ Object
72 73 74 |
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 72 def Time.now end |