Class: Lanes::API::Root

Inherits:
Sinatra::Application
  • Object
show all
Defined in:
lib/lanes/api/root.rb

Class Method Summary collapse

Class Method Details

.resources(model, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/lanes/api/root.rb', line 42

def self.resources(model, options = {})

    path = options[:path] || model.api_path
    controller = options[:controller] || Lanes::API::Controller

    parent_attribute = false
    prefix = if options[:under]
                 parent_attribute = options[:parent_attribute] || options[:under].underscore.singularize+'_id'
             else
                 ''
             end

    # index
    get "#{prefix}/#{path}/?:id?.json", &RequestWrapper.get(model, controller, parent_attribute)

    # create
    post "#{prefix}/#{path}.json", &RequestWrapper.post(model, controller, parent_attribute)

    unless options[:immutable]

        # update
        patch "#{prefix}/#{path}/?:id?.json", &RequestWrapper.update(model, controller, parent_attribute)
        put   "#{prefix}/#{path}/?:id?.json", &RequestWrapper.update(model, controller, parent_attribute)

        unless options[:indestructible]
            # destroy
            delete "#{prefix}/#{path}/?:id?.json", &RequestWrapper.delete(model, controller, parent_attribute)
        end

    end

end