2010-03-28 08:15:02 -04:00
|
|
|
require 'active_support/core_ext/object/blank'
|
|
|
|
|
2007-04-25 21:53:01 -04:00
|
|
|
module ActiveResource
|
2007-12-21 16:42:27 -05:00
|
|
|
# A module to support custom REST methods and sub-resources, allowing you to break out
|
2012-01-21 18:44:25 -05:00
|
|
|
# of the "default" REST methods with your own custom resource requests. For example,
|
2007-12-21 16:42:27 -05:00
|
|
|
# say you use Rails to expose a REST service and configure your routes with:
|
|
|
|
#
|
|
|
|
# map.resources :people, :new => { :register => :post },
|
2008-01-23 12:04:49 -05:00
|
|
|
# :member => { :promote => :put, :deactivate => :delete }
|
2007-12-21 16:42:27 -05:00
|
|
|
# :collection => { :active => :get }
|
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# This route set creates routes for the following HTTP requests:
|
2007-12-21 16:42:27 -05:00
|
|
|
#
|
2011-05-06 17:03:55 -04:00
|
|
|
# POST /people/new/register.json # PeopleController.register
|
2012-02-24 19:35:48 -05:00
|
|
|
# PATCH/PUT /people/1/promote.json # PeopleController.promote with :id => 1
|
2011-05-06 17:03:55 -04:00
|
|
|
# DELETE /people/1/deactivate.json # PeopleController.deactivate with :id => 1
|
|
|
|
# GET /people/active.json # PeopleController.active
|
2007-12-21 16:42:27 -05:00
|
|
|
#
|
|
|
|
# Using this module, Active Resource can use these custom REST methods just like the
|
|
|
|
# standard methods.
|
|
|
|
#
|
|
|
|
# class Person < ActiveResource::Base
|
2012-01-22 19:27:22 -05:00
|
|
|
# self.site = "https://37s.sunrise.com"
|
2007-12-21 16:42:27 -05:00
|
|
|
# end
|
|
|
|
#
|
2011-11-21 20:06:00 -05:00
|
|
|
# Person.new(:name => 'Ryan').post(:register) # POST /people/new/register.json
|
2007-12-21 16:42:27 -05:00
|
|
|
# # => { :id => 1, :name => 'Ryan' }
|
|
|
|
#
|
2011-05-17 19:50:34 -04:00
|
|
|
# Person.find(1).put(:promote, :position => 'Manager') # PUT /people/1/promote.json
|
|
|
|
# Person.find(1).delete(:deactivate) # DELETE /people/1/deactivate.json
|
2007-12-21 16:42:27 -05:00
|
|
|
#
|
2011-05-17 19:50:34 -04:00
|
|
|
# Person.get(:active) # GET /people/active.json
|
2007-12-21 16:42:27 -05:00
|
|
|
# # => [{:id => 1, :name => 'Ryan'}, {:id => 2, :name => 'Joe'}]
|
|
|
|
#
|
2008-08-29 21:19:18 -04:00
|
|
|
module CustomMethods
|
2009-06-08 21:52:27 -04:00
|
|
|
extend ActiveSupport::Concern
|
2007-10-02 01:32:14 -04:00
|
|
|
|
2009-06-08 21:52:27 -04:00
|
|
|
included do
|
|
|
|
class << self
|
|
|
|
alias :orig_delete :delete
|
2007-10-02 01:32:14 -04:00
|
|
|
|
2009-06-08 21:52:27 -04:00
|
|
|
# Invokes a GET to a given custom REST method. For example:
|
|
|
|
#
|
2011-05-17 19:50:34 -04:00
|
|
|
# Person.get(:active) # GET /people/active.json
|
2009-06-08 21:52:27 -04:00
|
|
|
# # => [{:id => 1, :name => 'Ryan'}, {:id => 2, :name => 'Joe'}]
|
|
|
|
#
|
2011-05-17 19:50:34 -04:00
|
|
|
# Person.get(:active, :awesome => true) # GET /people/active.json?awesome=true
|
2009-06-08 21:52:27 -04:00
|
|
|
# # => [{:id => 1, :name => 'Ryan'}]
|
|
|
|
#
|
|
|
|
# Note: the objects returned from this method are not automatically converted
|
|
|
|
# into ActiveResource::Base instances - they are ordinary Hashes. If you are expecting
|
|
|
|
# ActiveResource::Base instances, use the <tt>find</tt> class method with the
|
|
|
|
# <tt>:from</tt> option. For example:
|
|
|
|
#
|
|
|
|
# Person.find(:all, :from => :active)
|
|
|
|
def get(custom_method_name, options = {})
|
2011-05-17 19:30:43 -04:00
|
|
|
hashified = format.decode(connection.get(custom_method_collection_url(custom_method_name, options), headers).body)
|
|
|
|
derooted = Formats.remove_root(hashified)
|
|
|
|
derooted.is_a?(Array) ? derooted.map { |e| Formats.remove_root(e) } : derooted
|
2009-06-08 21:52:27 -04:00
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
|
2009-06-08 21:52:27 -04:00
|
|
|
def post(custom_method_name, options = {}, body = '')
|
|
|
|
connection.post(custom_method_collection_url(custom_method_name, options), body, headers)
|
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
|
2011-05-06 17:03:55 -04:00
|
|
|
def patch(custom_method_name, options = {}, body = '')
|
|
|
|
connection.patch(custom_method_collection_url(custom_method_name, options), body, headers)
|
|
|
|
end
|
|
|
|
|
2009-06-08 21:52:27 -04:00
|
|
|
def put(custom_method_name, options = {}, body = '')
|
|
|
|
connection.put(custom_method_collection_url(custom_method_name, options), body, headers)
|
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
|
2009-06-08 21:52:27 -04:00
|
|
|
def delete(custom_method_name, options = {})
|
|
|
|
# Need to jump through some hoops to retain the original class 'delete' method
|
|
|
|
if custom_method_name.is_a?(Symbol)
|
|
|
|
connection.delete(custom_method_collection_url(custom_method_name, options), headers)
|
|
|
|
else
|
|
|
|
orig_delete(custom_method_name, options)
|
2007-04-25 21:53:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
|
2007-04-25 21:53:01 -04:00
|
|
|
module ClassMethods
|
|
|
|
def custom_method_collection_url(method_name, options = {})
|
|
|
|
prefix_options, query_options = split_options(options)
|
2007-12-28 12:03:58 -05:00
|
|
|
"#{prefix(prefix_options)}#{collection_name}/#{method_name}.#{format.extension}#{query_string(query_options)}"
|
2007-04-25 21:53:01 -04:00
|
|
|
end
|
|
|
|
end
|
2008-08-29 21:19:18 -04:00
|
|
|
|
2011-11-30 10:44:37 -05:00
|
|
|
def get(method_name, options = {})
|
|
|
|
self.class.format.decode(connection.get(custom_method_element_url(method_name, options), self.class.headers).body)
|
|
|
|
end
|
2008-08-29 21:19:18 -04:00
|
|
|
|
2011-11-30 10:44:37 -05:00
|
|
|
def post(method_name, options = {}, body = nil)
|
|
|
|
request_body = body.blank? ? encode : body
|
|
|
|
if new?
|
|
|
|
connection.post(custom_method_new_element_url(method_name, options), request_body, self.class.headers)
|
|
|
|
else
|
|
|
|
connection.post(custom_method_element_url(method_name, options), request_body, self.class.headers)
|
2007-04-25 21:53:01 -04:00
|
|
|
end
|
2011-11-30 10:44:37 -05:00
|
|
|
end
|
2008-08-29 21:19:18 -04:00
|
|
|
|
2011-05-06 17:03:55 -04:00
|
|
|
def patch(method_name, options = {}, body = '')
|
|
|
|
connection.patch(custom_method_element_url(method_name, options), body, self.class.headers)
|
|
|
|
end
|
|
|
|
|
2011-11-30 10:44:37 -05:00
|
|
|
def put(method_name, options = {}, body = '')
|
|
|
|
connection.put(custom_method_element_url(method_name, options), body, self.class.headers)
|
|
|
|
end
|
2008-08-29 21:19:18 -04:00
|
|
|
|
2011-11-30 10:44:37 -05:00
|
|
|
def delete(method_name, options = {})
|
|
|
|
connection.delete(custom_method_element_url(method_name, options), self.class.headers)
|
|
|
|
end
|
2007-04-25 21:53:01 -04:00
|
|
|
|
|
|
|
|
2011-11-30 10:44:37 -05:00
|
|
|
private
|
|
|
|
def custom_method_element_url(method_name, options = {})
|
|
|
|
"#{self.class.prefix(prefix_options)}#{self.class.collection_name}/#{id}/#{method_name}.#{self.class.format.extension}#{self.class.__send__(:query_string, options)}"
|
|
|
|
end
|
2008-08-29 21:19:18 -04:00
|
|
|
|
2011-11-30 10:44:37 -05:00
|
|
|
def custom_method_new_element_url(method_name, options = {})
|
|
|
|
"#{self.class.prefix(prefix_options)}#{self.class.collection_name}/new/#{method_name}.#{self.class.format.extension}#{self.class.__send__(:query_string, options)}"
|
|
|
|
end
|
2007-04-25 21:53:01 -04:00
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
end
|