0
@@ -13,43 +13,43 @@ module ActiveResource
0
# to Ruby objects, Active Resource only needs a class name that corresponds to the resource name (e.g., the class
0
# Person maps to the resources people, very similarly to Active Record) and a +site+ value, which holds the
0
# URI of the resources.
0
# class Person < ActiveResource::Base
0
# self.site = "http://api.people.com:3000/"
0
# Now the Person class is mapped to RESTful resources located at <tt>http://api.people.com:3000/people/</tt>, and
0
- # you can now use Active Resource's lifecycles methods to manipulate resources. In the case where you already have
0
+ # you can now use Active Resource's lifecycles methods to manipulate resources. In the case where you already have
0
# an existing model with the same name as the desired RESTful resource you can set the +element_name+ value.
0
# class PersonResource < ActiveResource::Base
0
# self.site = "http://api.people.com:3000/"
0
# self.element_name = "person"
0
# Active Resource exposes methods for creating, finding, updating, and deleting resources
0
# from REST web services.
0
# ryan = Person.new(:first => 'Ryan', :last => 'Daigle')
0
# Person.exists?(ryan.id) # => true
0
# ryan.exists? # => true
0
# ryan = Person.find(1)
0
# # Resource holding our newly created Person object
0
# ryan.first = 'Rizzle'
0
# ryan.destroy # => true
0
# As you can see, these are very similar to Active Record's lifecycle methods for database records.
0
# You can read more about each of these methods in their respective documentation.
0
# === Custom REST methods
0
# Since simple CRUD/lifecycle methods can't accomplish every task, Active Resource also supports
0
@@ -71,14 +71,14 @@ module ActiveResource
0
# # DELETE to 'fire' a person, i.e. DELETE /people/1/fire.xml.
0
# Person.find(1).delete(:fire)
0
# For more information on using custom REST methods, see the
0
# ActiveResource::CustomMethods documentation.
0
# You can validate resources client side by overriding validation methods in the base class.
0
# class Person < ActiveResource::Base
0
# self.site = "http://api.people.com:3000/"
0
@@ -86,19 +86,19 @@ module ActiveResource
0
# errors.add("last", "has invalid characters") unless last =~ /[a-zA-Z]*/
0
# See the ActiveResource::Validations documentation for more information.
0
# Many REST APIs will require authentication, usually in the form of basic
0
# HTTP authentication. Authentication can be specified by:
0
# * putting the credentials in the URL for the +site+ variable.
0
# class Person < ActiveResource::Base
0
# self.site = "http://ryan:password@api.people.com:3000/"
0
# * defining +user+ and/or +password+ variables
0
# class Person < ActiveResource::Base
0
@@ -107,29 +107,29 @@ module ActiveResource
0
# self.password = "password"
0
- # For obvious security reasons, it is probably best if such services are available
0
+ # For obvious security reasons, it is probably best if such services are available
0
- # Note: Some values cannot be provided in the URL passed to site. e.g. email addresses
0
+ # Note: Some values cannot be provided in the URL passed to site. e.g. email addresses
0
# as usernames. In those situations you should use the separate user and password option.
0
# == Errors & Validation
0
# Error handling and validation is handled in much the same manner as you're used to seeing in
0
# Active Record. Both the response code in the HTTP response and the body of the response are used to
0
# indicate that an error occurred.
0
# When a GET is requested for a resource that does not exist, the HTTP <tt>404</tt> (Resource Not Found)
0
# response code will be returned from the server which will raise an ActiveResource::ResourceNotFound
0
# # GET http://api.people.com:3000/people/999.xml
0
# ryan = Person.find(999) # 404, raises ActiveResource::ResourceNotFound
0
# <tt>404</tt> is just one of the HTTP error response codes that Active Resource will handle with its own exception. The
0
# following HTTP response codes will also result in these exceptions:
0
# * 200..399 - Valid response, no exception
0
# * 404 - ActiveResource::ResourceNotFound
0
# * 409 - ActiveResource::ResourceConflict
0
@@ -149,17 +149,17 @@ module ActiveResource
0
# === Validation errors
0
# Active Resource supports validations on resources and will return errors if any these validations fail
0
- # (e.g., "First name can not be blank" and so on). These types of errors are denoted in the response by
0
+ # (e.g., "First name can not be blank" and so on). These types of errors are denoted in the response by
0
# a response code of <tt>422</tt> and an XML representation of the validation errors. The save operation will
0
# then fail (with a <tt>false</tt> return value) and the validation errors can be accessed on the resource in question.
0
# ryan = Person.find(1)
0
# # PUT http://api.people.com:3000/people/1.xml
0
# # is requested with invalid values, the response is:
0
@@ -169,7 +169,7 @@ module ActiveResource
0
# ryan.errors.invalid?(:first) # => true
0
# ryan.errors.full_messages # => ['First cannot be empty']
0
# Learn more about Active Resource's validation features in the ActiveResource::Validations documentation.
0
@@ -280,7 +280,7 @@ module ActiveResource
0
# Default format is <tt>:xml</tt>.
0
def format=(mime_type_reference_or_format)
0
- format = mime_type_reference_or_format.is_a?(Symbol) ?
0
+ format = mime_type_reference_or_format.is_a?(Symbol) ?
0
ActiveResource::Formats[mime_type_reference_or_format] : mime_type_reference_or_format
0
write_inheritable_attribute("format", format)
0
@@ -332,7 +332,7 @@ module ActiveResource
0
attr_accessor_with_default(:collection_name) { element_name.pluralize } #:nodoc:
0
attr_accessor_with_default(:primary_key, 'id') #:nodoc:
0
# Gets the prefix for a resource's nested URL (e.g., <tt>prefix/collectionname/1.xml</tt>)
0
# This method is regenerated at runtime based on what the prefix is set to.
0
@@ -381,21 +381,21 @@ module ActiveResource
0
# +query_options+ - A hash to add items to the query string for the request.
0
- # Post.element_path(1)
0
+ # Post.element_path(1)
0
- # Comment.element_path(1, :post_id => 5)
0
+ # Comment.element_path(1, :post_id => 5)
0
# # => /posts/5/comments/1.xml
0
- # Comment.element_path(1, :post_id => 5, :active => 1)
0
+ # Comment.element_path(1, :post_id => 5, :active => 1)
0
# # => /posts/5/comments/1.xml?active=1
0
- # Comment.element_path(1, {:post_id => 5}, {:active => 1})
0
+ # Comment.element_path(1, {:post_id => 5}, {:active => 1})
0
# # => /posts/5/comments/1.xml?active=1
0
def element_path(id, prefix_options = {}, query_options = nil)
0
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
0
- "#{prefix(prefix_options)}#{collection_name}/#{id}.#{format.extension}#{query_string(query_options)}"
0
+ "#{prefix(prefix_options)}#{collection_name}/#{id}.#{format.extension}#{query_string(query_options)}"
0
# Gets the collection path for the REST resources. If the +query_options+ parameter is omitted, Rails
0
@@ -410,13 +410,13 @@ module ActiveResource
0
- # Comment.collection_path(:post_id => 5)
0
+ # Comment.collection_path(:post_id => 5)
0
# # => /posts/5/comments.xml
0
- # Comment.collection_path(:post_id => 5, :active => 1)
0
+ # Comment.collection_path(:post_id => 5, :active => 1)
0
# # => /posts/5/comments.xml?active=1
0
- # Comment.collection_path({:post_id => 5}, {:active => 1})
0
+ # Comment.collection_path({:post_id => 5}, {:active => 1})
0
# # => /posts/5/comments.xml?active=1
0
def collection_path(prefix_options = {}, query_options = nil)
0
@@ -451,50 +451,54 @@ module ActiveResource
0
# that_guy.valid? # => false
0
# that_guy.new? # => true
0
def create(attributes = {})
0
- returning(self.new(attributes)) { |res| res.save }
0
+ returning(self.new(attributes)) { |res| res.save }
0
# Core method for finding resources. Used similarly to Active Record's +find+ method.
0
- # The first argument is considered to be the scope of the query. That is, how many
0
+ # The first argument is considered to be the scope of the query. That is, how many
0
# resources are returned from the request. It can be one of the following.
0
# * <tt>:one</tt> - Returns a single resource.
0
# * <tt>:first</tt> - Returns the first resource found.
0
+ # * <tt>:last</tt> - Returns the last resource found.
0
# * <tt>:all</tt> - Returns every resource that matches the request.
0
# * <tt>:from</tt> - Sets the path or custom method that resources will be fetched from.
0
# * <tt>:params</tt> - Sets query and prefix (nested URL) parameters.
0
# # => GET /people/1.xml
0
- # Person.find(:all, :params => { :title => "CEO" })
0
+ # Person.find(:all, :params => { :title => "CEO" })
0
# # => GET /people.xml?title=CEO
0
- # Person.find(:first, :from => :managers)
0
+ # Person.find(:first, :from => :managers)
0
+ # # => GET /people/managers.xml
0
+ # Person.find(:last, :from => :managers)
0
# # => GET /people/managers.xml
0
- # Person.find(:all, :from => "/companies/1/people.xml")
0
+ # Person.find(:all, :from => "/companies/1/people.xml")
0
# # => GET /companies/1/people.xml
0
- # Person.find(:one, :from => :leader)
0
+ # Person.find(:one, :from => :leader)
0
# # => GET /people/leader.xml
0
# Person.find(:all, :from => :developers, :params => { :language => 'ruby' })
0
# # => GET /people/developers.xml?language=ruby
0
- # Person.find(:one, :from => "/companies/1/manager.xml")
0
+ # Person.find(:one, :from => "/companies/1/manager.xml")
0
# # => GET /companies/1/manager.xml
0
- # StreetAddress.find(1, :params => { :person_id => 1 })
0
+ # StreetAddress.find(1, :params => { :person_id => 1 })
0
# # => GET /people/1/street_addresses/1.xml
0
scope = arguments.slice!(0)
0
@@ -503,6 +507,7 @@ module ActiveResource
0
when :all then find_every(options)
0
when :first then find_every(options).first
0
+ when :last then find_every(options).last
0
when :one then find_one(options)
0
else find_single(scope, options)
0
@@ -560,7 +565,7 @@ module ActiveResource
0
instantiate_collection( (connection.get(path, headers) || []), prefix_options )
0
# Find a single resource from a one-off URL
0
case from = options[:from]
0
@@ -578,7 +583,7 @@ module ActiveResource
0
path = element_path(scope, prefix_options, query_options)
0
instantiate_record(connection.get(path, headers), prefix_options)
0
def instantiate_collection(collection, prefix_options = {})
0
collection.collect! { |record| instantiate_record(record, prefix_options) }
0
@@ -602,10 +607,10 @@ module ActiveResource
0
# Builds the query string for the request.
0
def query_string(options)
0
- "?#{options.to_query}" unless options.nil? || options.empty?
0
+ "?#{options.to_query}" unless options.nil? || options.empty?
0
- # split an option hash into two hashes, one containing the prefix options,
0
+ # split an option hash into two hashes, one containing the prefix options,
0
# and the other containing the leftovers.
0
def split_options(options = {})
0
prefix_options, query_options = {}, {}
0
@@ -654,7 +659,7 @@ module ActiveResource
0
# ryan = Person.find(1)
0
# ryan.address = StreetAddress.find(1, :person_id => ryan.id)
0
# ryan.hash = {:not => "an ARes instance"}
0
# not_ryan = ryan.clone
0
# not_ryan.new? # => true
0
# not_ryan.address # => NoMethodError
0
@@ -706,7 +711,7 @@ module ActiveResource
0
- # Test for equality. Resource are equal if and only if +other+ is the same object or
0
+ # Test for equality. Resource are equal if and only if +other+ is the same object or
0
# is an instance of the same class, is not <tt>new?</tt>, and has the same +id+.
0
@@ -742,7 +747,7 @@ module ActiveResource
0
# Duplicate the current resource without saving it.
0
@@ -762,7 +767,7 @@ module ActiveResource
0
- # A method to save (+POST+) or update (+PUT+) a resource. It delegates to +create+ if a new object,
0
+ # A method to save (+POST+) or update (+PUT+) a resource. It delegates to +create+ if a new object,
0
# +update+ if it is existing. If the response to the save includes a body, it will be assumed that this body
0
# is XML for the final object as it looked after the save (which would include attributes like +created_at+
0
# that weren't part of the original submit).
0
@@ -786,7 +791,7 @@ module ActiveResource
0
# my_person = Person.find(my_id)
0
# Person.find(my_id) # 404 (Resource Not Found)
0
# new_person = Person.create(:name => 'James')
0
# new_id = new_person.id # => 7
0
@@ -825,7 +830,7 @@ module ActiveResource
0
# * <tt>:indent</tt> - Set the indent level for the XML output (default is +2+).
0
# * <tt>:dasherize</tt> - Boolean option to determine whether or not element names should
0
# replace underscores with dashes (default is <tt>false</tt>).
0
- # * <tt>:skip_instruct</tt> - Toggle skipping the +instruct!+ call on the XML builder
0
+ # * <tt>:skip_instruct</tt> - Toggle skipping the +instruct!+ call on the XML builder
0
# that generates the XML declaration (default is <tt>false</tt>).
0
@@ -849,7 +854,7 @@ module ActiveResource
0
# my_branch = Branch.find(:first)
0
# my_branch.name # => "Wislon Raod"
0
# # Another client fixes the typo...
0
# my_branch.name # => "Wislon Raod"
0
@@ -897,7 +902,7 @@ module ActiveResource
0
# For checking <tt>respond_to?</tt> without searching the attributes (which is faster).
0
alias_method :respond_to_without_attributes?, :respond_to?
0
@@ -909,7 +914,7 @@ module ActiveResource
0
elsif attributes.has_key?(method_name)
0
elsif ['?','='].include?(method_name.last) && attributes.has_key?(method_name.first(-1))
0
@@ -917,7 +922,7 @@ module ActiveResource
0
# would return true for generated readers, even if the attribute wasn't present
0
def connection(refresh = false)
0
@@ -938,7 +943,7 @@ module ActiveResource
0
load_attributes_from_response(response)
0
def load_attributes_from_response(response)
0
if response['Content-Length'] != "0" && response.body.strip.size > 0
0
load(self.class.format.decode(response.body))
0
@@ -963,7 +968,7 @@ module ActiveResource
0
def find_or_create_resource_for_collection(name)
0
find_or_create_resource_for(name.to_s.singularize)
0
# Tries to find a resource in a non empty list of nested modules
0
# Raises a NameError if it was not found in any of the given nested modules
0
def find_resource_in_modules(resource_name, module_names)