Class: RestClient::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/rest_client.rb

Overview

Internal class used to build and execute the request.

Defined Under Namespace

Classes: Redirect, RequestFailed, Unauthorized

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, url, payload, headers) ⇒ Request

Returns a new instance of Request.



26
27
28
29
30
31
# File 'lib/rest_client.rb', line 26

def initialize(method, url, payload, headers)
	@method = method
	@url = url
	@payload = payload
	@headers = headers
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#payloadObject (readonly)

Returns the value of attribute payload.



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

def payload
  @payload
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#default_headersObject



94
95
96
# File 'lib/rest_client.rb', line 94

def default_headers
	{ :accept => 'application/xml' }
end

#error_message(res) ⇒ Object



90
91
92
# File 'lib/rest_client.rb', line 90

def error_message(res)
	"HTTP code #{res.code}: #{res.body}"
end

#executeObject



33
34
35
36
37
38
# File 'lib/rest_client.rb', line 33

def execute
	execute_inner
rescue Redirect => e
	@url = e.message
	execute
end

#execute_innerObject



40
41
42
43
# File 'lib/rest_client.rb', line 40

def execute_inner
	uri = parse_url(url)
	transmit uri, net_http_class(method).new(uri.path, make_headers(headers)), payload
end

#make_headers(user_headers) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/rest_client.rb', line 45

def make_headers(user_headers)
	final = {}
	merged = default_headers.merge(user_headers)
	merged.keys.each do |key|
		final[key.to_s.gsub(/_/, '-').capitalize] = merged[key]
	end
	final
end

#net_http_class(method) ⇒ Object



54
55
56
# File 'lib/rest_client.rb', line 54

def net_http_class(method)
	Object.module_eval "Net::HTTP::#{method.to_s.capitalize}"
end

#parse_url(url) ⇒ Object



58
59
60
61
# File 'lib/rest_client.rb', line 58

def parse_url(url)
	url = "http://#{url}" unless url.match(/^http/)
	URI.parse(url)
end

#process_result(res) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rest_client.rb', line 78

def process_result(res)
	if %w(200 201 202).include? res.code
		res.body
	elsif %w(301 302 303).include? res.code
		raise Redirect, res.header['Location']
	elsif res.code == "401"
		raise Unauthorized
	else
		raise RequestFailed, error_message(res)
	end
end

#transmit(uri, req, payload) ⇒ Object



72
73
74
75
76
# File 'lib/rest_client.rb', line 72

def transmit(uri, req, payload)
	Net::HTTP.start(uri.host, uri.port) do |http|
		process_result http.request(req, payload || "")
	end
end