Class: Sinatra::Rabbit::Operation

Inherits:
Object
  • Object
show all
Includes:
Deltacloud::Validation
Defined in:
lib/sinatra/rabbit.rb

Constant Summary collapse

STANDARD =
{
  :index => { :method => :get, :member => false },
  :show =>  { :method => :get, :member => true },
  :create => { :method => :post, :member => false },
  :update => { :method => :put, :member => true },
  :destroy => { :method => :delete, :member => true }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Deltacloud::Validation

#add_params, #each_param, #param, #params, #validate

Constructor Details

#initialize(coll, name, opts, &block) ⇒ Operation

Returns a new instance of Operation.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sinatra/rabbit.rb', line 26

def initialize(coll, name, opts, &block)
  @name = name.to_sym
  opts = STANDARD[@name].merge(opts) if standard?
  @collection = coll
  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
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



14
15
16
# File 'lib/sinatra/rabbit.rb', line 14

def method
  @method
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/sinatra/rabbit.rb', line 14

def name
  @name
end

Instance Method Details

#control(opts = nil, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sinatra/rabbit.rb', line 58

def control(opts=nil, &block)
  op = self
  @control = Proc.new do
    if opts and
        opts[:with_feature] and
        !driver.respond_to?(opts[:with_feature])
      raise Deltacloud::BackendFeatureUnsupported.new('501', nil,
        "#{opts[:with_feature]} not supported by backend", nil)
    end
    op.validate(params)
    instance_eval(&block)
  end
end

#description(text = "") ⇒ Object



42
43
44
45
# File 'lib/sinatra/rabbit.rb', line 42

def description(text="")
  return @description if text.blank?
  @description = text
end

#generateObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sinatra/rabbit.rb', line 90

def generate
  ::Sinatra::Application.send(@method, 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_documentationObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/sinatra/rabbit.rb', line 47

def generate_documentation
  coll, oper = @collection, self
  ::Sinatra::Application.get("/api/docs/#{@collection.name}/#{@name}") do
    @collection, @operation = coll, oper
    respond_to do |format|
      format.html { haml :'docs/operation' }
      format.xml { haml :'docs/operation' }
    end
  end
end

#path(args = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sinatra/rabbit.rb', line 77

def path(args = {})
  l_prefix = args[:prefix] ? args[:prefix] : prefix
  if @member
    if standard?
      "#{l_prefix}/#{@collection.name}/:id"
    else
      "#{l_prefix}/#{@collection.name}/:id/#{name}"
    end
  else
    "#{l_prefix}/#{@collection.name}"
  end
end

#prefixObject



72
73
74
75
# File 'lib/sinatra/rabbit.rb', line 72

def prefix
  # FIXME: Make the /api prefix configurable
  "/api"
end

#standard?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/sinatra/rabbit.rb', line 38

def standard?
  STANDARD.keys.include?(name)
end