module ActiveResource::Singleton::ClassMethods

Attributes

singleton_name[W]

Public Instance Methods

find(options={}) click to toggle source

Core method for finding singleton resources.

Arguments

Takes a single argument of options

Options

  • :params - Sets the query and prefix (nested URL) parameters.

Examples

Weather.find
# => GET /weather.json

Weather.find(:params => {:degrees => 'fahrenheit'})
# => GET /weather.json?degrees=fahrenheit

Failure or missing data

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
singleton_name() click to toggle source
# File lib/active_resource/singleton.rb, line 8
def singleton_name
  @singleton_name ||= model_name.element
end
singleton_path(prefix_options = {}, query_options = nil) click to toggle source

Gets the singleton path for the object. If the query_options parameter is omitted, Rails will split from the prefix options.

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.

Examples

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

Private Instance Methods

find_singleton(options) click to toggle source

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, prefix_options)
end