Sha256: 73398e8c09ea4adf97cf22883f6176ff2affa203b76dda8b105a5ab38bdaeef4

Contents?: true

Size: 1.17 KB

Versions: 7

Compression:

Stored size: 1.17 KB

Contents

module NestConnect
  class ChunkParser
    EVENT = -'event: '
    DATA = -'data: '

    def self.write(chunk)
      unless chunk.empty?
        new(chunk)
      end
    end

    def initialize(chunk)
      @raw_event_line, @raw_data_line = chunk.split("\n")
    end

    def event
      event_line
    end

    def data
      JSON.parse(data_line, symbolize_names: true) || {}
    end

    def thermostats
      Device::Thermostat.from_hash_collection(thermostats_hash)
    end

    def protects
      Device::Protect.from_hash_collection(smoke_co_alarms_hash)
    end

    def cameras
      Device::Camera.from_hash_collection(cameras_hash)
    end

    private

      def data_hash
        data.fetch(:data, {})
      end

      def devices_hash
        data_hash.fetch(:devices, {})
      end

      def thermostats_hash
        devices_hash.fetch(:thermostats, {})
      end

      def smoke_co_alarms_hash
        devices_hash.fetch(:smoke_co_alarms, {})
      end

      def cameras_hash
        devices_hash.fetch(:cameras, {})
      end

      def data_line
        @raw_data_line.gsub(DATA, "")
      end

      def event_line
        @raw_event_line.gsub(EVENT, "")
      end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
nest_connect-0.1.7 lib/nest_connect/chunk_parser.rb
nest_connect-0.1.6 lib/nest_connect/chunk_parser.rb
nest_connect-0.1.5 lib/nest_connect/chunk_parser.rb
nest_connect-0.1.4 lib/nest_connect/chunk_parser.rb
nest_connect-0.1.3 lib/nest_connect/chunk_parser.rb
nest_connect-0.1.2 lib/nest_connect/chunk_parser.rb
nest_connect-0.1.1 lib/nest_connect/chunk_parser.rb