Class: RestClient::AbstractResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/rest-client-1.4.2/lib/restclient/abstract_response.rb

Direct Known Subclasses

RawResponse, Response

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(net_http_res, args) ⇒ AbstractResponse

Returns a new instance of AbstractResponse.



7
8
9
10
# File 'lib/rest-client-1.4.2/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

#argsObject (readonly)

Returns the value of attribute args.



5
6
7
# File 'lib/rest-client-1.4.2/lib/restclient/abstract_response.rb', line 5

def args
  @args
end

#net_http_resObject (readonly)

Returns the value of attribute net_http_res.



5
6
7
# File 'lib/rest-client-1.4.2/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/rest-client-1.4.2/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

#codeObject

HTTP status code



13
14
15
# File 'lib/rest-client-1.4.2/lib/restclient/abstract_response.rb', line 13

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

#cookiesObject

Hash of cookies extracted from response headers



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rest-client-1.4.2/lib/restclient/abstract_response.rb', line 29

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

#follow_redirection(&block) ⇒ Object

Follow a redirection



71
72
73
74
75
76
77
78
# File 'lib/rest-client-1.4.2/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

#headersObject

A hash of the headers, beautified with symbols and underscores. e.g. “Content-type” will become :content_type.



19
20
21
# File 'lib/rest-client-1.4.2/lib/restclient/abstract_response.rb', line 19

def headers
  @headers ||= self.class.beautify_headers(@net_http_res.to_hash)
end

#inspectObject



66
67
68
# File 'lib/rest-client-1.4.2/lib/restclient/abstract_response.rb', line 66

def inspect
  "#{code} #{STATUSES[code]} | #{(headers[:content_type] || '').gsub(/;.*$/, '')} #{size} bytes\n"
end

#raw_headersObject

The raw headers.



24
25
26
# File 'lib/rest-client-1.4.2/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/rest-client-1.4.2/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