Module: RestClient::Mixin::Response
- Included in:
- RawResponse, Response
- Defined in:
- lib/restclient/mixin/response.rb
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#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.
-
#headers ⇒ Object
A hash of the headers, beautified with symbols and underscores.
-
#raw_headers ⇒ Object
The raw headers.
-
#return! ⇒ Object
Return the default behavior corresponding to the response code: the response itself for code in 200..206 and an exception in other cases.
Instance Attribute Details
#net_http_res ⇒ Object (readonly)
Returns the value of attribute net_http_res.
4 5 6 |
# File 'lib/restclient/mixin/response.rb', line 4 def net_http_res @net_http_res end |
Class Method Details
.included(receiver) ⇒ Object
50 51 52 |
# File 'lib/restclient/mixin/response.rb', line 50 def self.included(receiver) receiver.extend(RestClient::Mixin::Response::ClassMethods) end |
Instance Method Details
#code ⇒ Object
HTTP status code
7 8 9 |
# File 'lib/restclient/mixin/response.rb', line 7 def code @code ||= @net_http_res.code.to_i end |
#cookies ⇒ Object
Hash of cookies extracted from response headers
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/restclient/mixin/response.rb', line 23 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 |
#headers ⇒ Object
A hash of the headers, beautified with symbols and underscores. e.g. “Content-type” will become :content_type.
13 14 15 |
# File 'lib/restclient/mixin/response.rb', line 13 def headers @headers ||= self.class.beautify_headers(@net_http_res.to_hash) end |
#raw_headers ⇒ Object
The raw headers.
18 19 20 |
# File 'lib/restclient/mixin/response.rb', line 18 def raw_headers @raw_headers ||= @net_http_res.to_hash end |
#return! ⇒ Object
Return the default behavior corresponding to the response code: the response itself for code in 200..206 and an exception in other cases
40 41 42 43 44 45 46 47 48 |
# File 'lib/restclient/mixin/response.rb', line 40 def return! if (200..206).include? code self elsif Exceptions::EXCEPTIONS_MAP[code] raise Exceptions::EXCEPTIONS_MAP[code], self else raise RequestFailed, self end end |