Class: MilkMaid::FirebaseNotifier::BatchRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/milk_maid/firebase_notifier/batch_record.rb

Constant Summary collapse

CACHE_THRESHOLD =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBatchRecord

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_temperatureObject

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_sizeObject

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

#durationObject

Returns the value of attribute duration.



7
8
9
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 7

def duration
  @duration
end

#guidObject

Returns the value of attribute guid.



7
8
9
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 7

def guid
  @guid
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 7

def id
  @id
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 7

def name
  @name
end

#recordObject

Returns the value of attribute record.



7
8
9
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 7

def record
  @record
end

#statusObject

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, timestamp)
  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_batchObject



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_recordObject



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

#eventsObject



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

#timestampObject



72
73
74
# File 'lib/milk_maid/firebase_notifier/batch_record.rb', line 72

def timestamp
  Time.now
end