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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Request

Returns a new instance of Request.



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

def initialize(args)
	@method = args[:method] or raise ArgumentError, "must pass :method"
	@url = args[:url] or raise ArgumentError, "must pass :url"
	@headers = args[:headers] || {}
	@payload = process_payload(args[:payload])
	@user = args[:user]
	@password = args[:password]
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#payloadObject (readonly)

Returns the value of attribute payload.



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

def payload
  @payload
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

#userObject (readonly)

Returns the value of attribute user.



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

def user
  @user
end

Class Method Details

.execute(args) ⇒ Object



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

def self.execute(args)
	new(args).execute
end

Instance Method Details

#default_headersObject



130
131
132
# File 'lib/rest_client.rb', line 130

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

#error_message(res) ⇒ Object



126
127
128
# File 'lib/rest_client.rb', line 126

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

#executeObject



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

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

#execute_innerObject



61
62
63
64
# File 'lib/rest_client.rb', line 61

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

#make_headers(user_headers) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/rest_client.rb', line 66

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



75
76
77
# File 'lib/rest_client.rb', line 75

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

#parse_url(url) ⇒ Object



79
80
81
82
# File 'lib/rest_client.rb', line 79

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

#process_payload(p = nil) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/rest_client.rb', line 93

def process_payload(p=nil)
	unless p.is_a?(Hash)
		p
	else
		@headers[:content_type] = 'application/x-www-form-urlencoded'
		p.keys.map { |k| "#{k}=#{URI.escape(p[k].to_s)}" }.join("&")
	end
end

#process_result(res) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rest_client.rb', line 114

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

#setup_credentials(req) ⇒ Object



110
111
112
# File 'lib/rest_client.rb', line 110

def setup_credentials(req)
	req.basic_auth(user, password) if user
end

#transmit(uri, req, payload) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/rest_client.rb', line 102

def transmit(uri, req, payload)
	setup_credentials(req)

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