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.
Class Method Summary collapse
Instance Method Summary collapse
-
#code ⇒ Object
HTTP status code.
-
#cookies ⇒ Object
Hash of cookies extracted from response headers.
- #description ⇒ Object
-
#follow_redirection(&block) ⇒ Object
Follow a redirection.
-
#headers ⇒ Object
A hash of the headers, beautified with symbols and underscores.
-
#raw_headers ⇒ Object
The raw headers.
-
#return!(&block) ⇒ Object
Return the default behavior corresponding to the response code: the response itself for code in 200..206, redirection for 301 and 302 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.
5 6 7 |
# File 'lib/restclient/abstract_response.rb', line 5 def args @args end |
#net_http_res ⇒ Object (readonly)
Returns the value of attribute net_http_res.
5 6 7 |
# File 'lib/restclient/abstract_response.rb', line 5 def net_http_res @net_http_res end |
Class Method Details
.beautify_headers(headers) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/restclient/abstract_response.rb', line 79 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
#code ⇒ Object
HTTP status code
8 9 10 |
# File 'lib/restclient/abstract_response.rb', line 8 def code @code ||= @net_http_res.code.to_i end |
#cookies ⇒ Object
Hash of cookies extracted from response headers
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/restclient/abstract_response.rb', line 24 def @cookies ||= (self.headers[:set_cookie] || []).inject({}) do |out, | # correctly parse comma-separated cookies containing HTTP dates (which also contain a comma) .split(/,\s*/).inject([""]) { |array, blob| blob =~ /expires=.+?$/ ? array.push(blob) : array.last.concat(blob) array }.each do || next if .empty? key, *val = .split(";").first.split("=") out[key] = val.join("=") end out end end |
#description ⇒ Object
65 66 67 |
# File 'lib/restclient/abstract_response.rb', line 65 def description "#{code} #{STATUSES[code]} | #{(headers[:content_type] || '').gsub(/;.*$/, '')} #{size} bytes\n" end |
#follow_redirection(&block) ⇒ Object
Follow a redirection
70 71 72 73 74 75 76 77 |
# File 'lib/restclient/abstract_response.rb', line 70 def follow_redirection &block url = headers[:location] if url !~ /^http/ url = URI.parse(args[:url]).merge(url).to_s end args[:url] = url Request.execute args, &block end |
#headers ⇒ Object
A hash of the headers, beautified with symbols and underscores. e.g. “Content-type” will become :content_type.
14 15 16 |
# File 'lib/restclient/abstract_response.rb', line 14 def headers @headers ||= AbstractResponse.beautify_headers(@net_http_res.to_hash) end |
#raw_headers ⇒ Object
The raw headers.
19 20 21 |
# File 'lib/restclient/abstract_response.rb', line 19 def raw_headers @raw_headers ||= @net_http_res.to_hash end |
#return!(&block) ⇒ Object
Return the default behavior corresponding to the response code: the response itself for code in 200..206, redirection for 301 and 302 in get and head cases, redirection for 303 and an exception in other cases
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/restclient/abstract_response.rb', line 41 def return! &block if (200..206).include? code self elsif [301, 302].include? code unless [:get, :head].include? args[:method] raise Exceptions::EXCEPTIONS_MAP[code], self else follow_redirection &block end elsif code == 303 args[:method] = :get args.delete :payload follow_redirection &block elsif Exceptions::EXCEPTIONS_MAP[code] raise Exceptions::EXCEPTIONS_MAP[code], self else raise RequestFailed, self end end |
#to_i ⇒ Object
61 62 63 |
# File 'lib/restclient/abstract_response.rb', line 61 def to_i code end |