Class: Patron::Response
- Inherits:
-
Object
- Object
- Patron::Response
- Defined in:
- lib/patron/response.rb
Overview
Represents the response from the HTTP server.
Instance Attribute Summary collapse
-
#body ⇒ String?
readonly
The response body as a String encoded as
Encoding::BINARY
or ornil
if the response was written directly to a file. -
#charset ⇒ String
readonly
The recognized name of the charset for the response.
-
#headers ⇒ Hash
readonly
The response headers.
-
#redirect_count ⇒ Integer
readonly
How many redirects were followed when fulfilling this request.
-
#status ⇒ Integer
readonly
The HTTP status code of the final response after all the redirects.
-
#status_line ⇒ String
readonly
The complete status line (code and message).
-
#url ⇒ String
readonly
The original URL used to perform the request (contains the final URL after redirects).
Instance Method Summary collapse
-
#body_decodable? ⇒ Boolean
Tells whether the response body can be decoded losslessly into the curren internal encoding.
-
#decoded_body ⇒ String?
Returns the response body converted into the Ruby process internal encoding (the one set as
Encoding.default_internal
). -
#error? ⇒ Boolean
Tells whether the HTTP response code is larger than 399.
-
#initialize(url, status, redirect_count, raw_header_data, body, default_charset = nil) ⇒ Response
constructor
A new instance of Response.
-
#inspect ⇒ Object
Overridden so that the output is shorter and there is no response body printed.
-
#inspectable_body ⇒ String?
Works the same as
decoded_body
, with one substantial difference: characters which can't be represented in your process' default encoding are going to be replaced with question marks. -
#ok? ⇒ Boolean
Tells whether the HTTP response code is less than 400.
Constructor Details
permalink #initialize(url, status, redirect_count, raw_header_data, body, default_charset = nil) ⇒ Response
Returns a new instance of Response.
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/patron/response.rb', line 37 def initialize(url, status, redirect_count, raw_header_data, body, default_charset = nil) @url = url.force_encoding(Encoding::ASCII) # the URL is always an ASCII subset, _always_. @status = status @redirect_count = redirect_count @body = body.force_encoding(Encoding::BINARY) if body header_data = decode_header_data(raw_header_data) parse_headers(header_data) @charset = charset_from_content_type end |
Instance Attribute Details
permalink #body ⇒ String? (readonly)
Returns the response body as a String encoded as Encoding::BINARY
or
or nil
if the response was written directly to a file.
21 22 23 |
# File 'lib/patron/response.rb', line 21 def body @body end |
permalink #charset ⇒ String (readonly)
Returns the recognized name of the charset for the response. The name is not checked to be a valid charset name, just stored. To check the charset for validity, use #body_decodable?.
29 30 31 |
# File 'lib/patron/response.rb', line 29 def charset @charset end |
permalink #headers ⇒ Hash (readonly)
Returns the response headers. If there were multiple headers received for the same value (like "Cookie"), the header values will be within an Array under the key for the header, in order.
25 26 27 |
# File 'lib/patron/response.rb', line 25 def headers @headers end |
permalink #redirect_count ⇒ Integer (readonly)
Returns how many redirects were followed when fulfilling this request.
17 18 19 |
# File 'lib/patron/response.rb', line 17 def redirect_count @redirect_count end |
permalink #status ⇒ Integer (readonly)
Returns the HTTP status code of the final response after all the redirects.
11 12 13 |
# File 'lib/patron/response.rb', line 11 def status @status end |
permalink #status_line ⇒ String (readonly)
Returns the complete status line (code and message).
14 15 16 |
# File 'lib/patron/response.rb', line 14 def status_line @status_line end |
permalink #url ⇒ String (readonly)
Returns the original URL used to perform the request (contains the final URL after redirects).
8 9 10 |
# File 'lib/patron/response.rb', line 8 def url @url end |
Instance Method Details
permalink #body_decodable? ⇒ Boolean
Tells whether the response body can be decoded losslessly into the curren internal encoding
65 66 67 68 69 70 |
# File 'lib/patron/response.rb', line 65 def body_decodable? return true if @body.nil? return true if decoded_body rescue HeaderCharsetInvalid, NonRepresentableBody false end |
permalink #decoded_body ⇒ String?
Returns the response body converted into the Ruby process internal encoding (the one set as Encoding.default_internal
).
As the response gets returned, the response body is not assumed to be in any encoding whatsoever - it will be explicitly
set to Encoding::BINARY
(as if you were reading a file in binary mode).
When you call decoded_body
, the method will
look at the Content-Type
response header, and check if that header specified a charset. If it did, the method will then
check whether the specified charset is valid (whether it is possible to find a matching Encoding
class in the VM).
Once that succeeds, the method will check whether the response body is in the encoding that the server said it is.
This might not be the case - you can, for instance, easily serve an HTML document with a UTF-8 header (with the header
being configured somewhere on the webserver level) and then have the actual HTML document override it with a
meta
element or charset
containing an overriding charset. However, parsing the response body is outside of scope for
Patron, so if this situation happens (the server sets a charset in the header but this header does not match what the server
actually sends in the body) you will get an exception stating this is a problem.
The next step is actually converting the body to the internal Ruby encoding. That stage may raise an exception as well, if you are using an internal encoding which can't represent the response body faithfully. For example, if you run Ruby with a CJK internal encoding, and the response you are trying to decode uses Greek characters and is UTF-8, you are going to get an exception since it is impossible to coerce those characters to your internal encoding.
95 96 97 98 |
# File 'lib/patron/response.rb', line 95 def decoded_body return unless @body @decoded_body ||= decode_body(true) end |
permalink #error? ⇒ Boolean
Tells whether the HTTP response code is larger than 399
58 59 60 |
# File 'lib/patron/response.rb', line 58 def error? status >= 400 end |
permalink #inspect ⇒ Object
Overridden so that the output is shorter and there is no response body printed
32 33 34 35 |
# File 'lib/patron/response.rb', line 32 def inspect # Avoid spamming the console with the header and body data "#<Patron::Response @status_line='#{@status_line}'>" end |
permalink #inspectable_body ⇒ String?
Works the same as decoded_body
, with one substantial difference: characters which can't be represented
in your process' default encoding are going to be replaced with question marks. This can be used for raising
errors when you receive responses which indicate errors on the server you are calling. For example, if you expect
a binary download, and the server sends you an error message and you don't really want to bother figuring out
the encoding it has - but you need to append this response to an error log or similar.
108 109 110 111 |
# File 'lib/patron/response.rb', line 108 def inspectable_body return unless @body @inspectable_body ||= decode_body(false) end |
permalink #ok? ⇒ Boolean
Tells whether the HTTP response code is less than 400
51 52 53 |
# File 'lib/patron/response.rb', line 51 def ok? !error? end |