Parent

Files

ActiveResource::Connection

Class to handle connections to remote web services. This class is used by ActiveResource::Base to interface with REST services.

Constants

HTTP_FORMAT_HEADER_NAMES

Attributes

site[R]
user[R]
password[R]
auth_type[R]
timeout[R]
proxy[R]
ssl_options[R]
format[RW]

Public Class Methods

new(site, format = ActiveResource::Formats::JsonFormat) click to toggle source

The site parameter is required and will set the site attribute to the URI for the remote resource service.

    # File lib/active_resource/connection.rb, line 33
33:     def initialize(site, format = ActiveResource::Formats::JsonFormat)
34:       raise ArgumentError, 'Missing site URI' unless site
35:       @user = @password = nil
36:       self.site = site
37:       self.format = format
38:     end
requests() click to toggle source
    # File lib/active_resource/connection.rb, line 26
26:       def requests
27:         @@requests ||= []
28:       end

Public Instance Methods

auth_type=(auth_type) click to toggle source

Sets the auth type for remote service.

    # File lib/active_resource/connection.rb, line 63
63:     def auth_type=(auth_type)
64:       @auth_type = legitimize_auth_type(auth_type)
65:     end
delete(path, headers = {}) click to toggle source

Executes a DELETE request (see HTTP protocol documentation if unfamiliar). Used to delete resources.

    # File lib/active_resource/connection.rb, line 85
85:     def delete(path, headers = {})
86:       with_auth { request(:delete, path, build_request_headers(headers, :delete, self.site.merge(path))) }
87:     end
get(path, headers = {}) click to toggle source

Executes a GET request. Used to get (find) resources.

    # File lib/active_resource/connection.rb, line 79
79:     def get(path, headers = {})
80:       with_auth { request(:get, path, build_request_headers(headers, :get, self.site.merge(path))) }
81:     end
head(path, headers = {}) click to toggle source

Executes a HEAD request. Used to obtain meta-information about resources, such as whether they exist and their size (via response headers).

     # File lib/active_resource/connection.rb, line 103
103:     def head(path, headers = {})
104:       with_auth { request(:head, path, build_request_headers(headers, :head, self.site.merge(path))) }
105:     end
password=(password) click to toggle source

Sets the password for remote service.

    # File lib/active_resource/connection.rb, line 58
58:     def password=(password)
59:       @password = password
60:     end
post(path, body = '', headers = {}) click to toggle source

Executes a POST request. Used to create new resources.

    # File lib/active_resource/connection.rb, line 97
97:     def post(path, body = '', headers = {})
98:       with_auth { request(:post, path, body.to_s, build_request_headers(headers, :post, self.site.merge(path))) }
99:     end
proxy=(proxy) click to toggle source

Set the proxy for remote service.

    # File lib/active_resource/connection.rb, line 48
48:     def proxy=(proxy)
49:       @proxy = proxy.is_a?(URI) ? proxy : URI.parser.parse(proxy)
50:     end
put(path, body = '', headers = {}) click to toggle source

Executes a PUT request (see HTTP protocol documentation if unfamiliar). Used to update resources.

    # File lib/active_resource/connection.rb, line 91
91:     def put(path, body = '', headers = {})
92:       with_auth { request(:put, path, body.to_s, build_request_headers(headers, :put, self.site.merge(path))) }
93:     end
site=(site) click to toggle source

Set URI for remote service.

    # File lib/active_resource/connection.rb, line 41
41:     def site=(site)
42:       @site = site.is_a?(URI) ? site : URI.parser.parse(site)
43:       @user = URI.parser.unescape(@site.user) if @site.user
44:       @password = URI.parser.unescape(@site.password) if @site.password
45:     end
ssl_options=(opts={}) click to toggle source

Hash of options applied to Net::HTTP instance when site protocol is ‘https’.

    # File lib/active_resource/connection.rb, line 73
73:     def ssl_options=(opts={})
74:       @ssl_options = opts
75:     end
timeout=(timeout) click to toggle source

Sets the number of seconds after which HTTP requests to the remote service should time out.

    # File lib/active_resource/connection.rb, line 68
68:     def timeout=(timeout)
69:       @timeout = timeout
70:     end
user=(user) click to toggle source

Sets the user for remote service.

    # File lib/active_resource/connection.rb, line 53
53:     def user=(user)
54:       @user = user
55:     end

Private Instance Methods

apply_ssl_options(http) click to toggle source
     # File lib/active_resource/connection.rb, line 180
180:       def apply_ssl_options(http)
181:         return http unless @site.is_a?(URI::HTTPS)
182: 
183:         http.use_ssl     = true
184:         http.verify_mode = OpenSSL::SSL::VERIFY_NONE
185:         return http unless defined?(@ssl_options)
186: 
187:         http.ca_path     = @ssl_options[:ca_path] if @ssl_options[:ca_path]
188:         http.ca_file     = @ssl_options[:ca_file] if @ssl_options[:ca_file]
189: 
190:         http.cert        = @ssl_options[:cert] if @ssl_options[:cert]
191:         http.key         = @ssl_options[:key]  if @ssl_options[:key]
192: 
193:         http.cert_store  = @ssl_options[:cert_store]  if @ssl_options[:cert_store]
194:         http.ssl_timeout = @ssl_options[:ssl_timeout] if @ssl_options[:ssl_timeout]
195: 
196:         http.verify_mode     = @ssl_options[:verify_mode]     if @ssl_options[:verify_mode]
197:         http.verify_callback = @ssl_options[:verify_callback] if @ssl_options[:verify_callback]
198:         http.verify_depth    = @ssl_options[:verify_depth]    if @ssl_options[:verify_depth]
199: 
200:         http
201:       end
auth_attributes_for(uri, request_digest, params) click to toggle source
     # File lib/active_resource/connection.rb, line 264
264:       def auth_attributes_for(uri, request_digest, params)
265:         [
266:           %(username="#{@user}"),
267:           %(realm="#{params['realm']}"),
268:           %(qop="#{params['qop']}"),
269:           %(uri="#{uri.path}"),
270:           %(nonce="#{params['nonce']}"),
271:           %(nc="0"),
272:           %(cnonce="#{params['cnonce']}"),
273:           %(opaque="#{params['opaque']}"),
274:           %(response="#{request_digest}")].join(", ")
275:       end
authorization_header(http_method, uri) click to toggle source
     # File lib/active_resource/connection.rb, line 226
226:       def authorization_header(http_method, uri)
227:         if @user || @password
228:           if auth_type == :digest
229:             { 'Authorization' => digest_auth_header(http_method, uri) }
230:           else
231:             { 'Authorization' => 'Basic ' + ["#{@user}:#{@password}"].pack('m').delete("\r\n") }
232:           end
233:         else
234:           {}
235:         end
236:       end
build_request_headers(headers, http_method, uri) click to toggle source

Builds headers for request to remote service.

     # File lib/active_resource/connection.rb, line 208
208:       def build_request_headers(headers, http_method, uri)
209:         authorization_header(http_method, uri).update(default_header).update(http_format_header(http_method)).update(headers)
210:       end
client_nonce() click to toggle source
     # File lib/active_resource/connection.rb, line 252
252:       def client_nonce
253:         Digest::MD5.hexdigest("%x" % (Time.now.to_i + rand(65535)))
254:       end
configure_http(http) click to toggle source
     # File lib/active_resource/connection.rb, line 168
168:       def configure_http(http)
169:         http = apply_ssl_options(http)
170: 
171:         # Net::HTTP timeouts default to 60 seconds.
172:         if @timeout
173:           http.open_timeout = @timeout
174:           http.read_timeout = @timeout
175:         end
176: 
177:         http
178:       end
default_header() click to toggle source
     # File lib/active_resource/connection.rb, line 203
203:       def default_header
204:         @default_header ||= {}
205:       end
digest_auth_header(http_method, uri) click to toggle source
     # File lib/active_resource/connection.rb, line 238
238:       def digest_auth_header(http_method, uri)
239:         params = extract_params_from_response
240: 
241:         request_uri = uri.path
242:         request_uri << "?#{uri.query}" if uri.query
243: 
244:         ha1 = Digest::MD5.hexdigest("#{@user}:#{params['realm']}:#{@password}")
245:         ha2 = Digest::MD5.hexdigest("#{http_method.to_s.upcase}:#{request_uri}")
246: 
247:         params.merge!('cnonce' => client_nonce)
248:         request_digest = Digest::MD5.hexdigest([ha1, params['nonce'], "0", params['cnonce'], params['qop'], ha2].join(":"))
249:         "Digest #{auth_attributes_for(uri, request_digest, params)}"
250:       end
extract_params_from_response() click to toggle source
     # File lib/active_resource/connection.rb, line 256
256:       def extract_params_from_response
257:         params = {}
258:         if response_auth_header =~ /^(\w+) (.*)/
259:           $2.gsub(/(\w+)="(.*?)"/) { params[$1] = $2 }
260:         end
261:         params
262:       end
handle_response(response) click to toggle source

Handles response and error codes from the remote service.

     # File lib/active_resource/connection.rb, line 123
123:       def handle_response(response)
124:         case response.code.to_i
125:           when 301, 302, 303, 307
126:             raise(Redirection.new(response))
127:           when 200...400
128:             response
129:           when 400
130:             raise(BadRequest.new(response))
131:           when 401
132:             raise(UnauthorizedAccess.new(response))
133:           when 403
134:             raise(ForbiddenAccess.new(response))
135:           when 404
136:             raise(ResourceNotFound.new(response))
137:           when 405
138:             raise(MethodNotAllowed.new(response))
139:           when 409
140:             raise(ResourceConflict.new(response))
141:           when 410
142:             raise(ResourceGone.new(response))
143:           when 422
144:             raise(ResourceInvalid.new(response))
145:           when 401...500
146:             raise(ClientError.new(response))
147:           when 500...600
148:             raise(ServerError.new(response))
149:           else
150:             raise(ConnectionError.new(response, "Unknown response code: #{response.code}"))
151:         end
152:       end
http() click to toggle source

Creates new Net::HTTP instance for communication with the remote service and resources.

     # File lib/active_resource/connection.rb, line 156
156:       def http
157:         configure_http(new_http)
158:       end
http() click to toggle source
     # File lib/active_resource/http_mock.rb, line 330
330:         def http
331:           @http ||= HttpMock.new(@site)
332:         end
http_format_header(http_method) click to toggle source
     # File lib/active_resource/connection.rb, line 277
277:       def http_format_header(http_method)
278:         {HTTP_FORMAT_HEADER_NAMES[http_method] => format.mime_type}
279:       end
legitimize_auth_type(auth_type) click to toggle source
     # File lib/active_resource/connection.rb, line 281
281:       def legitimize_auth_type(auth_type)
282:         return :basic if auth_type.nil?
283:         auth_type = auth_type.to_sym
284:         auth_type.in?([:basic, :digest]) ? auth_type : :basic
285:       end
new_http() click to toggle source
     # File lib/active_resource/connection.rb, line 160
160:       def new_http
161:         if @proxy
162:           Net::HTTP.new(@site.host, @site.port, @proxy.host, @proxy.port, @proxy.user, @proxy.password)
163:         else
164:           Net::HTTP.new(@site.host, @site.port)
165:         end
166:       end
request(method, path, *arguments) click to toggle source

Makes a request to the remote service.

     # File lib/active_resource/connection.rb, line 109
109:       def request(method, path, *arguments)
110:         result = ActiveSupport::Notifications.instrument("request.active_resource") do |payload|
111:           payload[:method]      = method
112:           payload[:request_uri] = "#{site.scheme}://#{site.host}:#{site.port}#{path}"
113:           payload[:result]      = http.send(method, path, *arguments)
114:         end
115:         handle_response(result)
116:       rescue Timeout::Error => e
117:         raise TimeoutError.new(e.message)
118:       rescue OpenSSL::SSL::SSLError => e
119:         raise SSLError.new(e.message)
120:       end
response_auth_header() click to toggle source
     # File lib/active_resource/connection.rb, line 212
212:       def response_auth_header
213:         @response_auth_header ||= ""
214:       end
with_auth() click to toggle source
     # File lib/active_resource/connection.rb, line 216
216:       def with_auth
217:         retried ||= false
218:         yield
219:       rescue UnauthorizedAccess => e
220:         raise if retried || auth_type != :digest
221:         @response_auth_header = e.response['WWW-Authenticate']
222:         retried = true
223:         retry
224:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.