Class: Patron::Response

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

Overview

Represents the response from the HTTP server.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bodyString? (readonly)

Returns the response body, or nil if the response was written directly to a file.

Returns:

  • (String, nil)

    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

#charsetString (readonly)

Returns the recognized name of the charset for the response.

Returns:

  • (String)

    the recognized name of the charset for the response



51
52
53
# File 'lib/patron/response.rb', line 51

def charset
  @charset
end

#headersHash (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.

Returns:

  • (Hash)

    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_countFixnum (readonly)

Returns how many redirects were followed when fulfilling this request.

Returns:

  • (Fixnum)

    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

#statusFixnum (readonly)

Returns the HTTP status code of the final response after all the redirects.

Returns:

  • (Fixnum)

    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_lineString (readonly)

Returns the complete status line (code and message).

Returns:

  • (String)

    the complete status line (code and message)



38
39
40
# File 'lib/patron/response.rb', line 38

def status_line
  @status_line
end

#urlString (readonly)

Returns the original URL used to perform the request (contains the final URL after redirects).

Returns:

  • (String)

    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

Returns:

  • (Boolean)


89
90
91
# File 'lib/patron/response.rb', line 89

def error?
  status >= 400
end

#inspectObject

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

Returns:

  • (Boolean)


82
83
84
# File 'lib/patron/response.rb', line 82

def ok?
  !error?
end