Class: Regaliator::Response

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

Constant Summary collapse

PAGINATION_HEADER =
'X-Pagination'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_response) ⇒ Response

Returns a new instance of Response.



10
11
12
# File 'lib/regaliator/response.rb', line 10

def initialize(http_response)
  @http_response = http_response
end

Instance Attribute Details

#http_responseObject (readonly)

Returns the value of attribute http_response.



8
9
10
# File 'lib/regaliator/response.rb', line 8

def http_response
  @http_response
end

Instance Method Details

#dataObject



14
15
16
17
18
19
20
21
22
# File 'lib/regaliator/response.rb', line 14

def data
  @data ||= begin
    JSON::parse(http_response.body).tap do |hsh|
      hsh.merge!('pagination' => pagination) if paginated?
    end
  rescue
    { message: 'Server returned a non-json error' }
  end
end

#fail?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/regaliator/response.rb', line 28

def fail?
  !success?
end

#paginated?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/regaliator/response.rb', line 32

def paginated?
  http_response.key?(PAGINATION_HEADER)
end

#paginationObject



36
37
38
# File 'lib/regaliator/response.rb', line 36

def pagination
  @pagination ||= JSON::parse(http_response[PAGINATION_HEADER])
end

#success?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/regaliator/response.rb', line 24

def success?
  @success ||= http_response.kind_of?(Net::HTTPSuccess)
end