Module: Renstar::APIClient

Includes:
Control, Query, Settings
Included in:
Thermostat
Defined in:
lib/renstar/api_client.rb,
lib/renstar/api_client/query.rb,
lib/renstar/api_client/control.rb,
lib/renstar/api_client/settings.rb,
lib/renstar/api_client/api_object.rb,
lib/renstar/api_client/api_objects/info.rb,
lib/renstar/api_client/api_objects/alert.rb,
lib/renstar/api_client/api_objects/sensor.rb,
lib/renstar/api_client/api_objects/runtime.rb

Overview

The actual client that handles getting, posting, and parsing API responses

Defined Under Namespace

Modules: Control, Query, Settings Classes: APIError, APIObject, APIUnknownResponseError, Alert, Info, Runtime, Sensor

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Settings

#settings

Methods included from Control

#control

Methods included from Query

#alerts, #info, #runtimes, #sensors

Class Method Details

.key_to_description(type, key) ⇒ Object



44
45
46
# File 'lib/renstar/api_client.rb', line 44

def self.key_to_description(type, key)
  @api_ref.dig(type, key, 'description') || key
end

.value_to_formatted(type, key, value) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/renstar/api_client.rb', line 48

def self.value_to_formatted(type, key, value)
  case @api_ref.dig(type, key, 'values')
  when 'raw'
    value
  when 'epoch'
    Time.at(value)
  else
    @api_ref.dig(type, key, 'values', value.to_s) || value
  end
end

Instance Method Details

#get(endpoint) ⇒ Object



19
20
21
22
23
# File 'lib/renstar/api_client.rb', line 19

def get(endpoint)
  uri = URI(location + endpoint)
  response = Net::HTTP.get_response(uri)
  JSON.parse(response.body)
end

#post(endpoint, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/renstar/api_client.rb', line 25

def post(endpoint, options = {})
  uri = URI(location + endpoint)
  response = Net::HTTP.post_form(uri, options)
  json = JSON.parse(response.body)
  if json['error']
    raise APIError, json['reason']
  elsif json['success']
    json
  else
    raise APIUnknownResponseError, response.body
  end
end