Class: Regaliator::Response
- Inherits:
-
Object
- Object
- Regaliator::Response
- Defined in:
- lib/regaliator/response.rb
Constant Summary collapse
- PAGINATION_HEADER =
'X-Pagination'.freeze
Instance Attribute Summary collapse
-
#http_response ⇒ Object
readonly
Returns the value of attribute http_response.
Instance Method Summary collapse
- #data ⇒ Object
- #fail? ⇒ Boolean
-
#initialize(http_response) ⇒ Response
constructor
A new instance of Response.
- #paginated? ⇒ Boolean
- #pagination ⇒ Object
- #success? ⇒ Boolean
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_response ⇒ Object (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
#data ⇒ Object
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
28 29 30 |
# File 'lib/regaliator/response.rb', line 28 def fail? !success? end |
#paginated? ⇒ Boolean
32 33 34 |
# File 'lib/regaliator/response.rb', line 32 def paginated? http_response.key?(PAGINATION_HEADER) end |
#pagination ⇒ Object
36 37 38 |
# File 'lib/regaliator/response.rb', line 36 def pagination @pagination ||= JSON::parse(http_response[PAGINATION_HEADER]) end |
#success? ⇒ Boolean
24 25 26 |
# File 'lib/regaliator/response.rb', line 24 def success? @success ||= http_response.kind_of?(Net::HTTPSuccess) end |