Class: Restify::Response
- Inherits:
-
Object
- Object
- Restify::Response
- Defined in:
- lib/restify/response.rb
Overview
Constant Summary collapse
- SYMBOL_TO_STATUS_CODE =
Map of status symbols to codes. From Rack::Utils.
Rack::Utils::SYMBOL_TO_STATUS_CODE
- STATUS_CODE_TO_SYMBOL =
Map of status codes to symbols.
SYMBOL_TO_STATUS_CODE.invert
Instance Attribute Summary collapse
-
#body ⇒ String
readonly
Response body as string.
-
#code ⇒ Fixnum
readonly
Response status code.
-
#headers ⇒ Hash<String, String>
readonly
Response headers as hash.
-
#message ⇒ String
readonly
Response status message.
-
#request ⇒ Request
readonly
The request that led to this response.
-
#status ⇒ Symbol
readonly
Response status symbol.
Instance Method Summary collapse
-
#decoded_body ⇒ Array, ...
Return decoded body according to content type.
-
#initialize(request, code, headers, body) ⇒ Response
constructor
private
A new instance of Response.
-
#links ⇒ Array<Link>
Return list of links from the Link header.
-
#relations(client) ⇒ Array<Relation>
Return list of relations extracted from links.
-
#success? ⇒ Boolean
Check if response is successful e.g.
-
#url ⇒ Object
Return URL of this response.
Constructor Details
#initialize(request, code, headers, body) ⇒ Response
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Response.
69 70 71 72 73 74 75 76 |
# File 'lib/restify/response.rb', line 69 def initialize(request, code, headers, body) @request = request @code = code @status = STATUS_CODE_TO_SYMBOL[code] @headers = headers @body = body @message = Rack::Utils::HTTP_STATUS_CODES[code] end |
Instance Attribute Details
#body ⇒ String (readonly)
Response body as string.
32 33 34 |
# File 'lib/restify/response.rb', line 32 def body @body end |
#code ⇒ Fixnum (readonly)
Response status code.
44 45 46 |
# File 'lib/restify/response.rb', line 44 def code @code end |
#headers ⇒ Hash<String, String> (readonly)
Response headers as hash.
38 39 40 |
# File 'lib/restify/response.rb', line 38 def headers @headers end |
#message ⇒ String (readonly)
Response status message.
59 60 61 |
# File 'lib/restify/response.rb', line 59 def @message end |
#request ⇒ Request (readonly)
The request that led to this response.
65 66 67 |
# File 'lib/restify/response.rb', line 65 def request @request end |
#status ⇒ Symbol (readonly)
Response status symbol.
53 54 55 |
# File 'lib/restify/response.rb', line 53 def status @status end |
Instance Method Details
#decoded_body ⇒ Array, ...
Return decoded body according to content type. Will return ‘nil` if content cannot be decoded.
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/restify/response.rb', line 122 def decoded_body @decoded_body ||= begin case headers['Content-Type'] when /\Aapplication\/json($|;)/ MultiJson.load body else nil end end end |
#links ⇒ Array<Link>
Return list of links from the Link header.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/restify/response.rb', line 88 def links @links ||= begin if headers['Link'] begin Link.parse(headers['Link']) rescue ArgumentError => e warn e [] end else [] end end end |
#relations(client) ⇒ Array<Relation>
Return list of relations extracted from links.
107 108 109 110 111 112 113 114 115 |
# File 'lib/restify/response.rb', line 107 def relations(client) relations = {} links.each do |link| if (rel = link.['rel']) relations[rel] = Relation.new(client, link.uri) end end relations end |
#success? ⇒ Boolean
Check if response is successful e.g. the status code is on of 2XX.
138 139 140 |
# File 'lib/restify/response.rb', line 138 def success? (200...300) === code end |
#url ⇒ Object
Return URL of this response.
80 81 82 |
# File 'lib/restify/response.rb', line 80 def url request.uri end |