aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2009-05-29 16:06:21 -0500
committerJoshua Peek <josh@joshpeek.com>2009-05-29 16:06:21 -0500
commit69742ca8fa05509f7d7c5512cb6d8e002ecb3ab3 (patch)
tree044c2131cc87d21ee54027511aae2b7f2d2ae26a /activeresource
parent5f3f100ce2d689480da85abc88e5e940cf90189e (diff)
parent5ec2c7dc29b36d85b2658465b8a979deb0529d7e (diff)
downloadrails-69742ca8fa05509f7d7c5512cb6d8e002ecb3ab3.tar.gz
rails-69742ca8fa05509f7d7c5512cb6d8e002ecb3ab3.tar.bz2
rails-69742ca8fa05509f7d7c5512cb6d8e002ecb3ab3.zip
Merge branch 'master' into active_model
Conflicts: activemodel/lib/active_model/core.rb activemodel/test/cases/state_machine/event_test.rb activemodel/test/cases/state_machine/state_transition_test.rb activerecord/lib/active_record/validations.rb activerecord/test/cases/validations/i18n_validation_test.rb activeresource/lib/active_resource.rb activeresource/test/abstract_unit.rb
Diffstat (limited to 'activeresource')
-rw-r--r--activeresource/Rakefile10
-rw-r--r--activeresource/examples/simple.rb16
-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
-rw-r--r--activeresource/test/abstract_unit.rb12
-rw-r--r--activeresource/test/base/custom_methods_test.rb1
-rw-r--r--activeresource/test/base/load_test.rb2
-rw-r--r--activeresource/test/base_test.rb3
16 files changed, 144 insertions, 91 deletions
diff --git a/activeresource/Rakefile b/activeresource/Rakefile
index bf7bbb0201..eb5b1dd1ac 100644
--- a/activeresource/Rakefile
+++ b/activeresource/Rakefile
@@ -4,7 +4,6 @@ require 'rake/testtask'
require 'rake/rdoctask'
require 'rake/packagetask'
require 'rake/gempackagetask'
-require 'rake/contrib/sshpublisher'
require File.join(File.dirname(__FILE__), 'lib', 'active_resource', 'version')
@@ -35,6 +34,13 @@ Rake::TestTask.new { |t|
t.verbose = true
t.warning = true
}
+task :isolated_test do
+ ruby = File.join(*RbConfig::CONFIG.values_at('bindir', 'RUBY_INSTALL_NAME'))
+ activesupport_path = "#{File.dirname(__FILE__)}/../activesupport/lib"
+ Dir.glob("test/**/*_test.rb").all? do |file|
+ system(ruby, "-Ilib:test:#{activesupport_path}", file)
+ end or raise "Failures"
+end
# Generate the RDoc documentation
@@ -117,12 +123,14 @@ end
desc "Publish the beta gem"
task :pgem => [:package] do
+ require 'rake/contrib/sshpublisher'
Rake::SshFilePublisher.new("gems.rubyonrails.org", "/u/sites/gems/gems", "pkg", "#{PKG_FILE_NAME}.gem").upload
`ssh gems.rubyonrails.org '/u/sites/gems/gemupdate.sh'`
end
desc "Publish the API documentation"
task :pdoc => [:rdoc] do
+ require 'rake/contrib/sshpublisher'
Rake::SshDirPublisher.new("wrath.rubyonrails.org", "public_html/ar", "doc").upload
end
diff --git a/activeresource/examples/simple.rb b/activeresource/examples/simple.rb
new file mode 100644
index 0000000000..b20ef61670
--- /dev/null
+++ b/activeresource/examples/simple.rb
@@ -0,0 +1,16 @@
+$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
+require 'active_resource'
+require 'active_resource/http_mock'
+require 'active_support/core_ext/hash/conversions'
+
+ActiveSupport::XmlMini.backend = ENV['XMLMINI'] || 'REXML'
+ActiveResource::HttpMock.respond_to do |mock|
+ mock.get '/people/1.xml', {}, { :id => 1, :name => 'bob' }.to_xml(:root => 'person')
+end
+
+class Person < ActiveResource::Base
+ self.site = 'http://localhost/'
+end
+
+bob = Person.find(1)
+puts bob.inspect
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
diff --git a/activeresource/test/abstract_unit.rb b/activeresource/test/abstract_unit.rb
index ce9371d050..3398f2dac7 100644
--- a/activeresource/test/abstract_unit.rb
+++ b/activeresource/test/abstract_unit.rb
@@ -5,22 +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")
-# Show backtraces for deprecated behavior for quicker cleanup.
-ActiveSupport::Deprecation.debug = true
-
-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/custom_methods_test.rb b/activeresource/test/base/custom_methods_test.rb
index 61887f4ec7..2d81549a65 100644
--- a/activeresource/test/base/custom_methods_test.rb
+++ b/activeresource/test/base/custom_methods_test.rb
@@ -1,6 +1,7 @@
require 'abstract_unit'
require 'fixtures/person'
require 'fixtures/street_address'
+require 'active_support/core_ext/hash/conversions'
class CustomMethodsTest < Test::Unit::TestCase
def setup
diff --git a/activeresource/test/base/load_test.rb b/activeresource/test/base/load_test.rb
index a475fab34e..035bd965c2 100644
--- a/activeresource/test/base/load_test.rb
+++ b/activeresource/test/base/load_test.rb
@@ -1,6 +1,8 @@
require 'abstract_unit'
require "fixtures/person"
require "fixtures/street_address"
+require 'active_support/core_ext/symbol'
+require 'active_support/core_ext/hash/conversions'
module Highrise
class Note < ActiveResource::Base
diff --git a/activeresource/test/base_test.rb b/activeresource/test/base_test.rb
index 6ed6f1a406..82d3b2ae96 100644
--- a/activeresource/test/base_test.rb
+++ b/activeresource/test/base_test.rb
@@ -3,6 +3,7 @@ require "fixtures/person"
require "fixtures/customer"
require "fixtures/street_address"
require "fixtures/beast"
+require 'active_support/core_ext/hash/conversions'
class BaseTest < Test::Unit::TestCase
def setup
@@ -853,7 +854,7 @@ class BaseTest < Test::Unit::TestCase
def test_to_xml
matz = Person.find(1)
xml = matz.encode
- assert xml.starts_with?('<?xml version="1.0" encoding="UTF-8"?>')
+ assert xml.include?('<?xml version="1.0" encoding="UTF-8"?>')
assert xml.include?('<name>Matz</name>')
assert xml.include?('<id type="integer">1</id>')
end