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

Add the default access permissions to a route. Any group tagged superuser can automatically access any newly declared_route. params route [Arkaan::Monitoring::Route] the route to add the permissions to.



61
62
63
64
65
66
67
68
69
# File 'lib/virtuatable/helpers/declarators.rb', line 61

def add_permissions(route)
  groups = Arkaan::Permissions::Group.where(is_superuser: true)
  groups.each do |group|
    unless route.groups.where(id: group.id).exists?
      route.groups << group
      route.save!
    end
  end
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: 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)
    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.



73
74
75
# File 'lib/virtuatable/helpers/declarators.rb', line 73

def builder
  Virtuatable::Application.builder
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.



79
80
81
82
83
84
85
86
87
# File 'lib/virtuatable/helpers/declarators.rb', line 79

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