Parent

Class/Module Index [+]

Quicksearch

Sinatra::Rabbit::Operation

Constants

STANDARD

Attributes

collection[R]
member[R]
method[R]
name[R]

Public Class Methods

new(coll, name, opts, &block) click to toggle source
# File lib/sinatra/rabbit.rb, line 59
def initialize(coll, name, opts, &block)
  @name = name.to_sym
  opts = STANDARD[@name].merge(opts) if standard?
  @path_generator = opts[:path_generator]
  @collection, @standard = coll, opts[:standard]
  raise "No method for operation #{name}" unless opts[:method]
  @method = opts[:method].to_sym
  @member = opts[:member]
  @description = ""
  instance_eval(&block) if block_given?
  generate_documentation
  generate_options
end

Public Instance Methods

control(&block) click to toggle source
# File lib/sinatra/rabbit.rb, line 119
def control(&block)
  op = self
  @control = Proc.new do
    op.collection.check_supported(driver)
    op.check_capability(driver)
    op.validate(driver, op.effective_params(driver), params, credentials)
    instance_eval(&block)
  end
end
description(text="") click to toggle source
# File lib/sinatra/rabbit.rb, line 85
def description(text="")
  return @description if text.blank?
  @description = text
end
effective_params(driver) click to toggle source

Return a hash of all params, the params statically defined for this operation plus the params defined by any features in the driver that might modify this operation

# File lib/sinatra/rabbit.rb, line 170
def effective_params(driver)
  driver.features(@collection.name).collect do |f|
    f.decl.operation(@name)
  end.flatten.select { |op| op }.inject(params.dup) do |result, fop|
    fop.params.each_key do |k|
      if result.has_key?(k)
        raise DuplicateParamException, "Parameter '#{k}' for operation #{fop.name} in collection #{@collection.name}"
      else
        result[k] = fop.params[k]
      end
    end
    result
  end
end
form?() click to toggle source
# File lib/sinatra/rabbit.rb, line 81
def form?
  STANDARD[name] and STANDARD[name][:form]
end
generate() click to toggle source
# File lib/sinatra/rabbit.rb, line 154
def generate
  Rabbit::routes << [@method, "#{settings.root_url}/#{path}"]
  ::Sinatra::Application.send(@method, "#{settings.root_url}/#{path}", {}, &@control)
  # Set up some Rails-like URL helpers
  if name == :index
    gen_route "#{@collection.name}_url"
  elsif name == :show
    gen_route "#{@collection.name.to_s.singularize}_url"
  else
    gen_route "#{name}_#{@collection.name.to_s.singularize}_url"
  end
end
generate_documentation() click to toggle source
# File lib/sinatra/rabbit.rb, line 90
def generate_documentation
  coll, oper = @collection, self
  Rabbit::routes << [:get, "#{settings.root_url}/docs/#{@collection.name}/#{@name}"]
  ::Sinatra::Application.get("#{settings.root_url}/docs/#{@collection.name}/#{@name}") do
    @collection, @operation = coll, oper
    @features = driver.features_for_operation(coll.name, oper.name)
    respond_to do |format|
      format.html { haml :'docs/operation' }
      format.xml { haml :'docs/operation' }
    end
  end
end
generate_options() click to toggle source
# File lib/sinatra/rabbit.rb, line 103
def generate_options
  current_operation = self
  Rabbit::routes << [:options, "#{settings.root_url}/#{current_operation.collection.name}/#{current_operation.name}"]
  ::Sinatra::Application.options("#{settings.root_url}/#{current_operation.collection.name}/#{current_operation.name}") do
    required_params = current_operation.effective_params(driver).collect do |name, validation|
      name.to_s if validation.type.eql?(:required)
    end.compact.join(',')
    optional_params = current_operation.effective_params(driver).collect do |name, validation|
      name.to_s if validation.type.eql?(:optional)
    end.compact.join(',')
    headers 'X-Required-Parameters' => required_params
    headers 'X-Optional-Parameters' => optional_params
    [200, '']
  end
end
http_method() click to toggle source
# File lib/sinatra/rabbit.rb, line 73
def http_method
  @method
end
member?() click to toggle source
# File lib/sinatra/rabbit.rb, line 129
def member?
  if standard?
    @member || STANDARD[name][:member]
  else
    @member
  end
end
path(args = {}) click to toggle source
# File lib/sinatra/rabbit.rb, line 137
def path(args = {})
  return @path_generator.call(self) if @path_generator
  if member?
    if standard?
      "#{@collection.name}/:id"
    else
      "#{@collection.name}/:id/#{name}"
    end
  else
    if form?
      "#{@collection.name}/#{name}"
    else
      "#{@collection.name}"
    end
  end
end
standard?() click to toggle source
# File lib/sinatra/rabbit.rb, line 77
def standard?
  STANDARD.keys.include?(name) || @standard
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.