Class: RestClient::Request

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

Overview

backwards compatibility

Constant Summary collapse

Redirect =
RestClient::Redirect
Unauthorized =
RestClient::Unauthorized
RequestFailed =
RestClient::RequestFailed

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Request

Returns a new instance of Request.



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

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.



64
65
66
# File 'lib/rest_client.rb', line 64

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



64
65
66
# File 'lib/rest_client.rb', line 64

def method
  @method
end

#passwordObject (readonly)

Returns the value of attribute password.



64
65
66
# File 'lib/rest_client.rb', line 64

def password
  @password
end

#payloadObject (readonly)

Returns the value of attribute payload.



64
65
66
# File 'lib/rest_client.rb', line 64

def payload
  @payload
end

#urlObject (readonly)

Returns the value of attribute url.



64
65
66
# File 'lib/rest_client.rb', line 64

def url
  @url
end

#userObject (readonly)

Returns the value of attribute user.



64
65
66
# File 'lib/rest_client.rb', line 64

def user
  @user
end

Class Method Details

.execute(args) ⇒ Object



66
67
68
# File 'lib/rest_client.rb', line 66

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

Instance Method Details

#default_headersObject



174
175
176
# File 'lib/rest_client.rb', line 174

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

#executeObject



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

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

#execute_innerObject



86
87
88
89
# File 'lib/rest_client.rb', line 86

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

#make_headers(user_headers) ⇒ Object



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

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



100
101
102
# File 'lib/rest_client.rb', line 100

def net_http_class(method)
	Net::HTTP.const_get(method.to_s.capitalize)
end

#parse_url(url) ⇒ Object



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

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

#parse_url_with_auth(url) ⇒ Object



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

def parse_url_with_auth(url)
	uri = parse_url(url)
	@user = uri.user if uri.user
	@password = uri.password if uri.password
	uri
end

#process_payload(p = nil, parent_key = nil) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rest_client.rb', line 116

def process_payload(p=nil, parent_key=nil)
	unless p.is_a?(Hash)
		p
	else
		@headers[:content_type] ||= 'application/x-www-form-urlencoded'
		p.keys.map do |k|
			key = parent_key ? "#{parent_key}[#{k}]" : k
			if p[k].is_a? Hash
				process_payload(p[k], key)
			else
				value = URI.escape(p[k].to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
				"#{key}=#{value}"
			end
		end.join("&")
	end
end

#process_result(res) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rest_client.rb', line 152

def process_result(res)
	if %w(200 201 202).include? res.code
		res.body
	elsif %w(301 302 303).include? res.code
		url = res.header['Location']

		if url !~ /^http/
			uri = URI.parse(@url)
			uri.path = "/#{url}".squeeze('/')
			url = uri.to_s
		end

		raise Redirect, url
	elsif res.code == "401"
		raise Unauthorized
	elsif res.code == "404"
		raise ResourceNotFound
	else
		raise RequestFailed, res
	end
end

#setup_credentials(req) ⇒ Object



148
149
150
# File 'lib/rest_client.rb', line 148

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

#transmit(uri, req, payload) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rest_client.rb', line 133

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

	net = Net::HTTP.new(uri.host, uri.port)
	net.use_ssl = uri.is_a?(URI::HTTPS)

	net.start do |http|
		process_result http.request(req, payload || "")
	end
rescue EOFError
	raise RestClient::ServerBrokeConnection
rescue Timeout::Error
	raise RestClient::RequestTimeout
end