aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource/lib/active_resource
diff options
context:
space:
mode:
Diffstat (limited to 'activeresource/lib/active_resource')
-rw-r--r--activeresource/lib/active_resource/base.rb104
-rw-r--r--activeresource/lib/active_resource/connection.rb12
-rw-r--r--activeresource/lib/active_resource/custom_methods.rb4
-rw-r--r--activeresource/lib/active_resource/exceptions.rb3
-rw-r--r--activeresource/lib/active_resource/http_mock.rb129
-rw-r--r--activeresource/lib/active_resource/schema.rb4
-rw-r--r--activeresource/lib/active_resource/validations.rb30
-rw-r--r--activeresource/lib/active_resource/version.rb6
8 files changed, 205 insertions, 87 deletions
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb
index 62420725ad..160763779e 100644
--- a/activeresource/lib/active_resource/base.rb
+++ b/activeresource/lib/active_resource/base.rb
@@ -1,6 +1,6 @@
require 'active_support'
require 'active_support/core_ext/class/attribute_accessors'
-require 'active_support/core_ext/class/inheritable_attributes'
+require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/hash/indifferent_access'
require 'active_support/core_ext/kernel/reporting'
require 'active_support/core_ext/module/attr_accessor_with_default'
@@ -12,6 +12,7 @@ require 'active_support/core_ext/object/duplicable'
require 'set'
require 'uri'
+require 'active_support/core_ext/uri'
require 'active_resource/exceptions'
require 'active_resource/connection'
require 'active_resource/formats'
@@ -21,7 +22,7 @@ require 'active_resource/log_subscriber'
module ActiveResource
# 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.
+ # For an outline of what Active Resource is capable of, see its {README}[link:files/activeresource/README_rdoc.html].
#
# == Automated mapping
#
@@ -35,7 +36,7 @@ module ActiveResource
# end
#
# Now the Person class is mapped to RESTful resources located at <tt>http://api.people.com:3000/people/</tt>, and
- # you can now use Active Resource's lifecycle methods to manipulate resources. In the case where you already have
+ # you can now use Active Resource's life cycle methods to manipulate resources. In the case where you already have
# an existing model with the same name as the desired RESTful resource you can set the +element_name+ value.
#
# class PersonResource < ActiveResource::Base
@@ -51,7 +52,7 @@ module ActiveResource
# end
#
#
- # == Lifecycle methods
+ # == Life cycle methods
#
# Active Resource exposes methods for creating, finding, updating, and deleting resources
# from REST web services.
@@ -70,12 +71,12 @@ module ActiveResource
#
# ryan.destroy # => true
#
- # As you can see, these are very similar to Active Record's lifecycle methods for database records.
+ # As you can see, these are very similar to Active Record's life cycle methods for database records.
# You can read more about each of these methods in their respective documentation.
#
# === Custom REST methods
#
- # Since simple CRUD/lifecycle methods can't accomplish every task, Active Resource also supports
+ # Since simple CRUD/life cycle methods can't accomplish every task, Active Resource also supports
# defining your own custom REST methods. To invoke them, Active Resource provides the <tt>get</tt>,
# <tt>post</tt>, <tt>put</tt> and <tt>\delete</tt> methods where you can specify a custom REST method
# name to invoke.
@@ -166,6 +167,7 @@ module ActiveResource
# # GET http://api.people.com:3000/people/999.xml
# ryan = Person.find(999) # 404, raises ActiveResource::ResourceNotFound
#
+ #
# <tt>404</tt> is just one of the HTTP error response codes that Active Resource will handle with its own exception. The
# following HTTP response codes will also result in these exceptions:
#
@@ -194,6 +196,16 @@ module ActiveResource
# redirect_to :action => 'new'
# end
#
+ # When a GET is requested for a nested resource and you don't provide the prefix_param
+ # an ActiveResource::MissingPrefixParam will be raised.
+ #
+ # class Comment < ActiveResource::Base
+ # self.site = "http://someip.com/posts/:post_id/"
+ # end
+ #
+ # Comment.find(1)
+ # # => ActiveResource::MissingPrefixParam: post_id prefix_option is missing
+ #
# === Validation errors
#
# Active Resource supports validations on resources and will return errors if any of these validations fail
@@ -251,6 +263,8 @@ module ActiveResource
# The logger for diagnosing and tracing Active Resource calls.
cattr_accessor :logger
+ class_attribute :_format
+
class << self
# Creates a schema for this resource - setting the attributes that are
# known prior to fetching an instance from the remote system.
@@ -403,8 +417,8 @@ module ActiveResource
@site = nil
else
@site = create_site_uri_from(site)
- @user = uri_parser.unescape(@site.user) if @site.user
- @password = uri_parser.unescape(@site.password) if @site.password
+ @user = URI.parser.unescape(@site.user) if @site.user
+ @password = URI.parser.unescape(@site.password) if @site.password
end
end
@@ -480,13 +494,13 @@ module ActiveResource
format = mime_type_reference_or_format.is_a?(Symbol) ?
ActiveResource::Formats[mime_type_reference_or_format] : mime_type_reference_or_format
- write_inheritable_attribute(:format, format)
+ self._format = format
connection.format = format if site
end
# Returns the current format, default is ActiveResource::Formats::XmlFormat.
def format
- read_inheritable_attribute(:format) || ActiveResource::Formats::XmlFormat
+ self._format || ActiveResource::Formats::XmlFormat
end
# Sets the number of seconds after which requests to the REST API should time out.
@@ -508,9 +522,9 @@ module ActiveResource
#
# * <tt>:key</tt> - An OpenSSL::PKey::RSA or OpenSSL::PKey::DSA object.
# * <tt>:cert</tt> - An OpenSSL::X509::Certificate object as client certificate
- # * <tt>:ca_file</tt> - Path to a CA certification file in PEM format. The file can contrain several CA certificates.
+ # * <tt>:ca_file</tt> - Path to a CA certification file in PEM format. The file can contain several CA certificates.
# * <tt>:ca_path</tt> - Path of a CA certification directory containing certifications in PEM format.
- # * <tt>:verify_mode</tt> - Flags for server the certification verification at begining of SSL/TLS session. (OpenSSL::SSL::VERIFY_NONE or OpenSSL::SSL::VERIFY_PEER is acceptable)
+ # * <tt>:verify_mode</tt> - Flags for server the certification verification at beginning of SSL/TLS session. (OpenSSL::SSL::VERIFY_NONE or OpenSSL::SSL::VERIFY_PEER is acceptable)
# * <tt>:verify_callback</tt> - The verify callback for the server certification verification.
# * <tt>:verify_depth</tt> - The maximum depth for the certificate chain verification.
# * <tt>:cert_store</tt> - OpenSSL::X509::Store to verify peer certificate.
@@ -577,7 +591,7 @@ module ActiveResource
# Default value is <tt>site.path</tt>.
def prefix=(value = '/')
# Replace :placeholders with '#{embedded options[:lookups]}'
- prefix_call = value.gsub(/:\w+/) { |key| "\#{URI.escape options[#{key}].to_s}" }
+ prefix_call = value.gsub(/:\w+/) { |key| "\#{URI.parser.escape options[#{key}].to_s}" }
# Clear prefix parameters in case they have been cached
@prefix_parameters = nil
@@ -589,8 +603,8 @@ module ActiveResource
def prefix(options={}) "#{prefix_call}" end
RUBY_EVAL
end
- rescue
- logger.error "Couldn't set prefix: #{$!}\n #{code}" if logger
+ rescue Exception => e
+ logger.error "Couldn't set prefix: #{e}\n #{code}" if logger
raise
end
@@ -621,8 +635,10 @@ module ActiveResource
# # => /posts/5/comments/1.xml?active=1
#
def element_path(id, prefix_options = {}, query_options = nil)
+ check_prefix_options(prefix_options)
+
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
- "#{prefix(prefix_options)}#{collection_name}/#{URI.escape id.to_s}.#{format.extension}#{query_string(query_options)}"
+ "#{prefix(prefix_options)}#{collection_name}/#{URI.parser.escape id.to_s}.#{format.extension}#{query_string(query_options)}"
end
# Gets the new element path for REST resources.
@@ -663,6 +679,7 @@ module ActiveResource
# # => /posts/5/comments.xml?active=1
#
def collection_path(prefix_options = {}, query_options = nil)
+ check_prefix_options(prefix_options)
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
"#{prefix(prefix_options)}#{collection_name}.#{format.extension}#{query_string(query_options)}"
end
@@ -678,7 +695,7 @@ module ActiveResource
# Returns the new resource instance.
#
def build(attributes = {})
- attrs = connection.get("#{new_element_path}").merge(attributes)
+ attrs = self.format.decode(connection.get("#{new_element_path}").body).merge(attributes)
self.new(attrs)
end
@@ -842,6 +859,14 @@ module ActiveResource
end
private
+
+ def check_prefix_options(prefix_options)
+ p_options = HashWithIndifferentAccess.new(prefix_options)
+ prefix_parameters.each do |p|
+ raise(MissingPrefixParam, "#{p} prefix_option is missing") if p_options[p].blank?
+ end
+ end
+
# Find every resource
def find_every(options)
begin
@@ -850,11 +875,11 @@ module ActiveResource
instantiate_collection(get(from, options[:params]))
when String
path = "#{from}#{query_string(options[:params])}"
- instantiate_collection(connection.get(path, headers) || [])
+ instantiate_collection(format.decode(connection.get(path, headers).body) || [])
else
prefix_options, query_options = split_options(options[:params])
path = collection_path(prefix_options, query_options)
- instantiate_collection( (connection.get(path, headers) || []), prefix_options )
+ instantiate_collection( (format.decode(connection.get(path, headers).body) || []), prefix_options )
end
rescue ActiveResource::ResourceNotFound
# Swallowing ResourceNotFound exceptions and return nil - as per
@@ -870,7 +895,7 @@ module ActiveResource
instantiate_record(get(from, options[:params]))
when String
path = "#{from}#{query_string(options[:params])}"
- instantiate_record(connection.get(path, headers))
+ instantiate_record(format.decode(connection.get(path, headers).body))
end
end
@@ -878,7 +903,7 @@ module ActiveResource
def find_single(scope, options)
prefix_options, query_options = split_options(options[:params])
path = element_path(scope, prefix_options, query_options)
- instantiate_record(connection.get(path, headers), prefix_options)
+ instantiate_record(format.decode(connection.get(path, headers).body), prefix_options)
end
def instantiate_collection(collection, prefix_options = {})
@@ -886,7 +911,7 @@ module ActiveResource
end
def instantiate_record(record, prefix_options = {})
- new(record).tap do |resource|
+ new(record, true).tap do |resource|
resource.prefix_options = prefix_options
end
end
@@ -894,12 +919,12 @@ module ActiveResource
# Accepts a URI and creates the site URI from that.
def create_site_uri_from(site)
- site.is_a?(URI) ? site.dup : uri_parser.parse(site)
+ site.is_a?(URI) ? site.dup : URI.parser.parse(site)
end
# Accepts a URI and creates the proxy URI from that.
def create_proxy_uri_from(proxy)
- proxy.is_a?(URI) ? proxy.dup : uri_parser.parse(proxy)
+ proxy.is_a?(URI) ? proxy.dup : URI.parser.parse(proxy)
end
# contains a set of the current prefix parameters.
@@ -924,10 +949,6 @@ module ActiveResource
[ prefix_options, query_options ]
end
-
- def uri_parser
- @uri_parser ||= URI.const_defined?(:Parser) ? URI::Parser.new : URI
- end
end
attr_accessor :attributes #:nodoc:
@@ -959,9 +980,10 @@ module ActiveResource
#
# my_other_course = Course.new(:name => "Philosophy: Reason and Being", :lecturer => "Ralph Cling")
# my_other_course.save
- def initialize(attributes = {})
+ def initialize(attributes = {}, persisted = false)
@attributes = {}.with_indifferent_access
@prefix_options = {}
+ @persisted = persisted
load(attributes)
end
@@ -987,10 +1009,7 @@ module ActiveResource
# not_ryan.hash # => {:not => "an ARes instance"}
def clone
# Clone all attributes except the pk and any nested ARes
- cloned = attributes.reject {|k,v| k == self.class.primary_key || v.is_a?(ActiveResource::Base)}.inject({}) do |attrs, (k, v)|
- attrs[k] = v.clone
- attrs
- end
+ cloned = Hash[attributes.reject {|k,v| k == self.class.primary_key || v.is_a?(ActiveResource::Base)}.map { |k, v| [k, v.clone] }]
# Form the new resource - bypass initialize of resource with 'new' as that will call 'load' which
# attempts to convert hashes into member objects and arrays into collections of objects. We want
# the raw objects to be cloned so we bypass load by directly setting the attributes hash.
@@ -1014,7 +1033,7 @@ module ActiveResource
# is_new.new? # => false
#
def new?
- id.nil?
+ !persisted?
end
alias :new_record? :new?
@@ -1031,7 +1050,7 @@ module ActiveResource
# not_persisted.persisted? # => true
#
def persisted?
- !new?
+ @persisted
end
# Gets the <tt>\id</tt> attribute of the resource.
@@ -1076,7 +1095,7 @@ module ActiveResource
end
# Delegates to id in order to allow two resources of the same type and \id to work with something like:
- # [Person.find(1), Person.find(2)] & [Person.find(1), Person.find(4)] # => [Person.find(1)]
+ # [(a = Person.find 1), (b = Person.find 2)] & [(c = Person.find 1), (d = Person.find 4)] # => [a]
def hash
id.hash
end
@@ -1318,8 +1337,9 @@ module ActiveResource
end
def load_attributes_from_response(response)
- if response['Content-Length'] != "0" && response.body.strip.size > 0
+ if !response['Content-Length'].blank? && response['Content-Length'] != "0" && !response.body.nil? && response.body.strip.size > 0
load(self.class.format.decode(response.body))
+ @persisted = true
end
end
@@ -1353,8 +1373,9 @@ module ActiveResource
namespaces = module_names[0, module_names.size-1].map do |module_name|
receiver = receiver.const_get(module_name)
end
- if namespace = namespaces.reverse.detect { |ns| ns.const_defined?(resource_name) }
- return namespace.const_get(resource_name)
+ const_args = RUBY_VERSION < "1.9" ? [resource_name] : [resource_name, false]
+ if namespace = namespaces.reverse.detect { |ns| ns.const_defined?(*const_args) }
+ return namespace.const_get(*const_args)
else
raise NameError
end
@@ -1370,8 +1391,9 @@ module ActiveResource
self.class.const_get(resource_name)
end
rescue NameError
- if self.class.const_defined?(resource_name)
- resource = self.class.const_get(resource_name)
+ const_args = RUBY_VERSION < "1.9" ? [resource_name] : [resource_name, false]
+ if self.class.const_defined?(*const_args)
+ resource = self.class.const_get(*const_args)
else
resource = self.class.const_set(resource_name, Class.new(ActiveResource::Base))
end
diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb
index b7befe110d..480f2fbecb 100644
--- a/activeresource/lib/active_resource/connection.rb
+++ b/activeresource/lib/active_resource/connection.rb
@@ -1,4 +1,5 @@
require 'active_support/core_ext/benchmark'
+require 'active_support/core_ext/uri'
require 'net/https'
require 'date'
require 'time'
@@ -31,21 +32,20 @@ module ActiveResource
def initialize(site, format = ActiveResource::Formats::XmlFormat)
raise ArgumentError, 'Missing site URI' unless site
@user = @password = nil
- @uri_parser = URI.const_defined?(:Parser) ? URI::Parser.new : URI
self.site = site
self.format = format
end
# Set URI for remote service.
def site=(site)
- @site = site.is_a?(URI) ? site : @uri_parser.parse(site)
- @user = @uri_parser.unescape(@site.user) if @site.user
- @password = @uri_parser.unescape(@site.password) if @site.password
+ @site = site.is_a?(URI) ? site : URI.parser.parse(site)
+ @user = URI.parser.unescape(@site.user) if @site.user
+ @password = URI.parser.unescape(@site.password) if @site.password
end
# Set the proxy for remote service.
def proxy=(proxy)
- @proxy = proxy.is_a?(URI) ? proxy : @uri_parser.parse(proxy)
+ @proxy = proxy.is_a?(URI) ? proxy : URI.parser.parse(proxy)
end
# Sets the user for remote service.
@@ -76,7 +76,7 @@ module ActiveResource
# Executes a GET request.
# Used to get (find) resources.
def get(path, headers = {})
- with_auth { format.decode(request(:get, path, build_request_headers(headers, :get, self.site.merge(path))).body) }
+ with_auth { request(:get, path, build_request_headers(headers, :get, self.site.merge(path))) }
end
# Executes a DELETE request (see HTTP protocol documentation if unfamiliar).
diff --git a/activeresource/lib/active_resource/custom_methods.rb b/activeresource/lib/active_resource/custom_methods.rb
index dd3e35dfc7..9879f8cded 100644
--- a/activeresource/lib/active_resource/custom_methods.rb
+++ b/activeresource/lib/active_resource/custom_methods.rb
@@ -54,7 +54,7 @@ module ActiveResource
#
# Person.find(:all, :from => :active)
def get(custom_method_name, options = {})
- connection.get(custom_method_collection_url(custom_method_name, options), headers)
+ format.decode(connection.get(custom_method_collection_url(custom_method_name, options), headers).body)
end
def post(custom_method_name, options = {}, body = '')
@@ -85,7 +85,7 @@ module ActiveResource
module InstanceMethods
def get(method_name, options = {})
- connection.get(custom_method_element_url(method_name, options), self.class.headers)
+ self.class.format.decode(connection.get(custom_method_element_url(method_name, options), self.class.headers).body)
end
def post(method_name, options = {}, body = nil)
diff --git a/activeresource/lib/active_resource/exceptions.rb b/activeresource/lib/active_resource/exceptions.rb
index 0f4549fd73..6b953b28ad 100644
--- a/activeresource/lib/active_resource/exceptions.rb
+++ b/activeresource/lib/active_resource/exceptions.rb
@@ -36,6 +36,9 @@ module ActiveResource
def to_s; response['Location'] ? "#{super} => #{response['Location']}" : super; end
end
+ # Raised when ...
+ class MissingPrefixParam < ArgumentError; end # :nodoc:
+
# 4xx Client Error
class ClientError < ConnectionError; end # :nodoc:
diff --git a/activeresource/lib/active_resource/http_mock.rb b/activeresource/lib/active_resource/http_mock.rb
index 75425c01c0..75649053d0 100644
--- a/activeresource/lib/active_resource/http_mock.rb
+++ b/activeresource/lib/active_resource/http_mock.rb
@@ -29,7 +29,8 @@ module ActiveResource
#
# In order for a mock to deliver its content, the incoming request must match by the <tt>http_method</tt>,
# +path+ and <tt>request_headers</tt>. If no match is found an InvalidRequestError exception
- # will be raised letting you know you need to create a new mock for that request.
+ # will be raised showing you what request it could not find a response for and also what requests and response
+ # pairs have been recorded so you can create a new mock for that request.
#
# ==== Example
# def setup
@@ -41,7 +42,7 @@ module ActiveResource
# mock.delete "/people/1.xml", {}, nil, 200
# end
# end
- #
+ #
# def test_get_matz
# person = Person.find(1)
# assert_equal "Matz", person.name
@@ -59,10 +60,21 @@ module ActiveResource
# end
module_eval <<-EOE, __FILE__, __LINE__ + 1
def #{method}(path, request_headers = {}, body = nil, status = 200, response_headers = {})
- @responses << [Request.new(:#{method}, path, nil, request_headers), Response.new(body || "", status, response_headers)]
+ request = Request.new(:#{method}, path, nil, request_headers)
+ response = Response.new(body || "", status, response_headers)
+
+ delete_duplicate_responses(request)
+
+ @responses << [request, response]
end
EOE
end
+
+ private
+
+ def delete_duplicate_responses(request)
+ @responses.delete_if {|r| r[0] == request }
+ end
end
class << self
@@ -77,13 +89,13 @@ module ActiveResource
# mock.get "/people/1.xml", {}, @matz
# end
# end
- #
+ #
# def test_should_request_remote_service
# person = Person.find(1) # Call the remote service
- #
+ #
# # This request object has the same HTTP method and path as declared by the mock
# expected_request = ActiveResource::Request.new(:get, "/people/1.xml")
- #
+ #
# # Assert that the mock received, and responded to, the expected request from the model
# assert ActiveResource::HttpMock.requests.include?(expected_request)
# end
@@ -97,18 +109,105 @@ module ActiveResource
@@responses ||= []
end
- # Accepts a block which declares a set of requests and responses for the HttpMock to respond to. See the main
- # ActiveResource::HttpMock description for a more detailed explanation.
- def respond_to(pairs = {}) #:yields: mock
- reset!
- responses.concat pairs.to_a
+ # Accepts a block which declares a set of requests and responses for the HttpMock to respond to in
+ # the following format:
+ #
+ # mock.http_method(path, request_headers = {}, body = nil, status = 200, response_headers = {})
+ #
+ # === Example
+ #
+ # @matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
+ # ActiveResource::HttpMock.respond_to do |mock|
+ # mock.post "/people.xml", {}, @matz, 201, "Location" => "/people/1.xml"
+ # mock.get "/people/1.xml", {}, @matz
+ # mock.put "/people/1.xml", {}, nil, 204
+ # mock.delete "/people/1.xml", {}, nil, 200
+ # end
+ #
+ # Alternatively, accepts a hash of <tt>{Request => Response}</tt> pairs allowing you to generate
+ # these the following format:
+ #
+ # ActiveResource::Request.new(method, path, body, request_headers)
+ # ActiveResource::Response.new(body, status, response_headers)
+ #
+ # === Example
+ #
+ # Request.new(:#{method}, path, nil, request_headers)
+ #
+ # @matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
+ #
+ # create_matz = ActiveResource::Request.new(:post, '/people.xml', @matz, {})
+ # created_response = ActiveResource::Response.new("", 201, {"Location" => "/people/1.xml"})
+ # get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil)
+ # ok_response = ActiveResource::Response.new("", 200, {})
+ #
+ # pairs = {create_matz => created_response, get_matz => ok_response}
+ #
+ # ActiveResource::HttpMock.respond_to(pairs)
+ #
+ # Note, by default, every time you call +respond_to+, any previous request and response pairs stored
+ # in HttpMock will be deleted giving you a clean slate to work on.
+ #
+ # If you want to override this behaviour, pass in +false+ as the last argument to +respond_to+
+ #
+ # === Example
+ #
+ # ActiveResource::HttpMock.respond_to do |mock|
+ # mock.send(:get, "/people/1", {}, "XML1")
+ # end
+ # ActiveResource::HttpMock.responses.length #=> 1
+ #
+ # ActiveResource::HttpMock.respond_to(false) do |mock|
+ # mock.send(:get, "/people/2", {}, "XML2")
+ # end
+ # ActiveResource::HttpMock.responses.length #=> 2
+ #
+ # This also works with passing in generated pairs of requests and responses, again, just pass in false
+ # as the last argument:
+ #
+ # === Example
+ #
+ # ActiveResource::HttpMock.respond_to do |mock|
+ # mock.send(:get, "/people/1", {}, "XML1")
+ # end
+ # ActiveResource::HttpMock.responses.length #=> 1
+ #
+ # get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil)
+ # ok_response = ActiveResource::Response.new("", 200, {})
+ #
+ # pairs = {get_matz => ok_response}
+ #
+ # ActiveResource::HttpMock.respond_to(pairs, false)
+ # ActiveResource::HttpMock.responses.length #=> 2
+ #
+ # # If you add a response with an existing request, it will be replaced
+ #
+ # fail_response = ActiveResource::Response.new("", 404, {})
+ # pairs = {get_matz => fail_response}
+ #
+ # ActiveResource::HttpMock.respond_to(pairs, false)
+ # ActiveResource::HttpMock.responses.length #=> 2
+ #
+ def respond_to(*args) #:yields: mock
+ pairs = args.first || {}
+ reset! if args.last.class != FalseClass
+
if block_given?
yield Responder.new(responses)
else
+ delete_responses_to_replace pairs.to_a
+ responses.concat pairs.to_a
Responder.new(responses)
end
end
+ def delete_responses_to_replace(new_responses)
+ new_responses.each{|nr|
+ request_to_remove = nr[0]
+ @@responses = responses.delete_if{|r| r[0] == request_to_remove}
+ }
+ end
+
# Deletes all logged requests and responses.
def reset!
requests.clear
@@ -126,7 +225,7 @@ module ActiveResource
# if response = self.class.responses.assoc(request)
# response[1]
# else
- # raise InvalidRequestError.new("No response recorded for #{request}")
+ # raise InvalidRequestError.new("Could not find a response recorded for #{request.to_s} - Responses recorded are: - #{inspect_responses}")
# end
# end
module_eval <<-EOE, __FILE__, __LINE__ + 1
@@ -136,7 +235,7 @@ module ActiveResource
if response = self.class.responses.assoc(request)
response[1]
else
- raise InvalidRequestError.new("No response recorded for \#{request}")
+ raise InvalidRequestError.new("Could not find a response recorded for \#{request.to_s} - Responses recorded are: \#{inspect_responses}")
end
end
EOE
@@ -146,6 +245,10 @@ module ActiveResource
def initialize(site) #:nodoc:
@site = site
end
+
+ def inspect_responses #:nodoc:
+ self.class.responses.map { |r| r[0].to_s }.inspect
+ end
end
class Request
diff --git a/activeresource/lib/active_resource/schema.rb b/activeresource/lib/active_resource/schema.rb
index 5758ac9502..3fd37a9bb6 100644
--- a/activeresource/lib/active_resource/schema.rb
+++ b/activeresource/lib/active_resource/schema.rb
@@ -4,7 +4,7 @@ module ActiveResource # :nodoc:
class Schema # :nodoc:
# attributes can be known to be one of these types. They are easy to
# cast to/from.
- KNOWN_ATTRIBUTE_TYPES = %w( string integer float )
+ KNOWN_ATTRIBUTE_TYPES = %w( string text integer float decimal datetime timestamp time date binary boolean )
# An array of attribute definitions, representing the attributes that
# have been defined.
@@ -39,8 +39,6 @@ module ActiveResource # :nodoc:
# The following are the attribute types supported by Active Resource
# migrations.
- # TODO: We should eventually support all of these:
- # %w( string text integer float decimal datetime timestamp time date binary boolean ).each do |attr_type|
KNOWN_ATTRIBUTE_TYPES.each do |attr_type|
class_eval <<-EOV, __FILE__, __LINE__ + 1
def #{attr_type.to_s}(*args)
diff --git a/activeresource/lib/active_resource/validations.rb b/activeresource/lib/active_resource/validations.rb
index 026d81e44a..a373e53f11 100644
--- a/activeresource/lib/active_resource/validations.rb
+++ b/activeresource/lib/active_resource/validations.rb
@@ -6,21 +6,21 @@ module ActiveResource
end
# Active Resource validation is reported to and from this object, which is used by Base#save
- # to determine whether the object in a valid state to be saved. See usage example in Validations.
+ # to determine whether the object in a valid state to be saved. See usage example in Validations.
class Errors < ActiveModel::Errors
# Grabs errors from an array of messages (like ActiveRecord::Validations)
# The second parameter directs the errors cache to be cleared (default)
# or not (by passing true)
def from_array(messages, save_cache = false)
clear unless save_cache
- humanized_attributes = @base.attributes.keys.inject({}) { |h, attr_name| h.update(attr_name.humanize => attr_name) }
+ humanized_attributes = Hash[@base.attributes.keys.map { |attr_name| [attr_name.humanize, attr_name] }]
messages.each do |message|
attr_message = humanized_attributes.keys.detect do |attr_name|
if message[0, attr_name.size + 1] == "#{attr_name} "
add humanized_attributes[attr_name], message[(attr_name.size + 1)..-1]
end
end
-
+
self[:base] << message if attr_message.nil?
end
end
@@ -37,15 +37,15 @@ module ActiveResource
from_array array, save_cache
end
end
-
+
# Module to support validation and errors with Active Resource objects. The module overrides
- # Base#save to rescue ActiveResource::ResourceInvalid exceptions and parse the errors returned
- # in the web service response. The module also adds an +errors+ collection that mimics the interface
+ # Base#save to rescue ActiveResource::ResourceInvalid exceptions and parse the errors returned
+ # in the web service response. The module also adds an +errors+ collection that mimics the interface
# of the errors provided by ActiveRecord::Errors.
#
# ==== Example
#
- # Consider a Person resource on the server requiring both a +first_name+ and a +last_name+ with a
+ # Consider a Person resource on the server requiring both a +first_name+ and a +last_name+ with a
# <tt>validates_presence_of :first_name, :last_name</tt> declaration in the model:
#
# person = Person.new(:first_name => "Jim", :last_name => "")
@@ -55,7 +55,7 @@ module ActiveResource
# person.errors.count # => 1
# person.errors.full_messages # => ["Last name can't be empty"]
# person.errors[:last_name] # => ["can't be empty"]
- # person.last_name = "Halpert"
+ # person.last_name = "Halpert"
# person.save # => true (and person is now saved to the remote service)
#
module Validations
@@ -68,16 +68,8 @@ module ActiveResource
# Validate a resource and save (POST) it to the remote web service.
# If any local validations fail - the save (POST) will not be attempted.
- def save_with_validation(options=nil)
- perform_validation = case options
- when Hash
- options[:validate] != false
- when NilClass
- true
- else
- ActiveSupport::Deprecation.warn "save(#{options}) is deprecated, please give save(:validate => #{options}) instead", caller
- options
- end
+ def save_with_validation(options={})
+ perform_validation = options[:validate] != false
# clear the remote validations so they don't interfere with the local
# ones. Otherwise we get an endless loop and can never change the
@@ -118,7 +110,7 @@ module ActiveResource
# also any errors returned from the remote system the last time we
# saved.
# Remote errors can only be cleared by trying to re-save the resource.
- #
+ #
# ==== Examples
# my_person = Person.create(params[:person])
# my_person.valid?
diff --git a/activeresource/lib/active_resource/version.rb b/activeresource/lib/active_resource/version.rb
index 43c00e9cf1..82dcb5d575 100644
--- a/activeresource/lib/active_resource/version.rb
+++ b/activeresource/lib/active_resource/version.rb
@@ -1,10 +1,10 @@
module ActiveResource
module VERSION #:nodoc:
MAJOR = 3
- MINOR = 0
+ MINOR = 1
TINY = 0
- BUILD = "rc"
+ PRE = "beta"
- STRING = [MAJOR, MINOR, TINY, BUILD].join('.')
+ STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
end
end