Class: FavroApi::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/favro_api/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Response

Returns a new instance of Response.



9
10
11
# File 'lib/favro_api/response.rb', line 9

def initialize(opts = {})
  self.response = opts.delete(:response)
end

Instance Attribute Details

#entitiesObject

Returns the value of attribute entities.



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

def entities
  @entities
end

#limitObject

Returns the value of attribute limit.



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

def limit
  @limit
end

#pageObject

Returns the value of attribute page.



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

def page
  @page
end

#pagesObject

Returns the value of attribute pages.



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

def pages
  @pages
end

#request_idObject

Returns the value of attribute request_id.



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

def request_id
  @request_id
end

#responseObject

Returns the value of attribute response.



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

def response
  @response
end

Instance Method Details

#backend_identifierObject



35
36
37
# File 'lib/favro_api/response.rb', line 35

def backend_identifier
  response.headers['x-favro-backend-identifier']
end

#error?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/favro_api/response.rb', line 13

def error?
  status >= 400
end

#parse(type: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/favro_api/response.rb', line 17

def parse(type: nil)
  self.entities =
    if type
      type.send(:parse, parsed_response['entities'])
    else
      parsed_response['entities']
    end
  self.pages      = parsed_response['pages']
  self.page       = parsed_response['page']
  self.limit      = parsed_response['limit']
  self.request_id = parsed_response['requestId']
  self
end

#parsed_responseObject



31
32
33
# File 'lib/favro_api/response.rb', line 31

def parsed_response
  @parsed_response ||= JSON.parse(body)
end

#ratelimitObject



39
40
41
42
43
44
45
# File 'lib/favro_api/response.rb', line 39

def ratelimit
  {
    limit:     response.headers['x-ratelimit-limit'].to_i,
    remaining: response.headers['x-ratelimit-remaining'].to_i,
    reset:     Time.parse(response.headers['x-ratelimit-reset'])
  }
end