Module: RestClient::AbstractResponse
- Included in:
- RawResponse, Response
- Defined in:
- lib/restclient/abstract_response.rb
Instance Attribute Summary (collapse)
-
- (Object) args
readonly
Returns the value of attribute args.
-
- (Object) net_http_res
readonly
Returns the value of attribute net_http_res.
Class Method Summary (collapse)
Instance Method Summary (collapse)
-
- (Object) code
HTTP status code.
-
- (Object) cookies
Hash of cookies extracted from response headers.
- - (Object) description
-
- (Object) follow_redirection(request = nil, result = nil, &block)
Follow a redirection.
-
- (Object) headers
A hash of the headers, beautified with symbols and underscores.
-
- (Object) raw_headers
The raw headers.
-
- (Object) return!(request = nil, result = nil, &block)
Return the default behavior corresponding to the response code: the response itself for code in 200..206, redirection for 301, 302 and 307 in get and head cases, redirection for 303 and an exception in other cases.
- - (Object) to_i
Instance Attribute Details
- (Object) args (readonly)
Returns the value of attribute args
7 8 9 |
# File 'lib/restclient/abstract_response.rb', line 7 def args @args end |
- (Object) net_http_res (readonly)
Returns the value of attribute net_http_res
7 8 9 |
# File 'lib/restclient/abstract_response.rb', line 7 def net_http_res @net_http_res end |
Class Method Details
+ (Object) beautify_headers(headers)
85 86 87 88 89 90 |
# File 'lib/restclient/abstract_response.rb', line 85 def AbstractResponse.beautify_headers(headers) headers.inject({}) do |out, (key, value)| out[key.gsub(/-/, '_').downcase.to_sym] = %w{ set-cookie }.include?(key.downcase) ? value : value.first out end end |
Instance Method Details
- (Object) code
HTTP status code
10 11 12 |
# File 'lib/restclient/abstract_response.rb', line 10 def code @code ||= @net_http_res.code.to_i end |
- (Object) cookies
Hash of cookies extracted from response headers
26 27 28 29 30 |
# File 'lib/restclient/abstract_response.rb', line 26 def @cookies ||= (self.headers[:set_cookie] || {}).inject({}) do |out, | out.merge () end end |
- (Object) description
58 59 60 |
# File 'lib/restclient/abstract_response.rb', line 58 def description "#{code} #{STATUSES[code]} | #{(headers[:content_type] || '').gsub(/;.*$/, '')} #{size} bytes\n" end |
- (Object) follow_redirection(request = nil, result = nil, &block)
Follow a redirection
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/restclient/abstract_response.rb', line 63 def follow_redirection request = nil, result = nil, & block url = headers[:location] if url !~ /^http/ url = URI.parse(args[:url]).merge(url).to_s end args[:url] = url if request if request.max_redirects == 0 raise MaxRedirectsReached end args[:password] = request.password args[:user] = request.user args[:headers] = request.headers args[:max_redirects] = request.max_redirects - 1 # pass any cookie set in the result if result && result['set-cookie'] args[:headers][:cookies] = (args[:headers][:cookies] || {}).merge((result['set-cookie'])) end end Request.execute args, &block end |
- (Object) headers
A hash of the headers, beautified with symbols and underscores. e.g. “Content-type” will become :content_type.
16 17 18 |
# File 'lib/restclient/abstract_response.rb', line 16 def headers @headers ||= AbstractResponse.beautify_headers(@net_http_res.to_hash) end |
- (Object) raw_headers
The raw headers.
21 22 23 |
# File 'lib/restclient/abstract_response.rb', line 21 def raw_headers @raw_headers ||= @net_http_res.to_hash end |
- (Object) return!(request = nil, result = nil, &block)
Return the default behavior corresponding to the response code: the response itself for code in 200..206, redirection for 301, 302 and 307 in get and head cases, redirection for 303 and an exception in other cases
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/restclient/abstract_response.rb', line 34 def return! request = nil, result = nil, & block if (200..207).include? code self elsif [301, 302, 307].include? code unless [:get, :head].include? args[:method] raise Exceptions::EXCEPTIONS_MAP[code].new(self, code) else follow_redirection(request, result, & block) end elsif code == 303 args[:method] = :get args.delete :payload follow_redirection(request, result, & block) elsif Exceptions::EXCEPTIONS_MAP[code] raise Exceptions::EXCEPTIONS_MAP[code].new(self, code) else raise RequestFailed.new(self, code) end end |
- (Object) to_i
54 55 56 |
# File 'lib/restclient/abstract_response.rb', line 54 def to_i code end |