Class: HiEnergyAi::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/hi_energy_ai/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bodyObject (readonly)

Returns the value of attribute body.



5
6
7
# File 'lib/hi_energy_ai/response.rb', line 5

def body
  @body
end

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/hi_energy_ai/response.rb', line 5

def data
  @data
end

#headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'lib/hi_energy_ai/response.rb', line 5

def headers
  @headers
end

#metaObject (readonly)

Returns the value of attribute meta.



5
6
7
# File 'lib/hi_energy_ai/response.rb', line 5

def meta
  @meta
end

#rawObject (readonly)

Returns the value of attribute raw.



5
6
7
# File 'lib/hi_energy_ai/response.rb', line 5

def raw
  @raw
end

#statusObject (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

Returns:

  • (Boolean)


42
43
44
45
46
47
# File 'lib/hi_energy_ai/response.rb', line 42

def has_more?
  return meta["has_more"] if meta.is_a?(Hash) && meta.key?("has_more")
  return meta[:has_more] if meta.is_a?(Hash) && meta.key?(:has_more)

  !next_page.nil?
end

#next_pageObject



36
37
38
39
40
# File 'lib/hi_energy_ai/response.rb', line 36

def next_page
  return nil unless meta.is_a?(Hash)

  meta["next_page"] || meta[:next_page]
end

#next_page_paramsObject



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

#paginationObject



30
31
32
33
34
# File 'lib/hi_energy_ai/response.rb', line 30

def pagination
  return meta if meta.is_a?(Hash) && meta.key?("pagination")

  body.is_a?(Hash) ? (body["pagination"] || body[:pagination]) : nil
end

#success?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/hi_energy_ai/response.rb', line 16

def success?
  status >= 200 && status < 300
end

#to_hObject



26
27
28
# File 'lib/hi_energy_ai/response.rb', line 26

def to_h
  body.is_a?(Hash) ? body : { "data" => body }
end