module RestClient::AbstractResponse

def self.beautify_headers(headers)

Returns:
  • (Hash) -

Parameters:
  • headers (Hash) --
def self.beautify_headers(headers)
  headers.inject({}) do |out, (key, value)|
    key_sym = key.tr('-', '_').downcase.to_sym
    # Handle Set-Cookie specially since it cannot be joined by comma.
    if key.downcase == 'set-cookie'
      out[key_sym] = value
    else
      out[key_sym] = value.join(', ')
    end
    out
  end
end

def _follow_redirection(new_args, &block)

Parameters:
  • new_args (Hash) -- Start with this hash of arguments for the
def _follow_redirection(new_args, &block)
  # parse location header and merge into existing URL
  url = headers[:location]
  # cannot follow redirection if there is no location header
  unless url
    raise exception_with_response
  end
  # handle relative redirects
  unless url.start_with?('http')
    url = URI.parse(request.url).merge(url).to_s
  end
  new_args[:url] = url
  new_args[:password] = request.password
  new_args[:user] = request.user
  new_args[:headers] = request.headers
  new_args[:max_redirects] = request.max_redirects - 1
  # pass through our new cookie jar
  new_args[:cookies] = cookie_jar
  # prepare new request
  new_req = Request.new(new_args)
  # append self to redirection history
  new_req.redirection_history = history + [self]
  # execute redirected request
  new_req.execute(&block)
end

def check_max_redirects

def check_max_redirects
  if request.max_redirects <= 0
    raise exception_with_response
  end
end

def code

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

def cookie_jar

Returns:
  • (HTTP::CookieJar) -
def cookie_jar
  return @cookie_jar if defined?(@cookie_jar) && @cookie_jar
  jar = @request.cookie_jar.dup
  headers.fetch(:set_cookie, []).each do |cookie|
    jar.parse(cookie, @request.uri)
  end
  @cookie_jar = jar
end

def cookies

Returns:
  • (Hash) -

Other tags:
    See: #cookie_jar -
def cookies
  hash = {}
  cookie_jar.cookies(@request.uri).each do |cookie|
    hash[cookie.name] = cookie.value
  end
  hash
end

def description

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

def exception_with_response

def exception_with_response
  begin
    klass = Exceptions::EXCEPTIONS_MAP.fetch(code)
  rescue KeyError
    raise RequestFailed.new(self, code)
  end
  raise klass.new(self, code)
end

def follow_get_redirection(&block)

the payload from the original request.
Follow a redirection response, but change the HTTP method to GET and drop
def follow_get_redirection(&block)
  new_args = request.args.dup
  new_args[:method] = :get
  new_args.delete(:payload)
  _follow_redirection(new_args, &block)
end

def follow_redirection(&block)

redirection target.
Follow a redirection response by making a new HTTP request to the
def follow_redirection(&block)
  _follow_redirection(request.args.dup, &block)
end

def headers

e.g. "Content-type" will become :content_type.
A hash of the headers, beautified with symbols and underscores.
def headers
  @headers ||= AbstractResponse.beautify_headers(@net_http_res.to_hash)
end

def history

def history
  @history ||= request.redirection_history || []
end

def inspect

def inspect
  raise NotImplementedError.new('must override in subclass')
end

def log

Logger from the request, potentially nil.
def log
  request.log
end

def log_response

def log_response
  return unless log
  code = net_http_res.code
  res_name = net_http_res.class.to_s.gsub(/\ANet::HTTP/, '')
  content_type = (net_http_res['Content-type'] || '').gsub(/;.*\z/, '')
  log << "# => #{code} #{res_name} | #{content_type} #{size} bytes, #{sprintf('%.2f', duration)}s\n"
end

def raw_headers

The raw headers.
def raw_headers
  @raw_headers ||= @net_http_res.to_hash
end

def response_set_vars(net_http_res, request, start_time)

Parameters:
  • start_time (Time) --
  • request (RestClient::Request) --
  • net_http_res (Net::HTTPResponse) --
def response_set_vars(net_http_res, request, start_time)
  @net_http_res = net_http_res
  @request = request
  @start_time = start_time
  @end_time = Time.now
  if @start_time
    @duration = @end_time - @start_time
  else
    @duration = nil
  end
  # prime redirection history
  history
end

def return!(&block)


For all other responses, raise a response exception

303: redirect, changing method to GET, if there is a Location header
301, 302, 307: redirect GET / HEAD if there is a Location header
For 30x status codes:

For 20x status codes: return the response itself

Return the default behavior corresponding to the response code:
def return!(&block)
  case code
  when 200..207
    self
  when 301, 302, 307
    case request.method
    when 'get', 'head'
      check_max_redirects
      follow_redirection(&block)
    else
      raise exception_with_response
    end
  when 303
    check_max_redirects
    follow_get_redirection(&block)
  else
    raise exception_with_response
  end
end

def to_i

def to_i
  warn('warning: calling Response#to_i is not recommended')
  super
end