Class: Lanes::API::RoutingBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/lanes/api/routing.rb

Instance Method Summary collapse

Constructor Details

#initialize(ext_id) ⇒ RoutingBlock

Returns a new instance of RoutingBlock.



5
6
7
8
9
# File 'lib/lanes/api/routing.rb', line 5

def initialize(ext_id)
    Lanes::Extensions.for_identifier(ext_id) ||
        raise( "Unable to find extension '#{ext_id}' for screen group")
    @ext_id = ext_id
end

Instance Method Details

#enable_cors(path_suffix, options = {origins: '*', methods: [:get]}) ⇒ Object



24
25
26
# File 'lib/lanes/api/routing.rb', line 24

def enable_cors(path_suffix, options = {origins: '*', methods: [:get]})
    API::Root::CORS_PATHS[make_path(path_suffix)] = options
end

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
74
75
76
77
78
79
80
81
# File 'lib/lanes/api/routing.rb', line 28

def resources(model, options = {})
    path = options[:path] || model.api_path
    controller = options[:controller] || Lanes::API::GenericController
    format = options[:format] || '.json'
    if options[:under]
        options[:parent_attribute] = options[:under].underscore.singularize+'_id'
    end

    prefix = options[:parent_attribute] ? options[:parent_attribute] + '/' : ''

    configured_routes = Hash.new{|hsh, key| hsh[key] = []}

    bind = lambda{ |method, route, wrapper_method = method|
        route = route + format
        configured_routes[route].push(method)
        self.send( method, route,
                   &RequestWrapper.send(wrapper_method, model, controller, options) )
    }

    # show
    if controller.method_defined?(:show)
        bind[:get, "#{prefix}#{path}/?:id?"]
    end

    # create
    if controller.method_defined?(:create)
        bind[:post, "#{prefix}#{path}"]
    end

    unless options[:immutable]

        # update
        if controller.method_defined?(:update)
            bind[:patch, "#{prefix}#{path}/?:id?", :update]
            bind[:put,   "#{prefix}#{path}/?:id?", :update]
        end

        # destroy
        if controller.method_defined?(:destroy) and not options[:indestructible]
            bind[:delete, "#{prefix}#{path}/?:id?"]
        end

    end

    if options[:cors]
        cors = options[:cors].is_a?(Hash) ? otions[:cors] : {origins: options[:cors]}

        configured_routes.each do | route, methods |
            enable_cors route, cors.merge(methods: methods)
        end

    end

end

#root_view(view) ⇒ Object



11
12
13
14
15
16
# File 'lib/lanes/api/routing.rb', line 11

def root_view( view )
    API::Root.get Lanes.config.mounted_at + '?*' do
        pass unless request.accept? 'text/html'
        erb view
    end
end