Module: RestClient::Mixin::Response

Included in:
RawResponse, Response
Defined in:
lib/restclient/mixin/response.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#net_http_resObject (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

#codeObject

HTTP status code


7
8
9
# File 'lib/restclient/mixin/response.rb', line 7

def code
  @code ||= @net_http_res.code.to_i
end

#cookiesObject

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
  @cookies ||= (self.headers[:set_cookie] || []).inject({}) do |out, cookie_content|
    # correctly parse comma-separated cookies containing HTTP dates (which also contain a comma)
    cookie_content.split(/,\s*/).inject([""]) { |array, blob| 
      blob =~ /expires=.+?$/ ? array.push(blob) : array.last.concat(blob)
      array
    }.each do |cookie|
      next if cookie.empty?
      key, *val = cookie.split(";").first.split("=")
      out[key] = val.join("=")
    end
    out
  end
end

#headersObject

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_headersObject

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