Class: Zaptec::Client
- Inherits:
-
Object
- Object
- Zaptec::Client
- Defined in:
- lib/zaptec/client.rb
Constant Summary collapse
- BASE_URL =
"https://api.zaptec.com".freeze
- USER_ROLE =
1- OWNER_ROLE =
2- TOKENS_CACHE_KEY =
"zaptec.auth.tokens".freeze
Instance Attribute Summary collapse
-
#credentials ⇒ Object
readonly
Returns the value of attribute credentials.
Instance Method Summary collapse
- #authorize(username:, password:) ⇒ Object
- #chargers ⇒ Object
- #get_installation(installation_id) ⇒ Object
- #get_installation_hierarchy(installation_id) ⇒ Object
- #grant_access_url(lookup_key:, partner_name:, redirect_url: nil, language: "en") ⇒ Object
-
#initialize(username:, password:, token_cache: ActiveSupport::Cache::MemoryStore.new, encryptor: NullEncryptor.new) ⇒ Client
constructor
A new instance of Client.
- #pause_charging(charger_id) ⇒ Object
- #resume_charging(charger_id) ⇒ Object
- #state(charger_id, device_type) ⇒ Object
Constructor Details
#initialize(username:, password:, token_cache: ActiveSupport::Cache::MemoryStore.new, encryptor: NullEncryptor.new) ⇒ Client
Returns a new instance of Client.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/zaptec/client.rb', line 16 def initialize( username:, password:, token_cache: ActiveSupport::Cache::MemoryStore.new, encryptor: NullEncryptor.new ) @username = username @password = password @token_cache = token_cache @encryptor = encryptor end |
Instance Attribute Details
#credentials ⇒ Object (readonly)
Returns the value of attribute credentials.
8 9 10 |
# File 'lib/zaptec/client.rb', line 8 def credentials @credentials end |
Instance Method Details
#authorize(username:, password:) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/zaptec/client.rb', line 35 def (username:, password:) raise Errors::ParameterMissing if username.blank? || password.blank? start = Time.current response = connection.post( "#{BASE_URL}/oauth/token", { username:, password:, grant_type: "password", }.to_query, { "Content-Type": "application/x-www-form-urlencoded", }, ) @credentials = Zaptec::Credentials.new( response.body["access_token"], start + response.body["expires_in"].to_f, ) rescue Faraday::BadRequestError raise Errors::AuthorizationFailed end |
#chargers ⇒ Object
61 62 63 64 65 66 |
# File 'lib/zaptec/client.rb', line 61 def chargers get("/api/chargers", { Roles: USER_ROLE | OWNER_ROLE }) .body .fetch("Data") .map { |data| Charger.new(data) } end |
#get_installation(installation_id) ⇒ Object
69 70 71 72 |
# File 'lib/zaptec/client.rb', line 69 def get_installation(installation_id) get("/api/installation/#{installation_id}") .then { |response| Installation.new(response.body) } end |
#get_installation_hierarchy(installation_id) ⇒ Object
75 76 77 78 |
# File 'lib/zaptec/client.rb', line 75 def get_installation_hierarchy(installation_id) get("/api/installation/#{installation_id}/hierarchy") .then { |response| InstallationHierarchy.new(response.body) } end |
#grant_access_url(lookup_key:, partner_name:, redirect_url: nil, language: "en") ⇒ Object
29 30 31 32 |
# File 'lib/zaptec/client.rb', line 29 def grant_access_url(lookup_key:, partner_name:, redirect_url: nil, language: "en") query = URI.encode_www_form(partnerName: partner_name, returnUrl: redirect_url, lang: language) "https://portal.zaptec.com/#!/access/request/#{lookup_key}?#{query}" end |
#pause_charging(charger_id) ⇒ Object
93 |
# File 'lib/zaptec/client.rb', line 93 def pause_charging(charger_id) = send_command(charger_id, :StopChargingFinal) |
#resume_charging(charger_id) ⇒ Object
95 |
# File 'lib/zaptec/client.rb', line 95 def resume_charging(charger_id) = send_command(charger_id, :ResumeCharging) |
#state(charger_id, device_type) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/zaptec/client.rb', line 81 def state(charger_id, device_type) get("/api/chargers/#{charger_id}/state") .body .to_h do |state| [ Constants.observation_state_id_to_name(state_id: state.fetch("StateId"), device_type:), state.fetch("ValueAsString", nil), ] end .then { |data| State.new(data) } end |