Module: RestClient::AbstractResponse
- Included in:
- RawResponse, Response
- Defined in:
- lib/restclient/abstract_response.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#net_http_res ⇒ Object
readonly
Returns the value of attribute net_http_res.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Class Method Summary collapse
Instance Method Summary collapse
-
#code ⇒ Object
HTTP status code.
-
#cookie_jar ⇒ HTTP::CookieJar
Cookie jar extracted from response headers.
-
#cookies ⇒ Object
Hash of cookies extracted from response headers.
- #description ⇒ Object
-
#follow_redirection(request = nil, result = nil, &block) ⇒ Object
Follow a redirection.
-
#headers ⇒ Object
A hash of the headers, beautified with symbols and underscores.
-
#raw_headers ⇒ Object
The raw headers.
- #response_set_vars(net_http_res, args, request) ⇒ Object
-
#return!(request = nil, result = nil, &block) ⇒ Object
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.
- #to_i ⇒ Object
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
8 9 10 |
# File 'lib/restclient/abstract_response.rb', line 8 def args @args end |
#net_http_res ⇒ Object (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 |
#request ⇒ Object (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) ⇒ Object
114 115 116 117 118 119 |
# File 'lib/restclient/abstract_response.rb', line 114 def self.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
#code ⇒ Object
HTTP status code
11 12 13 |
# File 'lib/restclient/abstract_response.rb', line 11 def code @code ||= @net_http_res.code.to_i end |
#cookie_jar ⇒ HTTP::CookieJar
Cookie jar extracted from response headers.
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/restclient/abstract_response.rb', line 47 def return @cookie_jar if @cookie_jar jar = HTTP::CookieJar.new headers.fetch(:set_cookie, []).each do || jar.parse(, @request.url) end @cookie_jar = jar end |
#cookies ⇒ Object
Hash of cookies extracted from response headers
33 34 35 36 37 38 39 40 41 |
# File 'lib/restclient/abstract_response.rb', line 33 def hash = {} ..each do || hash[.name] = .value end hash end |
#description ⇒ Object
84 85 86 |
# File 'lib/restclient/abstract_response.rb', line 84 def description "#{code} #{STATUSES[code]} | #{(headers[:content_type] || '').gsub(/;.*$/, '')} #{size} bytes\n" end |
#follow_redirection(request = nil, result = nil, &block) ⇒ Object
Follow a redirection
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/restclient/abstract_response.rb', line 89 def follow_redirection request = nil, result = nil, & block new_args = @args.dup url = headers[:location] if url !~ /^http/ url = URI.parse(request.url).merge(url).to_s end new_args[:url] = url if request if request.max_redirects == 0 raise MaxRedirectsReached end new_args[:password] = request.password new_args[:user] = request.user new_args[:headers] = request.headers new_args[:max_redirects] = request.max_redirects - 1 # TODO: figure out what to do with original :cookie, :cookies values new_args[:headers]['Cookie'] = HTTP::Cookie.( .(new_args.fetch(:url))) end Request.execute(new_args, &block) end |
#headers ⇒ Object
A hash of the headers, beautified with symbols and underscores. e.g. “Content-type” will become :content_type.
17 18 19 |
# File 'lib/restclient/abstract_response.rb', line 17 def headers @headers ||= AbstractResponse.beautify_headers(@net_http_res.to_hash) end |
#raw_headers ⇒ Object
The raw headers.
22 23 24 |
# File 'lib/restclient/abstract_response.rb', line 22 def raw_headers @raw_headers ||= @net_http_res.to_hash end |
#response_set_vars(net_http_res, args, request) ⇒ Object
26 27 28 29 30 |
# File 'lib/restclient/abstract_response.rb', line 26 def response_set_vars(net_http_res, args, request) @net_http_res = net_http_res @args = args @request = request end |
#return!(request = nil, result = nil, &block) ⇒ Object
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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/restclient/abstract_response.rb', line 60 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 |
#to_i ⇒ Object
80 81 82 |
# File 'lib/restclient/abstract_response.rb', line 80 def to_i code end |