Class: Reolink::HTTP

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#hostObject (readonly)

Returns the value of attribute host.



12
13
14
# File 'lib/reolink/http.rb', line 12

def host
  @host
end

#tokenObject (readonly)

Returns the value of attribute token.



12
13
14
# File 'lib/reolink/http.rb', line 12

def token
  @token
end

#token_expires_atObject (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:)
   = post(http:, command: "Login", params: { "User" => { "Version" => "0",
                                                                       "userName" => @username,
                                                                       "password" => @password } })
                   .first.value["Token"]
  @token = ["name"]
  @token_expires_at = Time.now + ["leaseTime"]
end

#respond_to_missing?(*_args) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/reolink/http.rb', line 31

def respond_to_missing?(*_args)
  true
end