Module RestClient::AbstractResponse
In: lib/rest-client-1.6.3/lib/restclient/abstract_response.rb

Methods

Attributes

args  [R] 
net_http_res  [R] 

Public Class methods

[Source]

# File lib/rest-client-1.6.3/lib/restclient/abstract_response.rb, line 85
    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

Public Instance methods

HTTP status code

[Source]

# File lib/rest-client-1.6.3/lib/restclient/abstract_response.rb, line 10
    def code
      @code ||= @net_http_res.code.to_i
    end

Hash of cookies extracted from response headers

[Source]

# File lib/rest-client-1.6.3/lib/restclient/abstract_response.rb, line 26
    def cookies
      @cookies ||= (self.headers[:set_cookie] || {}).inject({}) do |out, cookie_content|
        out.merge parse_cookie(cookie_content)
      end
    end

[Source]

# File lib/rest-client-1.6.3/lib/restclient/abstract_response.rb, line 58
    def description
      "#{code} #{STATUSES[code]} | #{(headers[:content_type] || '').gsub(/;.*$/, '')} #{size} bytes\n"
    end

Follow a redirection

[Source]

# File lib/rest-client-1.6.3/lib/restclient/abstract_response.rb, line 63
    def follow_redirection request = nil, result = nil, & block
      url = headers[:location]
      if url !~ /^http/
        url = URI.parse(args[:url]).merge(url).to_s
      end
      args[:url] = url
      if request
        if request.max_redirects == 0
          raise MaxRedirectsReached
        end
        args[:password] = request.password
        args[:user] = request.user
        args[:headers] = request.headers
        args[:max_redirects] = request.max_redirects - 1
        # pass any cookie set in the result
        if result && result['set-cookie']
          args[:headers][:cookies] = (args[:headers][:cookies] || {}).merge(parse_cookie(result['set-cookie']))
        end
      end
      Request.execute args, &block
    end

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

[Source]

# File lib/rest-client-1.6.3/lib/restclient/abstract_response.rb, line 16
    def headers
      @headers ||= AbstractResponse.beautify_headers(@net_http_res.to_hash)
    end

The raw headers.

[Source]

# File lib/rest-client-1.6.3/lib/restclient/abstract_response.rb, line 21
    def raw_headers
      @raw_headers ||= @net_http_res.to_hash
    end

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

[Source]

# File lib/rest-client-1.6.3/lib/restclient/abstract_response.rb, line 34
    def return! request = nil, result = 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].new(self, code)
        else
          follow_redirection(request, result, & block)
        end
      elsif code == 303
        args[:method] = :get
        args.delete :payload
        follow_redirection(request, result, & block)
      elsif Exceptions::EXCEPTIONS_MAP[code]
        raise Exceptions::EXCEPTIONS_MAP[code].new(self, code)
      else
        raise RequestFailed.new(self, code)
      end
    end

[Source]

# File lib/rest-client-1.6.3/lib/restclient/abstract_response.rb, line 54
    def to_i
      code
    end

[Validate]