Class: ToonApi
- Inherits:
-
Object
- Object
- ToonApi
- Defined in:
- lib/toon_api.rb,
lib/toon_api/version.rb
Constant Summary collapse
- VERSION =
"1.0.3"
Instance Attribute Summary collapse
-
#_last_response ⇒ Object
readonly
Returns the value of attribute _last_response.
-
#http ⇒ Object
Returns the value of attribute http.
-
#password ⇒ Object
Returns the value of attribute password.
-
#session_data ⇒ Object
Returns the value of attribute session_data.
-
#toon_state ⇒ Object
Returns the value of attribute toon_state.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #base_url ⇒ Object
- #clear_toon_state ⇒ Object
- #client_params ⇒ Object
- #get(path, params = {}, headers = {}) ⇒ Object
- #get_gas_usage ⇒ Object
- #get_power_usage ⇒ Object
- #get_program_state ⇒ Object
- #get_thermostat_info ⇒ Object
- #get_thermostat_states ⇒ Object
-
#initialize(username, password) ⇒ ToonApi
constructor
A new instance of ToonApi.
- #login ⇒ Object
- #logout ⇒ Object
- #refresh_toon_state ⇒ Object
- #retrieve_toon_state ⇒ Object
- #uri_for_path(path) ⇒ Object
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_response ⇒ Object (readonly)
Returns the value of attribute _last_response.
9 10 11 |
# File 'lib/toon_api.rb', line 9 def _last_response @_last_response end |
#http ⇒ Object
Returns the value of attribute http.
8 9 10 |
# File 'lib/toon_api.rb', line 8 def http @http end |
#password ⇒ Object
Returns the value of attribute password.
8 9 10 |
# File 'lib/toon_api.rb', line 8 def password @password end |
#session_data ⇒ Object
Returns the value of attribute session_data.
8 9 10 |
# File 'lib/toon_api.rb', line 8 def session_data @session_data end |
#toon_state ⇒ Object
Returns the value of attribute toon_state.
8 9 10 |
# File 'lib/toon_api.rb', line 8 def toon_state @toon_state end |
#username ⇒ Object
Returns the value of attribute username.
8 9 10 |
# File 'lib/toon_api.rb', line 8 def username @username end |
Instance Method Details
#base_url ⇒ Object
17 18 19 |
# File 'lib/toon_api.rb', line 17 def base_url "https://toonopafstand.eneco.nl" end |
#clear_toon_state ⇒ Object
21 22 23 |
# File 'lib/toon_api.rb', line 21 def clear_toon_state self.toon_state = {} end |
#client_params ⇒ Object
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_usage ⇒ Object
95 96 97 98 |
# File 'lib/toon_api.rb', line 95 def get_gas_usage retrieve_toon_state toon_state["gasUsage"] end |
#get_power_usage ⇒ Object
100 101 102 103 |
# File 'lib/toon_api.rb', line 100 def get_power_usage retrieve_toon_state toon_state["powerUsage"] end |
#get_program_state ⇒ Object
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_info ⇒ Object
90 91 92 93 |
# File 'lib/toon_api.rb', line 90 def get_thermostat_info retrieve_toon_state toon_state["thermostatInfo"] end |
#get_thermostat_states ⇒ Object
105 106 107 108 |
# File 'lib/toon_api.rb', line 105 def get_thermostat_states retrieve_toon_state toon_state["thermostatStates"] end |
#login ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# 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 ⇒ Object
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_state ⇒ Object
85 86 87 88 |
# File 'lib/toon_api.rb', line 85 def refresh_toon_state clear_toon_state retrieve_toon_state end |
#retrieve_toon_state ⇒ Object
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 |