Module: Virtuatable::Helpers::Declarators

Included in:
Controllers::Base
Defined in:
lib/virtuatable/helpers/declarators.rb

Overview

This helpers module is a bit larger than the others as it provides methods to declare routes whithin a service, performing needed checks and filters.

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_routesObject (readonly)

Returns the value of attribute api_routes.



11
12
13
# File 'lib/virtuatable/helpers/declarators.rb', line 11

def api_routes
  @api_routes
end

#routesArray<Arkaan::Monitoring::Route> (readonly)

Returns the currently declared routes.

Returns:

  • (Array<Arkaan::Monitoring::Route>)

    the currently declared routes.



11
# File 'lib/virtuatable/helpers/declarators.rb', line 11

attr_reader :api_routes

Instance Method Details

#add_permissions(route) ⇒ Object



58
59
60
61
62
# File 'lib/virtuatable/helpers/declarators.rb', line 58

def add_permissions(route)
  route.groups = Arkaan::Permissions::Group.where(is_superuser: true)
  route.save!
  route
end

#add_route(verb:, path:, options:) ⇒ Arkaan::Monitoring::Route

Add a route to the database, then to the routes array.

Parameters:

  • verb (String)

    the HTTP method used to request this route.

  • path (String)

    the path used to request this route.

Returns:

  • (Arkaan::Monitoring::Route)

    the created route.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/virtuatable/helpers/declarators.rb', line 37

def add_route(verb:, path:, options:)
  route = Arkaan::Monitoring::Route.find_or_create_by!(
    path: complete_path(path),
    verb: verb.downcase,
    premium: options[:premium],
    service: builder.service,
    authenticated: options[:authenticated]
  )
  api_routes.nil? ? @api_routes = [route] : push_route(route)
  add_permissions(route)
  route
end

#api_route(verb, path, options: {}, &block) ⇒ Object

Main method to declare new routes, persisting them in the database and declaring it in the Sinatra application with the needed before checks.

Parameters:

  • verb (String)

    the HTTP method for the route.

  • path (String)

    the whole URI with parameters for the route.

  • options (Hash) (defaults to: {})

    the additional options for the route.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/virtuatable/helpers/declarators.rb', line 19

def api_route(verb, path, options: {}, &block)
  options = default_options.merge(options)
  route = add_route(verb: verb, path: path, options: options)

  # TODO : do everything in the #send itself to avoid
  # route reload issues when premium is changed. It will
  # add some treatments but avoid many problems        if route.premium
  send(route.verb, route.path) do
    application!(premium: current_route.premium) && gateway!
    session! if current_route.authenticated
    instance_eval(&block)
  end
end

#builderVirtuatable::Builers::Base

Returns the current builder loading the application.

Returns:

  • (Virtuatable::Builers::Base)

    the current builder of the application.



70
71
72
# File 'lib/virtuatable/helpers/declarators.rb', line 70

def builder
  Virtuatable::Application.builder
end

#complete_path(path) ⇒ Object



64
65
66
# File 'lib/virtuatable/helpers/declarators.rb', line 64

def complete_path(path)
  "#{builder.service.path}#{path == '/' ? '' : path}"
end

#default_optionsHash

The default options for a route, being the most used value for each key.

Returns:

  • (Hash)

    the default options as a hash.



76
77
78
79
80
81
82
83
84
# File 'lib/virtuatable/helpers/declarators.rb', line 76

def default_options
  {
    # If TRUE the application MUST be premium to access the route.
    # Mainly used to protect administration routes against illegal accesses.
    premium: false,
    # If TRUE the user MUST be authenticated to access the route.
    authenticated: true
  }
end

#push_route(route) ⇒ Object

Pushes the route in the api routes list, by creating it if needed

Parameters:

  • route (Arkaan::Monitoring::Route)

    the route to push in the list of routes.



52
53
54
55
56
# File 'lib/virtuatable/helpers/declarators.rb', line 52

def push_route(route)
  @api_routes << route if api_routes.none? do |tmp_route|
    route.id == tmp_route.id
  end
end