Class: Cage::Response

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

Constant Summary collapse

JSON_MIME_REGEX =
%r{(?:application/json|application/x-javascript|
text/javascript|text/x-javascript|text/x-json)}x
XML_MIME_REGEX =
%r{(?:application/xml|text/xml|application/atom)}x

Instance Method Summary collapse

Constructor Details

#initialize(faraday_response) ⇒ Response

Returns a new instance of Response.



7
8
9
# File 'lib/cage/response.rb', line 7

def initialize faraday_response
  @faraday_response = faraday_response
end

Instance Method Details

#bodyObject



11
12
13
# File 'lib/cage/response.rb', line 11

def body
  @body ||= parsed_body
end

#format_for(content_type_string) ⇒ Object



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

def format_for content_type_string
  case content_type_string
  when JSON_MIME_REGEX
    :json
  when XML_MIME_REGEX
    :xml
  end
end

#headersObject



19
20
21
# File 'lib/cage/response.rb', line 19

def headers
  @faraday_response.headers
end

#inspectObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cage/response.rb', line 43

def inspect
  <<-PRETTY

Status: #{@faraday_response.status}

Headers:
#{@faraday_response.headers.map { |k, v| "  #{k}: #{v}" }.join "\n"}

Body:
  #{body}

#<Cage::Response>
  PRETTY
end

#parsed_bodyObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/cage/response.rb', line 23

def parsed_body
  case format_for @faraday_response.headers["content-type"]
  when :json
    MultiJson.decode @faraday_response.body
  when :xml
    Nokogiri::XML::Document.new @faraday_response.body
  else
    @faraday_response.body
  end
end

#statusObject



15
16
17
# File 'lib/cage/response.rb', line 15

def status
  @faraday_response.status
end