Class: Grac::Response

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

Instance Method Summary collapse

Constructor Details

#initialize(typhoeus_response) ⇒ Response

Returns a new instance of Response.



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

def initialize(typhoeus_response)
  @response    = typhoeus_response
end

Instance Method Details

#content_typeObject



20
21
22
# File 'lib/grac/response.rb', line 20

def content_type
  @response.headers["Content-Type"]
end

#json_content?Boolean

Returns:

  • (Boolean)


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

def json_content?
  !content_type.nil? && content_type.match('application/json')
end

#parsed_jsonObject



28
29
30
31
32
# File 'lib/grac/response.rb', line 28

def parsed_json
  JSON.parse(body)
rescue JSON::ParserError
  raise Exception::InvalidContent.new(body, 'json')
end

#parsed_or_raw_bodyObject



34
35
36
37
38
39
40
41
42
# File 'lib/grac/response.rb', line 34

def parsed_or_raw_body
  return body unless json_content?

  begin
    parsed_json
  rescue Exception::InvalidContent
    body
  end
end