Class: Grac::Response
- Inherits:
-
Object
- Object
- Grac::Response
- Extended by:
- Forwardable
- Defined in:
- lib/grac/response.rb
Instance Method Summary collapse
- #content_type ⇒ Object
-
#initialize(typhoeus_response) ⇒ Response
constructor
A new instance of Response.
- #json_content? ⇒ Boolean
- #parsed_json ⇒ Object
- #parsed_or_raw_body ⇒ Object
Constructor Details
#initialize(typhoeus_response) ⇒ Response
Returns a new instance of Response.
15 16 17 |
# File 'lib/grac/response.rb', line 15 def initialize(typhoeus_response) @response = typhoeus_response end |
Instance Method Details
#content_type ⇒ Object
19 20 21 |
# File 'lib/grac/response.rb', line 19 def content_type @response.headers["Content-Type"] end |
#json_content? ⇒ Boolean
23 24 25 |
# File 'lib/grac/response.rb', line 23 def json_content? !content_type.nil? && content_type.match('application/json') end |
#parsed_json ⇒ Object
27 28 29 30 31 |
# File 'lib/grac/response.rb', line 27 def parsed_json Oj.load(body) rescue Oj::ParseError, EncodingError raise Exception::InvalidContent.new(body, 'json') end |
#parsed_or_raw_body ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/grac/response.rb', line 33 def parsed_or_raw_body return body unless json_content? begin parsed_json rescue Exception::InvalidContent body end end |