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.



96
97
98
99
100
101
102
103
# File 'lib/rest_client.rb', line 96

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.



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

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#payloadObject (readonly)

Returns the value of attribute payload.



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

def payload
  @payload
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

#userObject (readonly)

Returns the value of attribute user.



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

def user
  @user
end

Class Method Details

.execute(args) ⇒ Object



92
93
94
# File 'lib/rest_client.rb', line 92

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

Instance Method Details

#decode(content_encoding, body) ⇒ Object



212
213
214
215
216
217
218
219
220
# File 'lib/rest_client.rb', line 212

def decode(content_encoding, body)
	if content_encoding == 'gzip'
		Zlib::GzipReader.new(StringIO.new(body)).read
	elsif content_encoding == 'deflate'
		Zlib::Inflate.new.inflate(body)
	else
		body
	end
end

#default_headersObject



246
247
248
# File 'lib/rest_client.rb', line 246

def default_headers
	{ :accept => 'application/xml', :accept_encoding => 'gzip, deflate' }
end

#display_log(msg) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
# File 'lib/rest_client.rb', line 234

def display_log(msg)
	return unless log_to = RestClient.log

	if log_to == 'stdout'
		STDOUT.puts msg
	elsif log_to == 'stderr'
		STDERR.puts msg
	else
		File.open(log_to, 'a') { |f| f.puts msg }
	end
end

#executeObject



105
106
107
108
109
110
# File 'lib/rest_client.rb', line 105

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

#execute_innerObject



112
113
114
115
# File 'lib/rest_client.rb', line 112

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

#make_headers(user_headers) ⇒ Object



117
118
119
120
121
122
# File 'lib/rest_client.rb', line 117

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

#net_http_classObject



124
125
126
127
128
129
130
131
# File 'lib/rest_client.rb', line 124

def net_http_class
	if RestClient.proxy
		proxy_uri = URI.parse(RestClient.proxy)
		Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
	else
		Net::HTTP
	end
end

#net_http_request_class(method) ⇒ Object



133
134
135
# File 'lib/rest_client.rb', line 133

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

#parse_url(url) ⇒ Object



137
138
139
140
# File 'lib/rest_client.rb', line 137

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

#parse_url_with_auth(url) ⇒ Object



142
143
144
145
146
147
# File 'lib/rest_client.rb', line 142

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



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rest_client.rb', line 149

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



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/rest_client.rb', line 190

def process_result(res)
	if %w(200 201 202).include? res.code
		decode res['content-encoding'], 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, res
	elsif res.code == "404"
		raise ResourceNotFound, res
	else
		raise RequestFailed, res
	end
end

#request_logObject



222
223
224
225
226
227
228
# File 'lib/rest_client.rb', line 222

def request_log
	out = []
	out << "RestClient.#{method} #{url.inspect}"
	out << (payload.size > 100 ? "(#{payload.size} byte payload)".inspect : payload.inspect) if payload
	out << headers.inspect.gsub(/^\{/, '').gsub(/\}$/, '') unless headers.empty?
	out.join(', ')
end

#response_log(res) ⇒ Object



230
231
232
# File 'lib/rest_client.rb', line 230

def response_log(res)
	"# => #{res.code} #{res.class.to_s.gsub(/^Net::HTTP/, '')} | #{(res['Content-type'] || '').gsub(/;.*$/, '')} #{res.body.size} bytes"
end

#setup_credentials(req) ⇒ Object



186
187
188
# File 'lib/rest_client.rb', line 186

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

#transmit(uri, req, payload) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/rest_client.rb', line 166

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

	net = net_http_class.new(uri.host, uri.port)
	net.use_ssl = uri.is_a?(URI::HTTPS)
	net.verify_mode = OpenSSL::SSL::VERIFY_NONE

	display_log request_log

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