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.
Instance Attribute Summary collapse
-
#api_routes ⇒ Object
readonly
Returns the value of attribute api_routes.
-
#routes ⇒ Array<Arkaan::Monitoring::Route>
readonly
The currently declared routes.
Instance Method Summary collapse
-
#add_permissions(route) ⇒ Object
Add the default access permissions to a route.
-
#add_route(verb:, path:, options:) ⇒ Arkaan::Monitoring::Route
Add a route to the database, then to the routes array.
-
#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.
-
#builder ⇒ Virtuatable::Builers::Base
Returns the current builder loading the application.
-
#default_options ⇒ Hash
The default options for a route, being the most used value for each key.
-
#push_route(route) ⇒ Object
Pushes the route in the api routes list, by creating it if needed.
Instance Attribute Details
#api_routes ⇒ Object (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 |
#routes ⇒ Array<Arkaan::Monitoring::Route> (readonly)
Returns 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 (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.
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: [:premium], service: builder.service, authenticated: [:authenticated] ) api_routes.nil? ? @api_routes = [route] : push_route(route) (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.
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) = .merge() route = add_route(verb: verb, path: path, 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 |
#builder ⇒ Virtuatable::Builers::Base
Returns the current builder loading the application.
73 74 75 |
# File 'lib/virtuatable/helpers/declarators.rb', line 73 def builder Virtuatable::Application.builder end |
#default_options ⇒ Hash
The default options for a route, being the most used value for each key.
79 80 81 82 83 84 85 86 87 |
# File 'lib/virtuatable/helpers/declarators.rb', line 79 def { # 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
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 |