Class: TuyaCloud::API::Auth
- Inherits:
-
Object
- Object
- TuyaCloud::API::Auth
- Defined in:
- lib/tuya_cloud/api.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#brand ⇒ Object
Returns the value of attribute brand.
-
#country_code ⇒ Object
Returns the value of attribute country_code.
-
#expire_time ⇒ Object
Returns the value of attribute expire_time.
-
#password ⇒ Object
Returns the value of attribute password.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
-
#region ⇒ Object
Returns the value of attribute region.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #cloud_url ⇒ Object
-
#initialize(username, password, country_code, brand, region) ⇒ Auth
constructor
A new instance of Auth.
- #login ⇒ Object
- #process_auth_response(json) ⇒ Object
- #process_request(name, namespace, device_id: nil, payload: {}) ⇒ Object
- #refresh_access_token ⇒ Object
- #token_expired? ⇒ Boolean
Constructor Details
#initialize(username, password, country_code, brand, region) ⇒ Auth
Returns a new instance of Auth.
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/tuya_cloud/api.rb', line 60 def initialize(username, password, country_code, brand, region) raise ArgumentError unless username.is_a?(String) && password.is_a?(String) && country_code.is_a?(String) && brand.is_a?(String) && region.is_a?(String) self.username = username self.password = password self.country_code = country_code self.brand = brand self.region = region end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
51 52 53 |
# File 'lib/tuya_cloud/api.rb', line 51 def access_token @access_token end |
#brand ⇒ Object
Returns the value of attribute brand.
51 52 53 |
# File 'lib/tuya_cloud/api.rb', line 51 def brand @brand end |
#country_code ⇒ Object
Returns the value of attribute country_code.
51 52 53 |
# File 'lib/tuya_cloud/api.rb', line 51 def country_code @country_code end |
#expire_time ⇒ Object
Returns the value of attribute expire_time.
51 52 53 |
# File 'lib/tuya_cloud/api.rb', line 51 def expire_time @expire_time end |
#password ⇒ Object
Returns the value of attribute password.
51 52 53 |
# File 'lib/tuya_cloud/api.rb', line 51 def password @password end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
51 52 53 |
# File 'lib/tuya_cloud/api.rb', line 51 def refresh_token @refresh_token end |
#region ⇒ Object
Returns the value of attribute region.
51 52 53 |
# File 'lib/tuya_cloud/api.rb', line 51 def region @region end |
#username ⇒ Object
Returns the value of attribute username.
51 52 53 |
# File 'lib/tuya_cloud/api.rb', line 51 def username @username end |
Instance Method Details
#cloud_url ⇒ Object
74 75 76 |
# File 'lib/tuya_cloud/api.rb', line 74 def cloud_url CLOUD_URL.gsub('%', region) end |
#login ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/tuya_cloud/api.rb', line 78 def login uri = URI.parse("#{cloud_url}/homeassistant/auth.do") response = Net::HTTP.post_form(uri, userName: username, password: password, countryCode: country_code, bizType: brand, from: 'tuya') unless response.is_a?(Net::HTTPOK) raise Error, 'invalid HTTP response from Tuya Cloud whilst trying to '\ 'get access token' end json = JSON.parse(response.body) process_auth_response(json) end |
#process_auth_response(json) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/tuya_cloud/api.rb', line 114 def process_auth_response(json) unless json['access_token'] && json['refresh_token'] && json['expires_in'] raise Error, 'invalid JSON response from Tuya Cloud whilst trying to '\ 'get access token' end self.access_token = json['access_token'] self.refresh_token = json['refresh_token'] self.expire_time = Time.now + json['expires_in'].to_i true end |
#process_request(name, namespace, device_id: nil, payload: {}) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/tuya_cloud/api.rb', line 129 def process_request(name, namespace, device_id: nil, payload: {}) raise ArgumentError unless name.is_a?(String) && namespace.is_a?(String) refresh_access_token header = { name: name, namespace: namespace, payloadVersion: 1 } payload[:accessToken] = access_token payload[:devId] = device_id unless namespace == 'discovery' data = { header: header, payload: payload } uri = URI.parse("#{cloud_url}/homeassistant/skill") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/json') request.body = data.to_json response = http.request(request) unless response.is_a?(Net::HTTPOK) raise Error, 'request was not processed by Tuya Cloud' end json = JSON.parse(response.body) unless json['header']['code'] == 'SUCCESS' raise Error, 'request was not processed by Tuya Cloud' end json['payload'] end |
#refresh_access_token ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/tuya_cloud/api.rb', line 96 def refresh_access_token return unless token_expired? uri = URI.parse("#{cloud_url}/homeassistant/access.do?grant_type=refresh_token&"\ "refresh_token=#{refresh_token}") response = Net::HTTP.get uri unless response.is_a?(String) && !response.empty? raise Error, 'failed to refresh access token' end json = JSON.parse(response) process_auth_response(json) end |
#token_expired? ⇒ Boolean
110 111 112 |
# File 'lib/tuya_cloud/api.rb', line 110 def token_expired? Time.now > expire_time end |