2006-06-25 10:44:22 -04:00
|
|
|
require 'net/https'
|
|
|
|
require 'date'
|
|
|
|
require 'time'
|
|
|
|
require 'uri'
|
2006-10-23 18:21:09 -04:00
|
|
|
require 'benchmark'
|
2006-06-25 10:44:22 -04:00
|
|
|
|
|
|
|
module ActiveResource
|
2007-06-23 13:29:54 -04:00
|
|
|
class ConnectionError < StandardError # :nodoc:
|
2006-06-25 10:44:22 -04:00
|
|
|
attr_reader :response
|
|
|
|
|
|
|
|
def initialize(response, message = nil)
|
|
|
|
@response = response
|
|
|
|
@message = message
|
|
|
|
end
|
2006-08-31 05:31:11 -04:00
|
|
|
|
2006-06-25 10:44:22 -04:00
|
|
|
def to_s
|
2007-08-09 15:22:04 -04:00
|
|
|
"Failed with #{response.code} #{response.message if response.respond_to?(:message)}"
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
|
|
|
end
|
2006-08-31 05:31:11 -04:00
|
|
|
|
2007-08-09 15:22:04 -04:00
|
|
|
# 3xx Redirection
|
|
|
|
class Redirection < ConnectionError # :nodoc:
|
|
|
|
def to_s; response['Location'] ? "#{super} => #{response['Location']}" : super; end
|
|
|
|
end
|
|
|
|
|
2007-06-23 13:29:54 -04:00
|
|
|
# 4xx Client Error
|
|
|
|
class ClientError < ConnectionError; end # :nodoc:
|
|
|
|
|
2007-12-14 18:09:46 -05:00
|
|
|
# 400 Bad Request
|
|
|
|
class BadRequest < ClientError; end # :nodoc
|
|
|
|
|
|
|
|
# 401 Unauthorized
|
|
|
|
class UnauthorizedAccess < ClientError; end # :nodoc
|
|
|
|
|
|
|
|
# 403 Forbidden
|
|
|
|
class ForbiddenAccess < ClientError; end # :nodoc
|
|
|
|
|
2007-06-23 13:29:54 -04:00
|
|
|
# 404 Not Found
|
|
|
|
class ResourceNotFound < ClientError; end # :nodoc:
|
|
|
|
|
|
|
|
# 409 Conflict
|
|
|
|
class ResourceConflict < ClientError; end # :nodoc:
|
2006-06-25 10:44:22 -04:00
|
|
|
|
2007-06-23 13:29:54 -04:00
|
|
|
# 5xx Server Error
|
|
|
|
class ServerError < ConnectionError; end # :nodoc:
|
2006-08-31 05:31:11 -04:00
|
|
|
|
2007-05-26 16:57:08 -04:00
|
|
|
# 405 Method Not Allowed
|
2007-06-23 13:29:54 -04:00
|
|
|
class MethodNotAllowed < ClientError # :nodoc:
|
2007-05-26 16:57:08 -04:00
|
|
|
def allowed_methods
|
|
|
|
@response['Allow'].split(',').map { |verb| verb.strip.downcase.to_sym }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-06-23 13:29:54 -04:00
|
|
|
# Class to handle connections to remote web services.
|
|
|
|
# This class is used by ActiveResource::Base to interface with REST
|
|
|
|
# services.
|
2006-06-25 10:44:22 -04:00
|
|
|
class Connection
|
2006-09-29 12:25:49 -04:00
|
|
|
attr_reader :site
|
2007-09-20 19:18:05 -04:00
|
|
|
attr_accessor :format
|
2006-08-31 05:31:11 -04:00
|
|
|
|
2006-06-25 10:44:22 -04:00
|
|
|
class << self
|
|
|
|
def requests
|
|
|
|
@@requests ||= []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-06-23 13:29:54 -04:00
|
|
|
# The +site+ parameter is required and will set the +site+
|
|
|
|
# attribute to the URI for the remote resource service.
|
2007-09-20 19:18:05 -04:00
|
|
|
def initialize(site, format = ActiveResource::Formats[:xml])
|
2006-12-05 14:12:51 -05:00
|
|
|
raise ArgumentError, 'Missing site URI' unless site
|
|
|
|
self.site = site
|
2007-09-20 19:18:05 -04:00
|
|
|
self.format = format
|
2006-09-29 12:25:49 -04:00
|
|
|
end
|
2006-12-05 14:12:51 -05:00
|
|
|
|
2007-01-16 01:34:10 -05:00
|
|
|
# Set URI for remote service.
|
2006-09-29 12:25:49 -04:00
|
|
|
def site=(site)
|
|
|
|
@site = site.is_a?(URI) ? site : URI.parse(site)
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
2006-08-31 05:31:11 -04:00
|
|
|
|
2007-01-16 01:34:10 -05:00
|
|
|
# Execute a GET request.
|
|
|
|
# Used to get (find) resources.
|
2007-04-28 23:14:36 -04:00
|
|
|
def get(path, headers = {})
|
2007-09-20 19:18:05 -04:00
|
|
|
format.decode(request(:get, path, build_request_headers(headers)).body)
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
2006-08-31 05:31:11 -04:00
|
|
|
|
2007-01-16 01:34:10 -05:00
|
|
|
# Execute a DELETE request (see HTTP protocol documentation if unfamiliar).
|
|
|
|
# Used to delete resources.
|
2007-04-28 23:14:36 -04:00
|
|
|
def delete(path, headers = {})
|
|
|
|
request(:delete, path, build_request_headers(headers))
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
2006-08-31 05:31:11 -04:00
|
|
|
|
2007-01-16 01:34:10 -05:00
|
|
|
# Execute a PUT request (see HTTP protocol documentation if unfamiliar).
|
|
|
|
# Used to update resources.
|
2007-04-28 23:14:36 -04:00
|
|
|
def put(path, body = '', headers = {})
|
2007-06-19 14:40:28 -04:00
|
|
|
request(:put, path, body.to_s, build_request_headers(headers))
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
|
|
|
|
2007-01-16 01:34:10 -05:00
|
|
|
# Execute a POST request.
|
|
|
|
# Used to create new resources.
|
2007-04-28 23:14:36 -04:00
|
|
|
def post(path, body = '', headers = {})
|
2007-06-19 14:40:28 -04:00
|
|
|
request(:post, path, body.to_s, build_request_headers(headers))
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
2006-08-31 05:31:11 -04:00
|
|
|
|
2007-04-19 18:18:03 -04:00
|
|
|
|
2006-06-25 10:44:22 -04:00
|
|
|
private
|
2007-01-16 01:34:10 -05:00
|
|
|
# Makes request to remote service.
|
2006-10-23 18:21:09 -04:00
|
|
|
def request(method, path, *arguments)
|
2006-12-05 14:12:51 -05:00
|
|
|
logger.info "#{method.to_s.upcase} #{site.scheme}://#{site.host}:#{site.port}#{path}" if logger
|
2006-10-23 18:21:09 -04:00
|
|
|
result = nil
|
|
|
|
time = Benchmark.realtime { result = http.send(method, path, *arguments) }
|
2007-12-10 00:53:56 -05:00
|
|
|
logger.info "--> #{result.code} #{result.message} (#{result.body ? result.body : 0}b %.2fs)" % time if logger
|
2006-10-23 18:21:09 -04:00
|
|
|
handle_response(result)
|
2006-08-31 05:31:11 -04:00
|
|
|
end
|
2006-06-25 10:44:22 -04:00
|
|
|
|
2007-01-16 01:34:10 -05:00
|
|
|
# Handles response and error codes from remote service.
|
2006-08-31 05:31:11 -04:00
|
|
|
def handle_response(response)
|
2006-06-25 10:44:22 -04:00
|
|
|
case response.code.to_i
|
2007-08-09 15:22:04 -04:00
|
|
|
when 301,302
|
|
|
|
raise(Redirection.new(response))
|
2006-08-31 05:31:11 -04:00
|
|
|
when 200...400
|
2006-06-25 10:44:22 -04:00
|
|
|
response
|
2007-12-14 18:09:46 -05:00
|
|
|
when 400
|
|
|
|
raise(BadRequest.new(response))
|
|
|
|
when 401
|
|
|
|
raise(UnauthorizedAccess.new(response))
|
|
|
|
when 403
|
|
|
|
raise(ForbiddenAccess.new(response))
|
2006-06-25 10:44:22 -04:00
|
|
|
when 404
|
|
|
|
raise(ResourceNotFound.new(response))
|
2007-05-26 16:57:08 -04:00
|
|
|
when 405
|
|
|
|
raise(MethodNotAllowed.new(response))
|
2006-09-08 20:11:12 -04:00
|
|
|
when 409
|
|
|
|
raise(ResourceConflict.new(response))
|
2007-01-16 19:46:32 -05:00
|
|
|
when 422
|
|
|
|
raise(ResourceInvalid.new(response))
|
2006-09-07 20:07:30 -04:00
|
|
|
when 401...500
|
2006-06-25 10:44:22 -04:00
|
|
|
raise(ClientError.new(response))
|
|
|
|
when 500...600
|
|
|
|
raise(ServerError.new(response))
|
|
|
|
else
|
|
|
|
raise(ConnectionError.new(response, "Unknown response code: #{response.code}"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-11-18 19:06:23 -05:00
|
|
|
# Creates new Net::HTTP instance for communication with
|
2007-01-16 01:34:10 -05:00
|
|
|
# remote service and resources.
|
2006-06-25 10:44:22 -04:00
|
|
|
def http
|
2007-11-18 19:06:23 -05:00
|
|
|
http = Net::HTTP.new(@site.host, @site.port)
|
|
|
|
http.use_ssl = @site.is_a?(URI::HTTPS)
|
|
|
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl
|
|
|
|
http
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
2007-09-20 19:18:05 -04:00
|
|
|
|
|
|
|
def default_header
|
|
|
|
@default_header ||= { 'Content-Type' => format.mime_type }
|
|
|
|
end
|
2006-09-29 12:25:49 -04:00
|
|
|
|
2007-01-16 01:34:10 -05:00
|
|
|
# Builds headers for request to remote service.
|
2007-04-28 23:14:36 -04:00
|
|
|
def build_request_headers(headers)
|
2007-09-20 19:18:05 -04:00
|
|
|
authorization_header.update(default_header).update(headers)
|
2006-09-29 12:25:49 -04:00
|
|
|
end
|
|
|
|
|
2007-01-16 01:34:10 -05:00
|
|
|
# Sets authorization header; authentication information is pulled from credentials provided with site URI.
|
2006-09-29 12:25:49 -04:00
|
|
|
def authorization_header
|
|
|
|
(@site.user || @site.password ? { 'Authorization' => 'Basic ' + ["#{@site.user}:#{ @site.password}"].pack('m').delete("\r\n") } : {})
|
|
|
|
end
|
2006-10-23 18:21:09 -04:00
|
|
|
|
2007-01-16 01:34:10 -05:00
|
|
|
def logger #:nodoc:
|
2006-10-23 18:21:09 -04:00
|
|
|
ActiveResource::Base.logger
|
|
|
|
end
|
2006-06-25 10:44:22 -04:00
|
|
|
end
|
2007-11-18 19:06:23 -05:00
|
|
|
end
|