Class: RestClient::Response

Inherits:
String
  • Object
show all
Defined in:
lib/restclient/response.rb

Overview

The response from RestClient looks like a string, but is actually one of these. 99% of the time you’re making a rest call all you care about is the body, but on the occassion you want to fetch the headers you can:

RestClient.get('http://example.com').headers[:content_type]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, net_http_res) ⇒ Response

Returns a new instance of Response.



11
12
13
14
# File 'lib/restclient/response.rb', line 11

def initialize(string, net_http_res)
	@net_http_res = net_http_res
	super(string || "")
end

Instance Attribute Details

#net_http_resObject (readonly)

Returns the value of attribute net_http_res.



9
10
11
# File 'lib/restclient/response.rb', line 9

def net_http_res
  @net_http_res
end

Class Method Details

.beautify_headers(headers) ⇒ Object



39
40
41
42
43
44
# File 'lib/restclient/response.rb', line 39

def self.beautify_headers(headers)
	headers.inject({}) do |out, (key, value)|
		out[key.gsub(/-/, '_').to_sym] = value.first
		out
	end
end

Instance Method Details

#codeObject

HTTP status code, always 200 since RestClient throws exceptions for other codes.



18
19
20
# File 'lib/restclient/response.rb', line 18

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

#cookiesObject

Hash of cookies extracted from response headers



29
30
31
32
33
34
35
36
37
# File 'lib/restclient/response.rb', line 29

def cookies
  @cookies ||= (self.headers[:set_cookie] || "").split('; ').inject({}) do |out, raw_c|
    key, val = raw_c.split('=')
    unless %w(expires domain path secure).member?(key)
      out[key] = val
    end
    out
  end
end

#headersObject

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



24
25
26
# File 'lib/restclient/response.rb', line 24

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