Class: HiEnergyAi::Response
- Inherits:
-
Object
- Object
- HiEnergyAi::Response
- Defined in:
- lib/hi_energy_ai/response.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #has_more? ⇒ Boolean
-
#initialize(status:, headers:, body:) ⇒ Response
constructor
A new instance of Response.
- #next_page ⇒ Object
- #next_page_params ⇒ Object
- #pagination ⇒ Object
- #success? ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(status:, headers:, body:) ⇒ Response
Returns a new instance of Response.
7 8 9 10 11 12 13 14 |
# File 'lib/hi_energy_ai/response.rb', line 7 def initialize(status:, headers:, body:) @status = status @headers = headers @body = body @raw = body @data = body.is_a?(Hash) ? (body["data"] || body[:data]) : body @meta = body.is_a?(Hash) ? (body["meta"] || body[:meta]) : nil end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
5 6 7 |
# File 'lib/hi_energy_ai/response.rb', line 5 def body @body end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
5 6 7 |
# File 'lib/hi_energy_ai/response.rb', line 5 def data @data end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
5 6 7 |
# File 'lib/hi_energy_ai/response.rb', line 5 def headers @headers end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
5 6 7 |
# File 'lib/hi_energy_ai/response.rb', line 5 def @meta end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
5 6 7 |
# File 'lib/hi_energy_ai/response.rb', line 5 def raw @raw end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
5 6 7 |
# File 'lib/hi_energy_ai/response.rb', line 5 def status @status end |
Instance Method Details
#[](key) ⇒ Object
20 21 22 23 24 |
# File 'lib/hi_energy_ai/response.rb', line 20 def [](key) return body[key] if body.is_a?(Hash) nil end |
#has_more? ⇒ Boolean
42 43 44 45 46 47 |
# File 'lib/hi_energy_ai/response.rb', line 42 def has_more? return ["has_more"] if .is_a?(Hash) && .key?("has_more") return [:has_more] if .is_a?(Hash) && .key?(:has_more) !next_page.nil? end |
#next_page ⇒ Object
36 37 38 39 40 |
# File 'lib/hi_energy_ai/response.rb', line 36 def next_page return nil unless .is_a?(Hash) ["next_page"] || [:next_page] end |
#next_page_params ⇒ Object
49 50 51 52 53 54 |
# File 'lib/hi_energy_ai/response.rb', line 49 def next_page_params page = next_page return nil if page.nil? { page: page } end |
#pagination ⇒ Object
30 31 32 33 34 |
# File 'lib/hi_energy_ai/response.rb', line 30 def pagination return if .is_a?(Hash) && .key?("pagination") body.is_a?(Hash) ? (body["pagination"] || body[:pagination]) : nil end |
#success? ⇒ Boolean
16 17 18 |
# File 'lib/hi_energy_ai/response.rb', line 16 def success? status >= 200 && status < 300 end |
#to_h ⇒ Object
26 27 28 |
# File 'lib/hi_energy_ai/response.rb', line 26 def to_h body.is_a?(Hash) ? body : { "data" => body } end |