Class: Sinatra::Rabbit::Collection::Operation

Inherits:
BaseCollection show all
Defined in:
lib/sinatra/rabbit/base.rb

Class Method Summary collapse

Methods inherited from BaseCollection

collection_class, http_method_for, root_path, route_for

Methods inherited from Base

options

Class Method Details

.control(&block) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# 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) ⇒ Object



374
375
376
# File 'lib/sinatra/rabbit/base.rb', line 374

def self.description(text=nil)
  @description ||= text
end

.docs_urlObject



287
288
289
# File 'lib/sinatra/rabbit/base.rb', line 287

def self.docs_url
  @collection.root_path + ['docs', @collection.collection_name, operation_name].join('/')
end

.featuresObject



331
332
333
# File 'lib/sinatra/rabbit/base.rb', line 331

def self.features
  @collection.features_for(operation_name)
end

.features_paramsObject



335
336
337
# 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_pathObject



367
# File 'lib/sinatra/rabbit/base.rb', line 367

def self.full_path; @operation_path; end

.generate(collection, name, opts = {}, &block) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# 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) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
320
321
# 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) ⇒ Object



303
304
305
306
307
# 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) ⇒ Object



323
324
325
326
327
328
329
# 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?Boolean

TODO: This method is here only to maintain ‘backward’ compatibility

Returns:

  • (Boolean)


380
381
382
# File 'lib/sinatra/rabbit/base.rb', line 380

def self.has_capability?
  true
end

.http_methodObject



295
296
297
# File 'lib/sinatra/rabbit/base.rb', line 295

def self.http_method
  @method ||= BaseCollection.http_method_for(@name)
end

.http_method=(method) ⇒ Object



299
300
301
# File 'lib/sinatra/rabbit/base.rb', line 299

def self.http_method=(method)
  @method = method
end

.operation_nameObject



368
# File 'lib/sinatra/rabbit/base.rb', line 368

def self.operation_name; @name; end

.param(*args) ⇒ Object



404
405
406
407
# 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

.paramsObject



409
# File 'lib/sinatra/rabbit/base.rb', line 409

def self.params; @params; end

.required_capabilityObject



370
371
372
# File 'lib/sinatra/rabbit/base.rb', line 370

def self.required_capability
  @options[:with_capability]
end

.route=(path) ⇒ Object



291
292
293
# File 'lib/sinatra/rabbit/base.rb', line 291

def self.route=(path)
  @operation_path = path
end