Module: RestClient::AbstractResponse

Included in:
RawResponse, Response
Defined in:
lib/restclient/abstract_response.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#net_http_resObject (readonly)

Returns the value of attribute net_http_res.



8
9
10
# File 'lib/restclient/abstract_response.rb', line 8

def net_http_res
  @net_http_res
end

#requestObject (readonly)

Returns the value of attribute request.



8
9
10
# File 'lib/restclient/abstract_response.rb', line 8

def request
  @request
end

Class Method Details

.beautify_headers(headers) ⇒ Hash

Convert headers hash into canonical form.

Header names will be converted to lowercase symbols with underscores instead of hyphens.

Headers specified multiple times will be joined by comma and space, except for Set-Cookie, which will always be an array.

Per RFC 2616, if a server sends multiple headers with the same key, they MUST be able to be joined into a single header by a comma. However, Set-Cookie (RFC 6265) cannot because commas are valid within cookie definitions. The newer RFC 7230 notes (3.2.2) that Set-Cookie should be handled as a special case.

tools.ietf.org/html/rfc2616#section-4.2 tools.ietf.org/html/rfc7230#section-3.2.2 tools.ietf.org/html/rfc6265

Parameters:

  • headers (Hash)

Returns:

  • (Hash)


153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/restclient/abstract_response.rb', line 153

def self.beautify_headers(headers)
  headers.inject({}) do |out, (key, value)|
    key_sym = key.tr('-', '_').downcase.to_sym

    # Handle Set-Cookie specially since it cannot be joined by comma.
    if key.downcase == 'set-cookie'
      out[key_sym] = value
    else
      out[key_sym] = value.join(', ')
    end

    out
  end
end

Instance Method Details

#codeObject

HTTP status code



15
16
17
# File 'lib/restclient/abstract_response.rb', line 15

def code
  @code ||= @net_http_res.code.to_i
end

Cookie jar extracted from response headers.

Returns:

  • (HTTP::CookieJar)


66
67
68
69
70
71
72
73
74
75
# File 'lib/restclient/abstract_response.rb', line 66

def cookie_jar
  return @cookie_jar if defined?(@cookie_jar) && @cookie_jar

  jar = @request.cookie_jar.dup
  headers.fetch(:set_cookie, []).each do |cookie|
    jar.parse(cookie, @request.uri)
  end

  @cookie_jar = jar
end

#cookiesHash

Hash of cookies extracted from response headers.

NB: This will return only cookies whose domain matches this request, and may not even return all of those cookies if there are duplicate names. Use the full cookie_jar for more nuanced access.

Returns:

  • (Hash)

See Also:



52
53
54
55
56
57
58
59
60
# File 'lib/restclient/abstract_response.rb', line 52

def cookies
  hash = {}

  cookie_jar.cookies(@request.uri).each do |cookie|
    hash[cookie.name] = cookie.value
  end

  hash
end

#descriptionObject



112
113
114
# File 'lib/restclient/abstract_response.rb', line 112

def description
  "#{code} #{STATUSES[code]} | #{(headers[:content_type] || '').gsub(/;.*$/, '')} #{size} bytes\n"
end

#follow_get_redirection(&block) ⇒ Object

Follow a redirection response, but change the HTTP method to GET and drop the payload from the original request.



124
125
126
127
128
129
130
# File 'lib/restclient/abstract_response.rb', line 124

def follow_get_redirection(&block)
  new_args = request.args.dup
  new_args[:method] = :get
  new_args.delete(:payload)

  (new_args, &block)
end

#follow_redirection(&block) ⇒ Object

Follow a redirection response by making a new HTTP request to the redirection target.



118
119
120
# File 'lib/restclient/abstract_response.rb', line 118

def follow_redirection(&block)
  (request.args.dup, &block)
end

#headersObject

A hash of the headers, beautified with symbols and underscores. e.g. “Content-type” will become :content_type.



25
26
27
# File 'lib/restclient/abstract_response.rb', line 25

def headers
  @headers ||= AbstractResponse.beautify_headers(@net_http_res.to_hash)
end

#historyObject



19
20
21
# File 'lib/restclient/abstract_response.rb', line 19

def history
  @history ||= request.redirection_history || []
end

#inspectObject

Raises:

  • (NotImplementedError)


10
11
12
# File 'lib/restclient/abstract_response.rb', line 10

def inspect
  raise NotImplementedError.new('must override in subclass')
end

#raw_headersObject

The raw headers.



30
31
32
# File 'lib/restclient/abstract_response.rb', line 30

def raw_headers
  @raw_headers ||= @net_http_res.to_hash
end

#response_set_vars(net_http_res, request) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/restclient/abstract_response.rb', line 34

def response_set_vars(net_http_res, request)
  @net_http_res = net_http_res
  @request = request

  # prime redirection history
  history
end

#return!(&block) ⇒ Object

Return the default behavior corresponding to the response code:

For 20x status codes: return the response itself

For 30x status codes:

301, 302, 307: redirect GET / HEAD if there is a Location header
303: redirect, changing method to GET, if there is a Location header

For all other responses, raise a response exception



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/restclient/abstract_response.rb', line 87

def return!(&block)
  case code
  when 200..207
    self
  when 301, 302, 307
    case request.method
    when 'get', 'head'
      check_max_redirects
      follow_redirection(&block)
    else
      raise exception_with_response
    end
  when 303
    check_max_redirects
    follow_get_redirection(&block)
  else
    raise exception_with_response
  end
end

#to_iObject



107
108
109
110
# File 'lib/restclient/abstract_response.rb', line 107

def to_i
  warn('warning: calling Response#to_i is not recommended')
  super
end