Core method for finding singleton resources.
Takes a single argument of options
:params
- Sets the query and prefix (nested URL) parameters.
Weather.find # => GET /weather.json Weather.find(:params => {:degrees => 'fahrenheit'}) # => GET /weather.json?degrees=fahrenheit
A failure to find the requested object raises a ResourceNotFound exception.
Inventory.find # => raises ResourceNotFound
# File lib/active_resource/singleton.rb, line 63 def find(options={}) find_singleton(options) end
# File lib/active_resource/singleton.rb, line 8 def singleton_name @singleton_name ||= model_name.element end
Gets the singleton path for the object. If the query_options
parameter is omitted, Rails will split from the prefix options.
prefix_options
- A hash to add a prefix to the request for
nested URLs (e.g., :account_id => 19
would yield a URL like /accounts/19/purchases.json
).
query_options
- A hash to add items to the query string for
the request.
Weather.singleton_path # => /weather.json class Inventory < ActiveResource::Base self.site = "https://37s.sunrise.com" self.prefix = "/products/:product_id/" end Inventory.singleton_path(:product_id => 5) # => /products/5/inventory.json Inventory.singleton_path({:product_id => 5}, {:sold => true}) # => /products/5/inventory.json?sold=true
# File lib/active_resource/singleton.rb, line 36 def singleton_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)}#{singleton_name}#{format_extension}#{query_string(query_options)}" end
Find singleton resource
# File lib/active_resource/singleton.rb, line 69 def find_singleton(options) prefix_options, query_options = split_options(options[:params]) path = singleton_path(prefix_options, query_options) resp = self.format.decode(self.connection.get(path, self.headers).body) instantiate_record(resp, {}) end