Class: WeatherLink::LocalClient
- Inherits:
-
Object
- Object
- WeatherLink::LocalClient
- Defined in:
- lib/weatherlink/local_client.rb
Constant Summary collapse
- META_KEYS =
%w[lsid data_structure_type txid].freeze
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Returns the value of attribute api.
-
#desired_units ⇒ Object
readonly
Returns the value of attribute desired_units.
Instance Method Summary collapse
- #attach_units(data) ⇒ Object
- #convert(field, value) ⇒ Object
- #current_conditions ⇒ Object
- #desired_unit_for(field) ⇒ Object
-
#initialize(host:, station_units: IMPERIAL_WEATHER_UNITS, desired_units: METRIC_WEATHER_UNITS) ⇒ LocalClient
constructor
A new instance of LocalClient.
- #sensors ⇒ Object
- #stations ⇒ Object
- #transform_like_api_v2(hash) ⇒ Object
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
#api ⇒ Object (readonly)
Returns the value of attribute api.
7 8 9 |
# File 'lib/weatherlink/local_client.rb', line 7 def api @api end |
#desired_units ⇒ Object (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_conditions ⇒ Object
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 |
#sensors ⇒ Object
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 |