Module: RestClient::AbstractResponse

### Home Classes Methods

### In Files

- lib/restclient/abstract_response.rb

### Methods

- ::beautify_headers
- #code
- #cookie_jar
- #cookies
- #description
- #follow_redirection
- #headers
- #raw_headers
- #response_set_vars
- #return!
- #to_i

### Class/Module Index

Quicksearch

- RestClient
- RestClient::AbstractResponse
- RestClient::Exception
- RestClient::ExceptionWithResponse
- RestClient::Exceptions
- RestClient::MaxRedirectsReached
- RestClient::Payload
- RestClient::Payload::Base
- RestClient::Payload::Multipart
- RestClient::Payload::Streamed
- RestClient::Payload::UrlEncoded
- RestClient::Platform
- RestClient::RawResponse
- RestClient::Redirect
- RestClient::Request
- RestClient::Request::Redirect
- RestClient::Request::RequestFailed
- RestClient::RequestFailed
- RestClient::Resource
- RestClient::Response
- RestClient::ResponseForException
- RestClient::SSLCertificateNotVerified
- RestClient::ServerBrokeConnection
- RestClient::Windows
- RestClient::Windows::RootCerts
- RestClient::Windows::RootCerts::CERT_CONTEXT

No matching classes.

# RestClient::AbstractResponse

### Attributes

args[R]
net_http_res[R]
request[R]

### Public Class Methods

beautify_headers(headers) click to toggle source

    # File lib/restclient/abstract_response.rb, line 114
    def self.beautify_headers(headers)
      headers.inject({}) do |out, (key, value)|
        out[key.gsub(/-/, '_').downcase.to_sym] = %{ set-cookie }.include?(key.downcase) ? value : value.first
        out
      end
    end

### Public Instance Methods

code() click to toggle source

HTTP status code

    # File lib/restclient/abstract_response.rb, line 11
    def code
      @code ||= @net_http_res.code.to_i
    end

cookie_jar() click to toggle source

Cookie jar extracted from response headers.

@return [HTTP::CookieJar]

    # File lib/restclient/abstract_response.rb, line 47
    def cookie_jar
      return @cookie_jar if @cookie_jar

      jar = HTTP::CookieJar.new
      headers.fetch(:set_cookie, []).each do |cookie|
        jar.parse(cookie, @request.url)
      end

      @cookie_jar = jar
    end

cookies() click to toggle source

Hash of cookies extracted from response headers

    # File lib/restclient/abstract_response.rb, line 33
    def cookies
      hash = {}

      cookie_jar.cookies.each do |cookie|
        hash[cookie.name] = cookie.value
      end

      hash
    end

description() click to toggle source

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

follow_redirection(request = nil, result = nil, & block) click to toggle source

Follow a redirection

    # File lib/restclient/abstract_response.rb, line 89
    def follow_redirection request = nil, result = nil, & block
      new_args = @args.dup

      url = headers[:location]
      if url !~ /^http/
        url = URI.parse(request.url).merge(url).to_s
      end
      new_args[:url] = url
      if request
        if request.max_redirects == 0
          raise MaxRedirectsReached
        end
        new_args[:password] = request.password
        new_args[:user] = request.user
        new_args[:headers] = request.headers
        new_args[:max_redirects] = request.max_redirects - 1

        # TODO: figure out what to do with original :cookie, :cookies values
        new_args[:headers]['Cookie'] = HTTP::Cookie.cookie_value(
          cookie_jar.cookies(new_args.fetch(:url)))
      end

      Request.execute(new_args, &block)
    end

headers() click to toggle source

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

    # File lib/restclient/abstract_response.rb, line 17
    def headers
      @headers ||= AbstractResponse.beautify_headers(@net_http_res.to_hash)
    end

raw_headers() click to toggle source

The raw headers.

    # File lib/restclient/abstract_response.rb, line 22
    def raw_headers
      @raw_headers ||= @net_http_res.to_hash
    end

response_set_vars(net_http_res, args, request) click to toggle source

    # File lib/restclient/abstract_response.rb, line 26
    def response_set_vars(net_http_res, args, request)
      @net_http_res = net_http_res
      @args = args
      @request = request
    end

return!(request = nil, result = nil, & block) click to toggle source

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

    # File lib/restclient/abstract_response.rb, line 60
    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

to_i() click to toggle source

    # File lib/restclient/abstract_response.rb, line 80
    def to_i
      code
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.