Class: ToonApi

Inherits:
Object
  • Object
show all
Defined in:
lib/toon_api.rb,
lib/toon_api/version.rb

Constant Summary collapse

VERSION =
"1.0.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ ToonApi

Returns a new instance of ToonApi.



11
12
13
14
15
# File 'lib/toon_api.rb', line 11

def initialize(username, password)
  self.username = username
  self.password = password
  clear_toon_state
end

Instance Attribute Details

#_last_responseObject (readonly)

Returns the value of attribute _last_response.



9
10
11
# File 'lib/toon_api.rb', line 9

def _last_response
  @_last_response
end

#httpObject

Returns the value of attribute http.



8
9
10
# File 'lib/toon_api.rb', line 8

def http
  @http
end

#passwordObject

Returns the value of attribute password.



8
9
10
# File 'lib/toon_api.rb', line 8

def password
  @password
end

#session_dataObject

Returns the value of attribute session_data.



8
9
10
# File 'lib/toon_api.rb', line 8

def session_data
  @session_data
end

#toon_stateObject

Returns the value of attribute toon_state.



8
9
10
# File 'lib/toon_api.rb', line 8

def toon_state
  @toon_state
end

#usernameObject

Returns the value of attribute username.



8
9
10
# File 'lib/toon_api.rb', line 8

def username
  @username
end

Instance Method Details

#base_urlObject



17
18
19
# File 'lib/toon_api.rb', line 17

def base_url
  "https://toonopafstand.eneco.nl"
end

#clear_toon_stateObject



21
22
23
# File 'lib/toon_api.rb', line 21

def clear_toon_state
  self.toon_state = {}
end

#client_paramsObject



67
68
69
70
71
72
73
# 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 = {}) ⇒ Object



38
39
40
41
42
# 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_usageObject



95
96
97
98
# File 'lib/toon_api.rb', line 95

def get_gas_usage
  retrieve_toon_state
  toon_state["gasUsage"]
end

#get_power_usageObject



100
101
102
103
# File 'lib/toon_api.rb', line 100

def get_power_usage
  retrieve_toon_state
  toon_state["powerUsage"]
end

#get_program_stateObject



110
111
112
113
# File 'lib/toon_api.rb', line 110

def get_program_state
  retrieve_toon_state
  toon_state["thermostatInfo"]["activeState"]
end

#get_thermostat_infoObject



90
91
92
93
# File 'lib/toon_api.rb', line 90

def get_thermostat_info
  retrieve_toon_state
  toon_state["thermostatInfo"]
end

#get_thermostat_statesObject



105
106
107
108
# File 'lib/toon_api.rb', line 105

def get_thermostat_states
  retrieve_toon_state
  toon_state["thermostatStates"]
end

#loginObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/toon_api.rb', line 44

def 
  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

#logoutObject



59
60
61
62
63
64
65
# 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_stateObject



85
86
87
88
# File 'lib/toon_api.rb', line 85

def refresh_toon_state
  clear_toon_state
  retrieve_toon_state
end

#retrieve_toon_stateObject



75
76
77
78
79
80
81
82
83
# 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) ⇒ Object



34
35
36
# File 'lib/toon_api.rb', line 34

def uri_for_path(path)
  URI.parse(File.join(base_url, path))
end