From 6d4a4fabbbb04c20cee51c4e374045cc75e2ec16 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 6 May 2009 00:14:55 -0700 Subject: Cherry pick Active Support dependencies. Autoload. --- activeresource/lib/active_resource.rb | 11 +--- activeresource/lib/active_resource/base.rb | 16 ++++-- activeresource/lib/active_resource/connection.rb | 60 ++-------------------- .../lib/active_resource/custom_methods.rb | 4 ++ activeresource/lib/active_resource/exceptions.rb | 55 ++++++++++++++++++++ activeresource/lib/active_resource/formats.rb | 8 +-- .../lib/active_resource/formats/xml_format.rb | 2 + activeresource/lib/active_resource/http_mock.rb | 7 ++- activeresource/lib/active_resource/validations.rb | 7 +++ activeresource/test/abstract_unit.rb | 9 ++-- activeresource/test/base_test.rb | 2 +- 11 files changed, 100 insertions(+), 81 deletions(-) create mode 100644 activeresource/lib/active_resource/exceptions.rb diff --git a/activeresource/lib/active_resource.rb b/activeresource/lib/active_resource.rb index 7a3faf445a..720abee72e 100644 --- a/activeresource/lib/active_resource.rb +++ b/activeresource/lib/active_resource.rb @@ -24,16 +24,7 @@ activesupport_path = "#{File.dirname(__FILE__)}/../../activesupport/lib" $:.unshift(activesupport_path) if File.directory?(activesupport_path) require 'active_support' -require 'active_support/core/all' - -require 'active_resource/formats' -require 'active_resource/base' -require 'active_resource/validations' -require 'active_resource/custom_methods' module ActiveResource - Base.class_eval do - include Validations - include CustomMethods - end + autoload :Base, 'active_resource/base' end diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 590d4f6232..be022f5c44 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -1,7 +1,14 @@ -require 'active_resource/connection' +require 'active_support/core_ext/class/attribute_accessors' +require 'active_support/core_ext/class/inheritable_attributes' +require 'active_support/core_ext/module/attr_accessor_with_default' +require 'active_support/core_ext/module/delegation' +require 'active_support/core_ext/module/aliasing' require 'set' module ActiveResource + autoload :Formats, 'active_resource/formats' + autoload :Connection, 'active_resource/connection' + # ActiveResource::Base is the main class for mapping RESTful resources as models in a Rails application. # # For an outline of what Active Resource is capable of, see link:files/vendor/rails/activeresource/README.html. @@ -297,7 +304,7 @@ module ActiveResource # Returns the current format, default is ActiveResource::Formats::XmlFormat. def format - read_inheritable_attribute(:format) || ActiveResource::Formats[:xml] + read_inheritable_attribute(:format) || ActiveResource::Formats::XmlFormat end # Sets the number of seconds after which requests to the REST API should time out. @@ -894,7 +901,7 @@ module ActiveResource # applicable depend on the configured encoding format. def encode(options={}) case self.class.format - when ActiveResource::Formats[:xml] + when ActiveResource::Formats::XmlFormat self.class.format.encode(attributes, {:root => self.class.element_name}.merge(options)) else self.class.format.encode(attributes, options) @@ -1079,3 +1086,6 @@ module ActiveResource end end end + +require 'active_resource/validations' +require 'active_resource/custom_methods' diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb index 80d5c95b68..6661469c5b 100644 --- a/activeresource/lib/active_resource/connection.rb +++ b/activeresource/lib/active_resource/connection.rb @@ -1,64 +1,12 @@ +require 'active_resource/exceptions' +require 'active_resource/formats' +require 'active_support/core_ext/benchmark' require 'net/https' require 'date' require 'time' require 'uri' -require 'benchmark' module ActiveResource - class ConnectionError < StandardError # :nodoc: - attr_reader :response - - def initialize(response, message = nil) - @response = response - @message = message - end - - def to_s - "Failed with #{response.code} #{response.message if response.respond_to?(:message)}" - end - end - - # Raised when a Timeout::Error occurs. - class TimeoutError < ConnectionError - def initialize(message) - @message = message - end - def to_s; @message ;end - end - - # 3xx Redirection - class Redirection < ConnectionError # :nodoc: - def to_s; response['Location'] ? "#{super} => #{response['Location']}" : super; end - end - - # 4xx Client Error - class ClientError < ConnectionError; end # :nodoc: - - # 400 Bad Request - class BadRequest < ClientError; end # :nodoc - - # 401 Unauthorized - class UnauthorizedAccess < ClientError; end # :nodoc - - # 403 Forbidden - class ForbiddenAccess < ClientError; end # :nodoc - - # 404 Not Found - class ResourceNotFound < ClientError; end # :nodoc: - - # 409 Conflict - class ResourceConflict < ClientError; end # :nodoc: - - # 5xx Server Error - class ServerError < ConnectionError; end # :nodoc: - - # 405 Method Not Allowed - class MethodNotAllowed < ClientError # :nodoc: - def allowed_methods - @response['Allow'].split(',').map { |verb| verb.strip.downcase.to_sym } - end - end - # Class to handle connections to remote web services. # This class is used by ActiveResource::Base to interface with REST # services. @@ -81,7 +29,7 @@ module ActiveResource # The +site+ parameter is required and will set the +site+ # attribute to the URI for the remote resource service. - def initialize(site, format = ActiveResource::Formats[:xml]) + def initialize(site, format = ActiveResource::Formats::XmlFormat) raise ArgumentError, 'Missing site URI' unless site @user = @password = nil self.site = site diff --git a/activeresource/lib/active_resource/custom_methods.rb b/activeresource/lib/active_resource/custom_methods.rb index 4647e8342c..0d05d06035 100644 --- a/activeresource/lib/active_resource/custom_methods.rb +++ b/activeresource/lib/active_resource/custom_methods.rb @@ -117,4 +117,8 @@ module ActiveResource end end end + + class Base + include CustomMethods + end end diff --git a/activeresource/lib/active_resource/exceptions.rb b/activeresource/lib/active_resource/exceptions.rb new file mode 100644 index 0000000000..5e4b1d4487 --- /dev/null +++ b/activeresource/lib/active_resource/exceptions.rb @@ -0,0 +1,55 @@ +module ActiveResource + class ConnectionError < StandardError # :nodoc: + attr_reader :response + + def initialize(response, message = nil) + @response = response + @message = message + end + + def to_s + "Failed with #{response.code} #{response.message if response.respond_to?(:message)}" + end + end + + # Raised when a Timeout::Error occurs. + class TimeoutError < ConnectionError + def initialize(message) + @message = message + end + def to_s; @message ;end + end + + # 3xx Redirection + class Redirection < ConnectionError # :nodoc: + def to_s; response['Location'] ? "#{super} => #{response['Location']}" : super; end + end + + # 4xx Client Error + class ClientError < ConnectionError; end # :nodoc: + + # 400 Bad Request + class BadRequest < ClientError; end # :nodoc + + # 401 Unauthorized + class UnauthorizedAccess < ClientError; end # :nodoc + + # 403 Forbidden + class ForbiddenAccess < ClientError; end # :nodoc + + # 404 Not Found + class ResourceNotFound < ClientError; end # :nodoc: + + # 409 Conflict + class ResourceConflict < ClientError; end # :nodoc: + + # 5xx Server Error + class ServerError < ConnectionError; end # :nodoc: + + # 405 Method Not Allowed + class MethodNotAllowed < ClientError # :nodoc: + def allowed_methods + @response['Allow'].split(',').map { |verb| verb.strip.downcase.to_sym } + end + end +end diff --git a/activeresource/lib/active_resource/formats.rb b/activeresource/lib/active_resource/formats.rb index 28864cf588..53b75b34e7 100644 --- a/activeresource/lib/active_resource/formats.rb +++ b/activeresource/lib/active_resource/formats.rb @@ -1,14 +1,14 @@ module ActiveResource module Formats + autoload :XmlFormat, 'active_resource/formats/xml_format' + autoload :JsonFormat, 'active_resource/formats/json_format' + # Lookup the format class from a mime type reference symbol. Example: # # ActiveResource::Formats[:xml] # => ActiveResource::Formats::XmlFormat # ActiveResource::Formats[:json] # => ActiveResource::Formats::JsonFormat def self.[](mime_type_reference) - ActiveResource::Formats.const_get(mime_type_reference.to_s.camelize + "Format") + ActiveResource::Formats.const_get(ActiveSupport::Inflector.camelize(mime_type_reference.to_s) + "Format") end end end - -require 'active_resource/formats/xml_format' -require 'active_resource/formats/json_format' \ No newline at end of file diff --git a/activeresource/lib/active_resource/formats/xml_format.rb b/activeresource/lib/active_resource/formats/xml_format.rb index 86c6cec745..3b2575cfa1 100644 --- a/activeresource/lib/active_resource/formats/xml_format.rb +++ b/activeresource/lib/active_resource/formats/xml_format.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/hash/conversions' + module ActiveResource module Formats module XmlFormat diff --git a/activeresource/lib/active_resource/http_mock.rb b/activeresource/lib/active_resource/http_mock.rb index 7d7e378436..aae2d6508c 100644 --- a/activeresource/lib/active_resource/http_mock.rb +++ b/activeresource/lib/active_resource/http_mock.rb @@ -1,4 +1,5 @@ require 'active_resource/connection' +require 'active_support/core_ext/kernel/reporting' module ActiveResource class InvalidRequestError < StandardError; end #:nodoc: @@ -129,7 +130,11 @@ module ActiveResource def #{method}(path, #{'body, ' if has_body}headers) request = ActiveResource::Request.new(:#{method}, path, #{has_body ? 'body, ' : 'nil, '}headers) self.class.requests << request - self.class.responses.assoc(request).try(:second) || raise(InvalidRequestError.new("No response recorded for \#{request}")) + if response = self.class.responses.assoc(request) + response[1] + else + raise InvalidRequestError.new("No response recorded for \#{request}") + end end EOE end diff --git a/activeresource/lib/active_resource/validations.rb b/activeresource/lib/active_resource/validations.rb index 8d21f8adbb..da6084fe9f 100644 --- a/activeresource/lib/active_resource/validations.rb +++ b/activeresource/lib/active_resource/validations.rb @@ -1,3 +1,6 @@ +require 'active_resource/exceptions' +require 'active_support/core_ext/array/wrap' + module ActiveResource class ResourceInvalid < ClientError #:nodoc: end @@ -272,4 +275,8 @@ module ActiveResource @errors ||= Errors.new(self) end end + + class Base + include Validations + end end diff --git a/activeresource/test/abstract_unit.rb b/activeresource/test/abstract_unit.rb index 0f11ea482a..3398f2dac7 100644 --- a/activeresource/test/abstract_unit.rb +++ b/activeresource/test/abstract_unit.rb @@ -5,19 +5,16 @@ gem 'mocha', '>= 0.9.5' require 'mocha' $:.unshift "#{File.dirname(__FILE__)}/../lib" -$:.unshift "#{File.dirname(__FILE__)}/../../activesupport/lib" require 'active_resource' require 'active_resource/http_mock' $:.unshift "#{File.dirname(__FILE__)}/../test" require 'setter_trap' +require 'logger' ActiveResource::Base.logger = Logger.new("#{File.dirname(__FILE__)}/debug.log") -def uses_gem(gem_name, test_name, version = '> 0') - gem gem_name.to_s, version - require gem_name.to_s - yield +begin + require 'ruby-debug' rescue LoadError - $stderr.puts "Skipping #{test_name} tests. `gem install #{gem_name}` and try again." end diff --git a/activeresource/test/base_test.rb b/activeresource/test/base_test.rb index 6ed6f1a406..a6cef6b2ae 100644 --- a/activeresource/test/base_test.rb +++ b/activeresource/test/base_test.rb @@ -853,7 +853,7 @@ class BaseTest < Test::Unit::TestCase def test_to_xml matz = Person.find(1) xml = matz.encode - assert xml.starts_with?('') + assert xml.include?('') assert xml.include?('Matz') assert xml.include?('1') end -- cgit v1.2.3