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, or nil 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 ⇒ Fixnum
readonly
How many redirects were followed when fulfilling this request.
-
#status ⇒ Fixnum
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
-
#error? ⇒ Boolean
Tells whether the HTTP response code is larger than 399.
-
#initialize(url, status, redirect_count, 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.
-
#ok? ⇒ Boolean
Tells whether the HTTP response code is less than 400.
Constructor Details
#initialize(url, status, redirect_count, header_data, body, default_charset = nil) ⇒ Response
Returns a new instance of Response.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/patron/response.rb', line 59 def initialize(url, status, redirect_count, header_data, body, default_charset = nil) # Don't let a response clear out the default charset, which would cause encoding to fail default_charset = "ASCII-8BIT" unless default_charset @url = url @status = status @redirect_count = redirect_count @body = body @charset = determine_charset(header_data, body) || default_charset [url, header_data].each do |attr| convert_to_default_encoding!(attr) end parse_headers(header_data) if @headers["Content-Type"] && @headers["Content-Type"][0, 5] == "text/" convert_to_default_encoding!(@body) end end |
Instance Attribute Details
#body ⇒ String? (readonly)
Returns the response body, or nil if the response was written directly to a file.
44 45 46 |
# File 'lib/patron/response.rb', line 44 def body @body end |
#charset ⇒ String (readonly)
Returns the recognized name of the charset for the response.
51 52 53 |
# File 'lib/patron/response.rb', line 51 def charset @charset end |
#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.
48 49 50 |
# File 'lib/patron/response.rb', line 48 def headers @headers end |
#redirect_count ⇒ Fixnum (readonly)
Returns how many redirects were followed when fulfilling this request.
41 42 43 |
# File 'lib/patron/response.rb', line 41 def redirect_count @redirect_count end |
#status ⇒ Fixnum (readonly)
Returns the HTTP status code of the final response after all the redirects.
35 36 37 |
# File 'lib/patron/response.rb', line 35 def status @status end |
#status_line ⇒ String (readonly)
Returns the complete status line (code and message).
38 39 40 |
# File 'lib/patron/response.rb', line 38 def status_line @status_line end |
#url ⇒ String (readonly)
Returns the original URL used to perform the request (contains the final URL after redirects).
32 33 34 |
# File 'lib/patron/response.rb', line 32 def url @url end |
Instance Method Details
#error? ⇒ Boolean
Tells whether the HTTP response code is larger than 399
89 90 91 |
# File 'lib/patron/response.rb', line 89 def error? status >= 400 end |
#inspect ⇒ Object
Overridden so that the output is shorter and there is no response body printed
54 55 56 57 |
# File 'lib/patron/response.rb', line 54 def inspect # Avoid spamming the console with the header and body data "#<Patron::Response @status_line='#{@status_line}'>" end |
#ok? ⇒ Boolean
Tells whether the HTTP response code is less than 400
82 83 84 |
# File 'lib/patron/response.rb', line 82 def ok? !error? end |