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.



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

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.



37
38
39
# File 'lib/rest_client.rb', line 37

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



37
38
39
# File 'lib/rest_client.rb', line 37

def method
  @method
end

#passwordObject (readonly)

Returns the value of attribute password.



37
38
39
# File 'lib/rest_client.rb', line 37

def password
  @password
end

#payloadObject (readonly)

Returns the value of attribute payload.



37
38
39
# File 'lib/rest_client.rb', line 37

def payload
  @payload
end

#urlObject (readonly)

Returns the value of attribute url.



37
38
39
# File 'lib/rest_client.rb', line 37

def url
  @url
end

#userObject (readonly)

Returns the value of attribute user.



37
38
39
# File 'lib/rest_client.rb', line 37

def user
  @user
end

Class Method Details

.execute(args) ⇒ Object



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

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

Instance Method Details

#default_headersObject



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

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

#executeObject



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

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

#execute_innerObject



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

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



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

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



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

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

#parse_url(url) ⇒ Object



77
78
79
80
# File 'lib/rest_client.rb', line 77

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

#parse_url_with_auth(url) ⇒ Object



82
83
84
85
86
87
# File 'lib/rest_client.rb', line 82

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) ⇒ Object



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

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

#process_result(res) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rest_client.rb', line 120

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



116
117
118
# File 'lib/rest_client.rb', line 116

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

#transmit(uri, req, payload) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rest_client.rb', line 101

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