Parent

Files

Patron::Response

Represents the response from the HTTP server.

Attributes

url[R]
status[R]
status_line[R]
redirect_count[R]
body[R]
headers[R]
charset[R]

Public Class Methods

new(url, status, redirect_count, header_data, body, default_charset = nil) click to toggle source
    # File lib/patron/response.rb, line 31
31:     def initialize(url, status, redirect_count, header_data, body, default_charset = nil)
32:       # Don't let a response clear out the default charset, which would cause encoding to fail
33:       default_charset = "ASCII-8BIT" unless default_charset
34:       @url            = url
35:       @status         = status
36:       @redirect_count = redirect_count
37:       @body           = body
38:       
39:       @charset        = determine_charset(header_data, body) || default_charset
40:       
41:       [url, header_data].each do |attr|
42:         convert_to_default_encoding!(attr)
43:       end
44:       
45:       parse_headers(header_data)
46:       if @headers["Content-Type"] && @headers["Content-Type"][0, 5] == "text/"
47:         convert_to_default_encoding!(@body)
48:       end
49:     end

Public Instance Methods

inspect() click to toggle source
    # File lib/patron/response.rb, line 53
53:     def inspect
54:       # Avoid spamming the console with the header and body data
55:       "#<Patron::Response @status_line='#{@status_line}'>"
56:     end

Private Instance Methods

charset_regex() click to toggle source
    # File lib/patron/response.rb, line 66
66:     def charset_regex
67:       /(?:charset|encoding)="?([a-z0-9-]+)"?/
68:     end
convert_to_default_encoding!(str) click to toggle source
    # File lib/patron/response.rb, line 70
70:     def convert_to_default_encoding!(str)
71:       if str.respond_to?(:encode) && Encoding.default_internal
72:         str.force_encoding(charset).encode!(Encoding.default_internal)
73:       end
74:     end
determine_charset(header_data, body) click to toggle source
    # File lib/patron/response.rb, line 60
60:     def determine_charset(header_data, body)
61:       header_data.match(charset_regex) || (body && body.match(charset_regex))
62:       
63:       $1
64:     end
parse_headers(header_data) click to toggle source

Called by the C code to parse and set the headers

    # File lib/patron/response.rb, line 77
77:     def parse_headers(header_data)
78:       @headers = {}
79: 
80:       header_data.split(/\r\n/).each do |header|
81:         if header =~ %^HTTP/1.[01]|
82:           @status_line = header.strip
83:         else
84:           parts = header.split(':', 2)
85:           unless parts.empty?
86:             parts[1].strip! unless parts[1].nil?
87:             if @headers.has_key?(parts[0])
88:               @headers[parts[0]] = [@headers[parts[0]]] unless @headers[parts[0]].kind_of? Array
89:               @headers[parts[0]] << parts[1]
90:             else
91:               @headers[parts[0]] = parts[1]
92:             end
93:           end
94:         end
95:       end
96:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.