Class: Cyclid::API::Plugins::ApiExtension::Controller

Inherits:
Module
  • Object
show all
Defined in:
app/cyclid/plugins/api.rb

Overview

Sinatra controller; this is more complex than usual to allow the plugin to connect it’s own set of methods as callbacks.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(methods = nil, custom_routes = []) ⇒ Controller

Returns a new instance of Controller.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/cyclid/plugins/api.rb', line 29

def initialize(methods = nil, custom_routes = [])
  @plugin_methods = methods

  # Provide default routes for the four basic HTTP verbs; for most
  # plugins they only need to implement the matching method for the
  # verb(s) they'll respond too.
  default_routes = [{ verb: :get, path: nil, func: 'get' },
                    { verb: :post, path: nil, func: 'post' },
                    { verb: :put, path: nil, func: 'put' },
                    { verb: :delete, path: nil, func: 'delete' }]

  # ...but more complex plugins can add additional routes with their
  # own paths & methods, if required.
  @routes = default_routes.concat custom_routes
end

Instance Attribute Details

#plugin_methodsObject (readonly)

Returns the value of attribute plugin_methods.



27
28
29
# File 'app/cyclid/plugins/api.rb', line 27

def plugin_methods
  @plugin_methods
end

Instance Method Details

#registered(app) ⇒ Object

Sinatra callback



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
# File 'app/cyclid/plugins/api.rb', line 46

def registered(app)
  include Errors::HTTPErrors

  @routes.each do |route|
    verb = route[:verb]
    path = route[:path]
    func = route[:func]

    app.send(verb, path) do
      Cyclid.logger.debug "ApiExtension::Controller::#{verb} #{path}"

      org = Organization.find_by(name: params[:name])
      halt_with_json_response(404, INVALID_ORG, 'organization does not exist') \
        if org.nil?

      config = controller_plugin.get_config(org)

      meth = method(func)
      meth.call(http_headers(request.env), config['config'])
    end
  end

  app.helpers do
    include Helpers
    include Job::Helpers
  end
end