Sha256: 42b2eab2b598c969b5c06bbd6dc8d48a439c51e9af09c68615fc7e9477407c45

Contents?: true

Size: 1.86 KB

Versions: 4

Compression:

Stored size: 1.86 KB

Contents

require 'firebase'
require 'yaml'

module MilkMaid
  module FirebaseNotifier
    class BatchRecord
      attr_accessor :name, :guid, :duration, :base_temperature, :batch_size, :status, :record, :id

      CACHE_THRESHOLD = 20

      def initialize
        @firebase = ::Firebase::Client.new(::YAML.load_file(File.join(CONFIG_DIR, 'milk_maid.yml'))['firebase_url'])
      end

      def start(batch_data = {})
        batch_data.each { |key, value| send("#{key}=", value) }
        @status = 'Started'

        @id = create_remote_record
      end

      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

      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/#{id}", :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/#{id}/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

4 entries across 4 versions & 1 rubygems

Version Path
milk_maid-0.6.1 lib/milk_maid/firebase_notifier/batch_record.rb
milk_maid-0.5.1 lib/milk_maid/firebase_notifier/batch_record.rb
milk_maid-0.5.0 lib/milk_maid/firebase_notifier/batch_record.rb
milk_maid-0.4.0 lib/milk_maid/firebase_notifier/batch_record.rb