Class: Reolink::HTTP::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/reolink/http.rb

Overview

Represents a response from the device.

cmd -- name of command
code -- status code. 0 means no errors.
initial -- initial value of attribute, for Get* requests when `verbose` is true
range -- range of possible values for attribute, for Get* requests when `verbose` is true
current -- current value of attribute, for Get* requests
error -- error code and details. nil if no error.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd:, code:, initial: nil, range: nil, value: nil, error: nil) ⇒ Response

Returns a new instance of Response.



91
92
93
94
95
96
97
98
# File 'lib/reolink/http.rb', line 91

def initialize(cmd:, code:, initial: nil, range: nil, value: nil, error: nil)
  @cmd = cmd
  @code = code
  @initial = initial&.[](cmd.delete_prefix("Get")) || initial
  @range = range&.[](cmd.delete_prefix("Get")) || range
  @value = value&.[](cmd.delete_prefix("Get")) || value
  @error = error
end

Instance Attribute Details

#cmdObject (readonly)

Returns the value of attribute cmd.



89
90
91
# File 'lib/reolink/http.rb', line 89

def cmd
  @cmd
end

#codeObject (readonly)

Returns the value of attribute code.



89
90
91
# File 'lib/reolink/http.rb', line 89

def code
  @code
end

#errorObject (readonly)

Returns the value of attribute error.



89
90
91
# File 'lib/reolink/http.rb', line 89

def error
  @error
end

#initialObject (readonly)

Returns the value of attribute initial.



89
90
91
# File 'lib/reolink/http.rb', line 89

def initial
  @initial
end

#rangeObject (readonly)

Returns the value of attribute range.



89
90
91
# File 'lib/reolink/http.rb', line 89

def range
  @range
end

#valueObject (readonly)

Returns the value of attribute value.



89
90
91
# File 'lib/reolink/http.rb', line 89

def value
  @value
end

Class Method Details

.from_json(str) ⇒ Object

Parse a JSON string from the device and return *an array* of Response objects.



82
83
84
85
86
87
# File 'lib/reolink/http.rb', line 82

def self.from_json(str)
  JSON.parse(str)
      .map do |response|
        new(**response.transform_keys(&:to_sym))
      end
end