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

#durationObject (readonly)

Returns the value of attribute duration


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

def duration
  @duration
end

#end_timeObject (readonly)

Returns the value of attribute end_time


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

def end_time
  @end_time
end

#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

#start_timeObject (readonly)

Returns the value of attribute start_time


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

def start_time
  @start_time
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)

179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/restclient/abstract_response.rb', line 179

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


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

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

Cookie jar extracted from response headers.

Returns:

  • (HTTP::CookieJar)

92
93
94
95
96
97
98
99
100
101
# File 'lib/restclient/abstract_response.rb', line 92

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:


78
79
80
81
82
83
84
85
86
# File 'lib/restclient/abstract_response.rb', line 78

def cookies
  hash = {}

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

  hash
end

#descriptionObject


138
139
140
# File 'lib/restclient/abstract_response.rb', line 138

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.


150
151
152
153
154
155
156
# File 'lib/restclient/abstract_response.rb', line 150

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.


144
145
146
# File 'lib/restclient/abstract_response.rb', line 144

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.


40
41
42
# File 'lib/restclient/abstract_response.rb', line 40

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

#historyObject


34
35
36
# File 'lib/restclient/abstract_response.rb', line 34

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

#logObject

Logger from the request, potentially nil.


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

def log
  request.log
end

#log_responseObject


19
20
21
22
23
24
25
26
27
# File 'lib/restclient/abstract_response.rb', line 19

def log_response
  return unless log

  code = net_http_res.code
  res_name = net_http_res.class.to_s.gsub(/\ANet::HTTP/, '')
  content_type = (net_http_res['Content-type'] || '').gsub(/;.*\z/, '')

  log << "# => #{code} #{res_name} | #{content_type} #{size} bytes, #{sprintf('%.2f', duration)}s\n"
end

#raw_headersObject

The raw headers.


45
46
47
# File 'lib/restclient/abstract_response.rb', line 45

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

#response_set_vars(net_http_res, request, start_time) ⇒ Object

Parameters:


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/restclient/abstract_response.rb', line 52

def response_set_vars(net_http_res, request, start_time)
  @net_http_res = net_http_res
  @request = request
  @start_time = start_time
  @end_time = Time.now

  if @start_time
    @duration = @end_time - @start_time
  else
    @duration = nil
  end

  # 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


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/restclient/abstract_response.rb', line 113

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


133
134
135
136
# File 'lib/restclient/abstract_response.rb', line 133

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