Module: RestClient::AbstractResponse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



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

def args
  @args
end

#net_http_resObject (readonly)

Returns the value of attribute net_http_res.



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

def net_http_res
  @net_http_res
end

Class Method Details

.beautify_headers(headers) ⇒ Object



82
83
84
85
86
87
# File 'lib/restclient/abstract_response.rb', line 82

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



10
11
12
# File 'lib/restclient/abstract_response.rb', line 10

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

#cookiesObject

Hash of cookies extracted from response headers



26
27
28
29
30
31
32
33
34
35
# File 'lib/restclient/abstract_response.rb', line 26

def cookies
  @cookies ||= (self.headers[:set_cookie] || {}).inject({}) do |out, cookie_content|
    CGI::Cookie::parse(cookie_content).each do |key, cookie|
      unless ['expires', 'path'].include? key
        out[CGI::escape(key)] = cookie.value[0] ? (CGI::escape(cookie.value[0]) || '') : ''
      end
    end
    out
  end
end

#descriptionObject



63
64
65
# File 'lib/restclient/abstract_response.rb', line 63

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

#follow_redirection(request = nil, &block) ⇒ Object

Follow a redirection



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/restclient/abstract_response.rb', line 68

def follow_redirection request = nil, &block
  url = headers[:location]
  if url !~ /^http/
    url = URI.parse(args[:url]).merge(url).to_s
  end
  args[:url] = url
  if request
    args[:password] = request.password
    args[:user] = request.user
    args[:headers] = request.headers
  end
  Request.execute args, &block
end

#headersObject

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



16
17
18
# File 'lib/restclient/abstract_response.rb', line 16

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

#raw_headersObject

The raw headers.



21
22
23
# File 'lib/restclient/abstract_response.rb', line 21

def raw_headers
  @raw_headers ||= @net_http_res.to_hash
end

#return!(request = nil, &block) ⇒ Object

Return the default behavior corresponding to the response code: the response itself for code in 200..206, redirection for 301, 302 and 307 in get and head cases, redirection for 303 and an exception in other cases



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/restclient/abstract_response.rb', line 39

def return! request  = nil, &block
  if (200..207).include? code
    self
  elsif [301, 302, 307].include? code
    unless [:get, :head].include? args[:method]
      raise Exceptions::EXCEPTIONS_MAP[code], self
    else
      follow_redirection(request, &block)
    end
  elsif code == 303
    args[:method] = :get
    args.delete :payload
    follow_redirection(request, &block)
  elsif Exceptions::EXCEPTIONS_MAP[code]
    raise Exceptions::EXCEPTIONS_MAP[code], self
  else
    raise RequestFailed, self
  end
end

#to_iObject



59
60
61
# File 'lib/restclient/abstract_response.rb', line 59

def to_i
  code
end