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_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.
- #complete_path(path) ⇒ Object
-
#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
58 59 60 61 62 |
# File 'lib/virtuatable/helpers/declarators.rb', line 58 def (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.
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: [: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) && gateway! session! if current_route.authenticated instance_eval(&block) end end |
#builder ⇒ Virtuatable::Builers::Base
Returns the current builder loading 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_options ⇒ Hash
The default options for a route, being the most used value for each key.
76 77 78 79 80 81 82 83 84 |
# File 'lib/virtuatable/helpers/declarators.rb', line 76 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 |