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.
-
#uri ⇒ Addressable::URI
readonly
Last effective URI.
Instance Method Summary collapse
-
#content_type ⇒ String
Return content type header from response headers.
- #decoded_body ⇒ Object private
-
#errored? ⇒ Boolean
Check if response is erroneous e.g.
- #follow_location ⇒ Object private
-
#initialize(request, uri, code, headers, body) ⇒ Response
constructor
private
A new instance of Response.
-
#links ⇒ Array<Link>
Return list of links from the Link header.
-
#success? ⇒ Boolean
Check if response is successful e.g.
Constructor Details
#initialize(request, uri, 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.
78 79 80 81 82 83 84 85 86 |
# File 'lib/restify/response.rb', line 78 def initialize(request, uri, code, headers, body) @request = request @uri = uri @code = code @status = STATUS_CODE_TO_SYMBOL[code] @headers = convert_headers(headers) @body = body @message = Rack::Utils::HTTP_STATUS_CODES[code] end |
Instance Attribute Details
#body ⇒ String (readonly)
Response body as string.
35 36 37 |
# File 'lib/restify/response.rb', line 35 def body @body end |
#code ⇒ Fixnum (readonly)
Response status code.
47 48 49 |
# File 'lib/restify/response.rb', line 47 def code @code end |
#headers ⇒ Hash<String, String> (readonly)
Response headers as hash.
41 42 43 |
# File 'lib/restify/response.rb', line 41 def headers @headers end |
#message ⇒ String (readonly)
Response status message.
62 63 64 |
# File 'lib/restify/response.rb', line 62 def @message end |
#request ⇒ Request (readonly)
The request that led to this response.
68 69 70 |
# File 'lib/restify/response.rb', line 68 def request @request end |
#status ⇒ Symbol (readonly)
Response status symbol.
56 57 58 |
# File 'lib/restify/response.rb', line 56 def status @status end |
#uri ⇒ Addressable::URI (readonly)
Last effective URI.
74 75 76 |
# File 'lib/restify/response.rb', line 74 def uri @uri end |
Instance Method Details
#content_type ⇒ String
Return content type header from response headers.
113 114 115 |
# File 'lib/restify/response.rb', line 113 def content_type headers['CONTENT_TYPE'] end |
#decoded_body ⇒ Object
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.
136 137 138 139 140 141 142 143 |
# File 'lib/restify/response.rb', line 136 def decoded_body @decoded_body ||= begin case content_type when %r{\Aapplication/json($|;)} ::JSON.parse(body) end end end |
#errored? ⇒ Boolean
Check if response is erroneous e.g. the status code is one of 4XX or 5XX.
131 132 133 |
# File 'lib/restify/response.rb', line 131 def errored? (400...600).cover? code end |
#follow_location ⇒ Object
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.
146 147 148 |
# File 'lib/restify/response.rb', line 146 def follow_location headers['LOCATION'] || headers['CONTENT_LOCATION'] end |
#links ⇒ Array<Link>
Return list of links from the Link header.
rubocop:disable Metrics/MethodLength
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/restify/response.rb', line 93 def links @links ||= begin if headers['LINK'] begin Link.parse(headers['LINK']) rescue ArgumentError => e warn e [] end else [] end end end |
#success? ⇒ Boolean
Check if response is successful e.g. the status code is on of 2XX.
122 123 124 |
# File 'lib/restify/response.rb', line 122 def success? (200...300).cover? code end |