class WeatherLink::LocalAPIv1

Constants

LOCAL_API
RECORD_FIELD_UNITS
RECORD_TYPES
RECORD_TYPES_BY_ID

Attributes

host[R]
units[R]

Public Class Methods

new(host:, units: IMPERIAL_WEATHER_UNITS) click to toggle source
# File lib/weatherlink/local_api_v1.rb, line 95
def initialize(host:, units: IMPERIAL_WEATHER_UNITS)
  @host = host
  @units = units
end
record_type(id) click to toggle source
# File lib/weatherlink/local_api_v1.rb, line 47
def self.record_type(id)
  RECORD_TYPES_BY_ID[id]
end

Public Instance Methods

base_uri() click to toggle source
# File lib/weatherlink/local_api_v1.rb, line 123
def base_uri
  "http://#{host}/v1"
end
current_conditions() click to toggle source
# File lib/weatherlink/local_api_v1.rb, line 110
def current_conditions
  request(path: 'current_conditions')
end
optional_array_param(param) click to toggle source

private

# File lib/weatherlink/local_api_v1.rb, line 139
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/local_api_v1.rb, line 114
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_response = JSON.parse(response.body)
  raise RequestError, json_response['error'] if json_response['error']

  json_response['data']
end
request_uri(path:, path_params: {}, query_params: {}) click to toggle source
# File lib/weatherlink/local_api_v1.rb, line 127
def request_uri(path:, path_params: {}, query_params: {})
  uri = ([base_uri, path] + Array(path_params.values)).compact.join('/')

  if query_params.none?
    URI(uri)
  else
    URI("#{uri}?#{URI.encode_www_form(query_params)}")
  end
end
type_for(field) click to toggle source
# File lib/weatherlink/local_api_v1.rb, line 100
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/local_api_v1.rb, line 106
def unit_for(field)
  units.fetch(type_for(field))
end