Class: WeatherLink::LocalClient

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

Constant Summary collapse

META_KEYS =
%w[lsid data_structure_type txid].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host:, station_units: IMPERIAL_WEATHER_UNITS, desired_units: METRIC_WEATHER_UNITS) ⇒ LocalClient

Returns a new instance of LocalClient.



9
10
11
12
# File 'lib/weatherlink/local_client.rb', line 9

def initialize(host:, station_units: IMPERIAL_WEATHER_UNITS, desired_units: METRIC_WEATHER_UNITS)
  @api = LocalAPIv1.new(host: host, units: station_units)
  @desired_units = desired_units
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



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

def api
  @api
end

#desired_unitsObject (readonly)

Returns the value of attribute desired_units.



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

def desired_units
  @desired_units
end

Instance Method Details

#attach_units(data) ⇒ Object



22
23
24
25
26
27
# File 'lib/weatherlink/local_client.rb', line 22

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



33
34
35
36
37
38
# File 'lib/weatherlink/local_client.rb', line 33

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

  value.convert_to(desired_unit)
end

#current_conditionsObject



40
41
42
43
44
45
46
# File 'lib/weatherlink/local_client.rb', line 40

def current_conditions
  sensors = api.current_conditions['conditions'].map do |conditions|
    SensorData.new(self, transform_like_api_v2(conditions))
  end

  SensorDataCollection.new(self, sensors)
end

#desired_unit_for(field) ⇒ Object



29
30
31
# File 'lib/weatherlink/local_client.rb', line 29

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

#sensorsObject



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

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

#stationsObject



48
49
50
51
52
# File 'lib/weatherlink/local_client.rb', line 48

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

#transform_like_api_v2(hash) ⇒ Object



16
17
18
19
20
# File 'lib/weatherlink/local_client.rb', line 16

def transform_like_api_v2(hash)
  hash.select { |k, _| META_KEYS.include?(k) }.merge('data' => [{
    'ts' => Time.now.to_i,
  }.merge(hash.reject { |k, _| META_KEYS.include?(k) })])
end