class ToonApi

Constants

VERSION

Attributes

_last_response[R]
http[RW]
password[RW]
session_data[RW]
toon_state[RW]
username[RW]

Public Class Methods

new(username, password) click to toggle source
# File lib/toon_api.rb, line 11
def initialize(username, password)
  self.username = username
  self.password = password
  clear_toon_state
end

Public Instance Methods

base_url() click to toggle source
# File lib/toon_api.rb, line 17
def base_url
  "https://toonopafstand.eneco.nl"
end
clear_toon_state() click to toggle source
# File lib/toon_api.rb, line 21
def clear_toon_state
  self.toon_state = {}
end
client_params() click to toggle source
# File lib/toon_api.rb, line 67
def client_params
  {
    clientId: self.session_data["clientId"],
    clientIdChecksum: self.session_data["clientIdChecksum"],
    random: SecureRandom.uuid
  }
end
get(path, params = {}, headers={}) click to toggle source
# File lib/toon_api.rb, line 38
def get(path, params = {}, headers={})
  uri = uri_for_path("#{path}?#{URI.encode_www_form(params)}")
  request = Net::HTTP::Get.new(uri.request_uri, {})
  perform(request)
end
get_gas_usage() click to toggle source
# File lib/toon_api.rb, line 95
def get_gas_usage
  retrieve_toon_state
  toon_state["gasUsage"]
end
get_power_usage() click to toggle source
# File lib/toon_api.rb, line 100
def get_power_usage
  retrieve_toon_state
  toon_state["powerUsage"]
end
get_program_state() click to toggle source
# File lib/toon_api.rb, line 110
def get_program_state
  retrieve_toon_state
  toon_state["thermostatInfo"]["activeState"]
end
get_thermostat_info() click to toggle source
# File lib/toon_api.rb, line 90
def get_thermostat_info
  retrieve_toon_state
  toon_state["thermostatInfo"]
end
get_thermostat_states() click to toggle source
# File lib/toon_api.rb, line 105
def get_thermostat_states
  retrieve_toon_state
  toon_state["thermostatStates"]
end
login() click to toggle source
# File lib/toon_api.rb, line 44
def login
  response = get("/toonMobileBackendWeb/client/login", { username: username, password: password })

  self.session_data = JSON.parse(response.body)

  params = client_params.merge({
    agreementId: session_data["agreements"][0]["agreementId"],
    agreementIdChecksum: session_data["agreements"][0]["agreementIdChecksum"],
  })

  response = get("/toonMobileBackendWeb/client/auth/start", params)

  successful_response?(response)
end
logout() click to toggle source
# File lib/toon_api.rb, line 59
def logout
  return unless session_data
  response = get("/toonMobileBackendWeb/client/auth/logout", client_params)
  clear_toon_state
  self.session_data = nil
  successful_response?(response)
end
refresh_toon_state() click to toggle source
# File lib/toon_api.rb, line 85
def refresh_toon_state
  clear_toon_state
  retrieve_toon_state
end
retrieve_toon_state() click to toggle source
# File lib/toon_api.rb, line 75
def retrieve_toon_state
  return unless session_data
  return toon_state unless toon_state.empty?

  self.toon_state = begin
    response = get("/toonMobileBackendWeb/client/auth/retrieveToonState", client_params)
    JSON.parse(response.body)
  end
end
uri_for_path(path) click to toggle source
# File lib/toon_api.rb, line 34
def uri_for_path(path)
  URI.parse(File.join(base_url, path))
end

Private Instance Methods

perform(request) click to toggle source
# File lib/toon_api.rb, line 120
def perform(request)
  @_last_response = http.request(request)
end
successful_response?(response) click to toggle source
# File lib/toon_api.rb, line 116
def successful_response?(response)
  JSON.parse(response.body)['success'] ==  true
end