Sha256: fd68997377afc7b7f38445f4245bbb1810c150caa812827647c209f8e555abcc

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module WeatherLink
  class Client
    attr_reader :api

    def initialize(api_key:, api_secret:, station_units: IMPERIAL_WEATHER_UNITS, desired_units: METRIC_WEATHER_UNITS)
      @api = APIv2.new(api_key: api_key, api_secret: api_secret, units: station_units)
      @desired_units = desired_units
    end

    def attach_units(data)
      data.map do |field, value|
        unit = api.unit_for(field)
        [field, unit ? Unit.new("#{value} #{unit}") : value]
      end.to_h
    end

    def desired_unit_for(field)
      desired_units.fetch(api.type_for(field))
    end

    def convert(field, value)
      desired_unit = desired_unit_for(field)
      return value unless desired_unit

      value.convert_to(desired_unit)
    end

    def stations
      @stations ||= api.station['stations'].map do |data|
        Station.new(self, data) if data
      end
    end

    def station
      stations.first
    end

    def stations_by_device_id_hex(device_id_hex)
      stations.select { |s| s.gateway_id_hex == device_id_hex }.first
    end

    def nodes
      @nodes ||= api.nodes['nodes'].map do |data|
        Node.new(self, data)
      end
    end

    def node_by_device_id_hex(device_id_hex)
      nodes.select { |n| n.device_id_hex == device_id_hex }.first
    end

    def sensors
      @sensors ||= api.sensors['sensors'].map do |data|
        Sensor.new(self, data)
      end
    end

    def sensor_by_lsid(lsid)
      sensors.select { |s| s.lsid == lsid }.first
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
weatherlink-0.1.3 lib/weatherlink/client.rb
weatherlink-0.1.2 lib/weatherlink/client.rb
weatherlink-0.1.1 lib/weatherlink/client.rb
weatherlink-0.1.0 lib/weatherlink/client.rb