class WeatherLink::APIv2

Constants

BASE_URI
RECORD_FIELD_UNITS

TODO: Eliminate duplicate data e.g. rain_rate_last_{in,mm,clicks} TODO: Wind speeds are actually in mph not m/s?

RECORD_TYPES
RECORD_TYPES_BY_ID
SYSTEM_TYPES

Attributes

api_key[R]
api_secret[R]
units[R]

Public Class Methods

new(api_key:, api_secret:, units: IMPERIAL_WEATHER_UNITS) click to toggle source
# File lib/weatherlink/api_v2.rb, line 181
def initialize(api_key:, api_secret:, units: IMPERIAL_WEATHER_UNITS)
  @api_key = api_key
  @api_secret = api_secret
  @units = units
end
record_type(id) click to toggle source
# File lib/weatherlink/api_v2.rb, line 131
def self.record_type(id)
  RECORD_TYPES_BY_ID[id]
end

Public Instance Methods

api_signature(path_params: {}, query_params: {}) click to toggle source
# File lib/weatherlink/api_v2.rb, line 278
def api_signature(path_params: {}, query_params: {})
  OpenSSL::HMAC.hexdigest('SHA256', api_secret, stuffed_params(path_params.merge(query_params)))
end
current(id) click to toggle source
# File lib/weatherlink/api_v2.rb, line 223
def current(id)
  request(path: 'current', path_params: { 'station-id' => id })
end
historic(id, start_timestamp, end_timestamp) click to toggle source
# File lib/weatherlink/api_v2.rb, line 227
def historic(id, start_timestamp, end_timestamp)
  request(
    path: 'historic',
    path_params: { 'station-id' => id },
    query_params: { 'start-timestamp' => start_timestamp, 'end-timestamp' => end_timestamp }
  )
end
last_day(id) click to toggle source
# File lib/weatherlink/api_v2.rb, line 243
def last_day(id)
  last_seconds(id, 86_400)
end
last_hour(id) click to toggle source
# File lib/weatherlink/api_v2.rb, line 239
def last_hour(id)
  last_seconds(id, 3600)
end
last_seconds(id, seconds) click to toggle source
# File lib/weatherlink/api_v2.rb, line 235
def last_seconds(id, seconds)
  historic(id, Time.now.to_i - seconds, Time.now.to_i)
end
node(ids = nil)
Alias for: nodes
nodes(ids = nil) click to toggle source
# File lib/weatherlink/api_v2.rb, line 207
def nodes(ids = nil)
  request(path: 'nodes', path_params: { 'node-ids' => optional_array_param(ids) })
end
Also aliased as: node
optional_array_param(param) click to toggle source

private

# File lib/weatherlink/api_v2.rb, line 270
def optional_array_param(param)
  param.is_a?(Array) ? param.join(',') : param
end
request(path:, path_params: {}, query_params: {}) click to toggle source
# File lib/weatherlink/api_v2.rb, line 247
def request(path:, path_params: {}, query_params: {})
  uri = request_uri(path: path, path_params: path_params, query_params: query_params)
  response = Net::HTTP.get_response(uri)
  JSON.parse(response.body)
end
request_uri(path:, path_params: {}, query_params: {}) click to toggle source
# File lib/weatherlink/api_v2.rb, line 253
def request_uri(path:, path_params: {}, query_params: {})
  used_path_params = path_params.compact

  request_params = query_params.merge({ 't' => Time.now.to_i, 'api-key' => api_key })
  request_params.merge!(
    {
      'api-signature' => api_signature(path_params: used_path_params, query_params: request_params),
    }
  )

  uri = ([BASE_URI, path] + Array(used_path_params.values)).compact.join('/')

  URI("#{uri}?#{URI.encode_www_form(request_params)}")
end
sensor(ids = nil)
Alias for: sensors
sensor_activity(ids = nil) click to toggle source
# File lib/weatherlink/api_v2.rb, line 219
def sensor_activity(ids = nil)
  request(path: 'sensor-activity', path_params: { 'sensor-ids' => optional_array_param(ids) })
end
sensor_catalog() click to toggle source
# File lib/weatherlink/api_v2.rb, line 197
def sensor_catalog
  request(path: 'sensor-catalog')
end
sensors(ids = nil) click to toggle source
# File lib/weatherlink/api_v2.rb, line 213
def sensors(ids = nil)
  request(path: 'sensors', path_params: { 'sensor-ids' => optional_array_param(ids) })
end
Also aliased as: sensor
station(ids = nil)
Alias for: stations
stations(ids = nil) click to toggle source
# File lib/weatherlink/api_v2.rb, line 201
def stations(ids = nil)
  request(path: 'stations', path_params: { 'station-ids' => optional_array_param(ids) })
end
Also aliased as: station
stuffed_params(params) click to toggle source
# File lib/weatherlink/api_v2.rb, line 274
def stuffed_params(params)
  params.sort_by { |k, _| k }.map { |k, v| k.to_s + v.to_s }.join
end
type_for(field) click to toggle source
# File lib/weatherlink/api_v2.rb, line 187
def type_for(field)
  return nil unless [String, Symbol].include?(field.class)

  RECORD_FIELD_UNITS.fetch(field.to_sym, nil)
end
unit_for(field) click to toggle source
# File lib/weatherlink/api_v2.rb, line 193
def unit_for(field)
  units.fetch(type_for(field))
end