Class: WeatherLink::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/weatherlink/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, api_secret:, station_units: IMPERIAL_WEATHER_UNITS, desired_units: METRIC_WEATHER_UNITS) ⇒ Client

Returns a new instance of Client.



7
8
9
10
# File 'lib/weatherlink/client.rb', line 7

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

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



5
6
7
# File 'lib/weatherlink/client.rb', line 5

def api
  @api
end

Instance Method Details

#attach_units(data) ⇒ Object



12
13
14
15
16
17
# File 'lib/weatherlink/client.rb', line 12

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

#convert(field, value) ⇒ Object



23
24
25
26
27
28
# File 'lib/weatherlink/client.rb', line 23

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

  value.convert_to(desired_unit)
end

#desired_unit_for(field) ⇒ Object



19
20
21
# File 'lib/weatherlink/client.rb', line 19

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

#node_by_device_id_hex(device_id_hex) ⇒ Object



50
51
52
# File 'lib/weatherlink/client.rb', line 50

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

#nodesObject



44
45
46
47
48
# File 'lib/weatherlink/client.rb', line 44

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

#sensor_by_lsid(lsid) ⇒ Object



60
61
62
# File 'lib/weatherlink/client.rb', line 60

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

#sensorsObject



54
55
56
57
58
# File 'lib/weatherlink/client.rb', line 54

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

#stationObject



36
37
38
# File 'lib/weatherlink/client.rb', line 36

def station
  stations.first
end

#stationsObject



30
31
32
33
34
# File 'lib/weatherlink/client.rb', line 30

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

#stations_by_device_id_hex(device_id_hex) ⇒ Object



40
41
42
# File 'lib/weatherlink/client.rb', line 40

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