Class: Reolink::HTTP
- Inherits:
-
Object
- Object
- Reolink::HTTP
- Defined in:
- lib/reolink/http.rb,
lib/reolink/http/version.rb
Overview
Provides an abstraction of the Reolink device API. See the Reolink API documentation for commands, parameters, and response fields.
Defined Under Namespace
Classes: Response
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#token_expires_at ⇒ Object
readonly
Returns the value of attribute token_expires_at.
Instance Method Summary collapse
- #command(method: :post, **kwargs) ⇒ Object
-
#initialize(host, username: nil, password: nil) ⇒ HTTP
constructor
A new instance of HTTP.
- #method_missing(method_name, params = {}, **kwargs) ⇒ Object
- #refresh_token(http:) ⇒ Object
- #respond_to_missing?(*_args) ⇒ Boolean
Constructor Details
#initialize(host, username: nil, password: nil) ⇒ HTTP
Returns a new instance of HTTP.
14 15 16 17 18 19 20 |
# File 'lib/reolink/http.rb', line 14 def initialize(host, username: nil, password: nil) @host = host @username = username @password = password @token = nil @token_expires_at = Time.new(0) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, params = {}, **kwargs) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/reolink/http.rb', line 22 def method_missing(method_name, params = {}, **kwargs) # Assume any missing method is a valid Reolink command. We have no good way to validate this: the API changes and # will be a maintenance burden, and GetAbility does not return all commands. An invalid command will still cause # a proper error response, so this functionality is correct, if not ideal. command(command: method_name.to_s.split("_").map(&:capitalize).join, params:, **kwargs) end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
12 13 14 |
# File 'lib/reolink/http.rb', line 12 def host @host end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
12 13 14 |
# File 'lib/reolink/http.rb', line 12 def token @token end |
#token_expires_at ⇒ Object (readonly)
Returns the value of attribute token_expires_at.
12 13 14 |
# File 'lib/reolink/http.rb', line 12 def token_expires_at @token_expires_at end |
Instance Method Details
#command(method: :post, **kwargs) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/reolink/http.rb', line 35 def command(method: :post, **kwargs) Net::HTTP.start(host, use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http| refresh_token(http:) if token_expires_at <= Time.now case method when :post then post(http:, **kwargs) when :get then get(http:, **kwargs) else raise "Unknown method #{method}" end end end |
#refresh_token(http:) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/reolink/http.rb', line 47 def refresh_token(http:) login_response = post(http:, command: "Login", params: { "User" => { "Version" => "0", "userName" => @username, "password" => @password } }) .first.value["Token"] @token = login_response["name"] @token_expires_at = Time.now + login_response["leaseTime"] end |
#respond_to_missing?(*_args) ⇒ Boolean
31 32 33 |
# File 'lib/reolink/http.rb', line 31 def respond_to_missing?(*_args) true end |