aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource/lib
diff options
context:
space:
mode:
Diffstat (limited to 'activeresource/lib')
-rw-r--r--activeresource/lib/active_resource.rb17
-rw-r--r--activeresource/lib/active_resource/base.rb29
-rw-r--r--activeresource/lib/active_resource/connection.rb60
-rw-r--r--activeresource/lib/active_resource/custom_methods.rb4
-rw-r--r--activeresource/lib/active_resource/exceptions.rb55
-rw-r--r--activeresource/lib/active_resource/formats.rb8
-rw-r--r--activeresource/lib/active_resource/formats/json_format.rb2
-rw-r--r--activeresource/lib/active_resource/formats/xml_format.rb2
-rw-r--r--activeresource/lib/active_resource/http_mock.rb7
-rw-r--r--activeresource/lib/active_resource/validations.rb7
10 files changed, 111 insertions, 80 deletions
diff --git a/activeresource/lib/active_resource.rb b/activeresource/lib/active_resource.rb
index f0d30b1624..b46801affc 100644
--- a/activeresource/lib/active_resource.rb
+++ b/activeresource/lib/active_resource.rb
@@ -21,15 +21,9 @@
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++
-begin
- require 'active_support'
-rescue LoadError
- activesupport_path = "#{File.dirname(__FILE__)}/../../activesupport/lib"
- if File.directory?(activesupport_path)
- $:.unshift activesupport_path
- require 'active_support'
- end
-end
+activesupport_path = "#{File.dirname(__FILE__)}/../../activesupport/lib"
+$:.unshift(activesupport_path) if File.directory?(activesupport_path)
+require 'active_support'
begin
require 'active_model'
@@ -44,8 +38,5 @@ 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 6cb5beb789..dc24e713ff 100644
--- a/activeresource/lib/active_resource/base.rb
+++ b/activeresource/lib/active_resource/base.rb
@@ -1,8 +1,17 @@
-require 'active_resource/connection'
-require 'cgi'
+require 'active_support'
+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 'active_support/core_ext/object/blank'
+require 'active_support/core_ext/object/misc'
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.
@@ -298,7 +307,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.
@@ -887,7 +896,7 @@ module ActiveResource
# person.to_json(:except => ["first_name"])
# # => {"last_name": "Smith"}
def to_json(options={})
- attributes.to_json(options)
+ ActiveSupport::JSON.encode(attributes, options)
end
# Returns the serialized string representation of the resource in the configured
@@ -895,7 +904,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)
@@ -1020,7 +1029,7 @@ module ActiveResource
private
# Tries to find a resource for a given collection name; if it fails, then the resource is created
def find_or_create_resource_for_collection(name)
- find_or_create_resource_for(name.to_s.singularize)
+ find_or_create_resource_for(ActiveSupport::Inflector.singularize(name.to_s))
end
# Tries to find a resource in a non empty list of nested modules
@@ -1061,6 +1070,11 @@ module ActiveResource
self.class.__send__(:split_options, options)
end
+ # For compatibility with ActiveSupport::JSON.encode
+ def rails_to_json(options, *args)
+ to_json(options)
+ end
+
def method_missing(method_symbol, *arguments) #:nodoc:
method_name = method_symbol.to_s
@@ -1075,3 +1089,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/json_format.rb b/activeresource/lib/active_resource/formats/json_format.rb
index 1d88fc5f16..101027d99e 100644
--- a/activeresource/lib/active_resource/formats/json_format.rb
+++ b/activeresource/lib/active_resource/formats/json_format.rb
@@ -1,3 +1,5 @@
+require 'active_support/json'
+
module ActiveResource
module Formats
module JsonFormat
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 9d3d45b010..95c5467647 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
@@ -77,4 +80,8 @@ module ActiveResource
@errors ||= Errors.new(self)
end
end
+
+ class Base
+ include Validations
+ end
end