Class: RestClient::AbstractResponse
- Inherits:
-
Object
- Object
- RestClient::AbstractResponse
- Defined in:
- lib/restclient/abstract_response.rb
Direct Known Subclasses
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.
-
#follow_redirection(&block) ⇒ Object
Follow a redirection.
-
#headers ⇒ Object
A hash of the headers, beautified with symbols and underscores.
-
#initialize(net_http_res, args) ⇒ AbstractResponse
constructor
A new instance of AbstractResponse.
- #inspect ⇒ Object
-
#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.
Constructor Details
#initialize(net_http_res, args) ⇒ AbstractResponse
Returns a new instance of AbstractResponse.
7 8 9 10 |
# File 'lib/restclient/abstract_response.rb', line 7 def initialize net_http_res, args @net_http_res = net_http_res @args = args end |
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
80 81 82 83 84 85 |
# File 'lib/restclient/abstract_response.rb', line 80 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
13 14 15 |
# File 'lib/restclient/abstract_response.rb', line 13 def code @code ||= @net_http_res.code.to_i end |
#cookies ⇒ Object
Hash of cookies extracted from response headers
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/restclient/abstract_response.rb', line 29 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 |
#follow_redirection(&block) ⇒ Object
Follow a redirection
71 72 73 74 75 76 77 78 |
# File 'lib/restclient/abstract_response.rb', line 71 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.
19 20 21 |
# File 'lib/restclient/abstract_response.rb', line 19 def headers @headers ||= self.class.beautify_headers(@net_http_res.to_hash) end |
#inspect ⇒ Object
66 67 68 |
# File 'lib/restclient/abstract_response.rb', line 66 def inspect "#{code} #{STATUSES[code]} | #{(headers[:content_type] || '').gsub(/;.*$/, '')} #{size} bytes\n" end |
#raw_headers ⇒ Object
The raw headers.
24 25 26 |
# File 'lib/restclient/abstract_response.rb', line 24 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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/restclient/abstract_response.rb', line 46 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 |