class Sinatra::Rabbit::Collection::Operation

Public Class Methods

control(&block) click to toggle source
# File lib/sinatra/rabbit/base.rb, line 384
def self.control(&block)
  params_def = @params
  klass = self
  @control ||= Proc.new {
    if settings.respond_to?(:capability) and !settings.capability(klass.required_capability)
      halt([412, { 'Expect' => klass.required_capability.to_s }, "The required capability to execute this operation is missing"])
    end
    begin
      Rabbit::Validator.validate!(params, params_def)
    rescue => e
      if e.kind_of? Rabbit::Validator::ValidationError
        halt e.http_status_code, e.message
      else
        raise e
      end
    end
    instance_eval(&block) if block_given?
  }
end
description(text=nil) click to toggle source
# File lib/sinatra/rabbit/base.rb, line 374
def self.description(text=nil)
  @description ||= text
end
docs_url() click to toggle source
# File lib/sinatra/rabbit/base.rb, line 287
def self.docs_url
  @collection.root_path + ['docs', @collection.collection_name, operation_name].join('/')
end
features() click to toggle source
# File lib/sinatra/rabbit/base.rb, line 331
def self.features
  @collection.features_for(operation_name)
end
features_params() click to toggle source
# File lib/sinatra/rabbit/base.rb, line 335
def self.features_params
  features.map { |f| f.operations.map { |o| o.params_array } }.flatten
end
full_path() click to toggle source
# File lib/sinatra/rabbit/base.rb, line 367
def self.full_path; @operation_path; end
generate(collection, name, opts={}, &block) click to toggle source
# File lib/sinatra/rabbit/base.rb, line 339
def self.generate(collection, name, opts={}, &block)
  @name, @params, @collection = name, [], collection
  @options = opts

  if @options.has_key?(:http_method)
    @method = @options.delete(:http_method)
  end

  features.each do |feature|
    if Sinatra::Rabbit.configuration[:check_features]
      next unless Sinatra::Rabbit.configuration[:check_features].call(collection.collection_name, feature.name)
    end
    feature.operations.each do |o|
      instance_eval(&o.params)
    end
  end

  if Sinatra::Rabbit::STANDARD_OPERATIONS.has_key? name
    required_params = Sinatra::Rabbit::STANDARD_OPERATIONS[name][:required_params]
    required_params.each do |p|
      param p, :string, :required, "The #{p} parameter"
    end unless required_params.nil?
  end
  class_eval(&block)
  description "#{name.to_s.capitalize} operation on #{@collection.name} collection" if description.nil?
  self
end
generate_docs_route(path) click to toggle source
# File lib/sinatra/rabbit/base.rb, line 309
def self.generate_docs_route(path)
  operation = self
  collection = @collection
  @collection.base_class.get path do
    css_file = File.read(File.join(File.dirname(__FILE__), '..', 'docs', 'bootstrap.min.css'))
    operation_file = File.read(File.join(File.dirname(__FILE__), '..', 'docs', 'operation.haml'))
    haml operation_file, :locals => {
      :css => css_file,
      :operation => operation,
      :collection => collection
    }
  end
end
generate_head_route(path) click to toggle source
# File lib/sinatra/rabbit/base.rb, line 303
def self.generate_head_route(path)
  @collection.base_class.head path do
    status 200
  end
end
generate_options_route(path) click to toggle source
# File lib/sinatra/rabbit/base.rb, line 323
def self.generate_options_route(path)
  operation_params = params.map { |p| p.to_s }.join(',')
  @collection.base_class.options path do
    headers 'Allow' => operation_params
    status 200
  end
end
has_capability?() click to toggle source

TODO: This method is here only to maintain 'backward' compatibility

# File lib/sinatra/rabbit/base.rb, line 380
def self.has_capability?
  true
end
http_method() click to toggle source
# File lib/sinatra/rabbit/base.rb, line 295
def self.http_method
  @method ||= BaseCollection.http_method_for(@name)
end
http_method=(method) click to toggle source
# File lib/sinatra/rabbit/base.rb, line 299
def self.http_method=(method)
  @method = method
end
operation_name() click to toggle source
# File lib/sinatra/rabbit/base.rb, line 368
def self.operation_name; @name; end
param(*args) click to toggle source
# File lib/sinatra/rabbit/base.rb, line 404
def self.param(*args)
  return @params.find { |p| p.name == args[0] } if args.size == 1
  @params << Rabbit::Param.new(*args)
end
params() click to toggle source
# File lib/sinatra/rabbit/base.rb, line 409
def self.params; @params; end
required_capability() click to toggle source
# File lib/sinatra/rabbit/base.rb, line 370
def self.required_capability
  @options[:with_capability]
end
route=(path) click to toggle source
# File lib/sinatra/rabbit/base.rb, line 291
def self.route=(path)
  @operation_path = path
end