# File lib/restclient/abstract_response.rb, line 77 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
HTTP status code
# File lib/restclient/abstract_response.rb, line 10 def code @code ||= @net_http_res.code.to_i end
# File lib/restclient/abstract_response.rb, line 63 def description "#{code} #{STATUSES[code]} | #{(headers[:content_type] || '').gsub(/;.*$/, '')} #{size} bytes\n" end
Follow a redirection
# File lib/restclient/abstract_response.rb, line 68 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
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 16 def headers @headers ||= AbstractResponse.beautify_headers(@net_http_res.to_hash) end
The raw headers.
# File 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 and 302 in get and head cases, redirection for 303 and an exception in other cases
# File lib/restclient/abstract_response.rb, line 39 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
# File lib/restclient/abstract_response.rb, line 59 def to_i code end