Class: Router
- Inherits:
-
Object
- Object
- Router
- Defined in:
- lib/blest.rb
Instance Attribute Summary collapse
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
- #after(&handler) ⇒ Object
- #before(&handler) ⇒ Object
- #describe(route, config) ⇒ Object
- #handle(request, context = {}) ⇒ Object
-
#initialize(options = nil) ⇒ Router
constructor
A new instance of Router.
- #merge(router) ⇒ Object
- #namespace(prefix, router) ⇒ Object
- #route(route, &handler) ⇒ Object
Constructor Details
#initialize(options = nil) ⇒ Router
Returns a new instance of Router.
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/blest.rb', line 11 def initialize( = nil) @middleware = [] @afterware = [] @timeout = 5000 @introspection = false @routes = {} if @timeout = ['timeout'] || [:timeout] || 5000 @introspection = ['introspection'] || [:introspection] || false end end |
Instance Attribute Details
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
9 10 11 |
# File 'lib/blest.rb', line 9 def routes @routes end |
Instance Method Details
#after(&handler) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/blest.rb', line 37 def after(&handler) unless handler.is_a?(Proc) raise ArgumentError, 'After handlers should be procs' end arg_count = handler.arity if arg_count <= 2 @afterware.push(handler) else raise ArgumentError, 'After handlers should have at most two arguments' end end |
#before(&handler) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/blest.rb', line 24 def before(&handler) unless handler.is_a?(Proc) raise ArgumentError, 'Before handlers should be procs' end arg_count = handler.arity if arg_count <= 2 @middleware.push(handler) else raise ArgumentError, 'Before handlers should have at most three arguments' end end |
#describe(route, config) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/blest.rb', line 66 def describe(route, config) raise ArgumentError, 'Route does not exist' unless @routes.key?(route) raise ArgumentError, 'Configuration should be an object' unless config.is_a?(Hash) if config.key?('description') raise ArgumentError, 'Description should be a str' if !config['description'].nil? && !config['description'].is_a?(String) @routes[route]['description'] = config['description'] end if config.key?('schema') raise ArgumentError, 'Schema should be a dict' if !config['schema'].nil? && !config['schema'].is_a?(Hash) @routes[route]['schema'] = config['schema'] end if config.key?('visible') raise ArgumentError, 'Visible should be True or False' if !config['visible'].nil? && ![true, false].include?(config['visible']) @routes[route]['visible'] = config['visible'] end if config.key?('validate') raise ArgumentError, 'Validate should be True or False' if !config['validate'].nil? && ![true, false].include?(config['validate']) @routes[route]['validate'] = config['validate'] end if config.key?('timeout') raise ArgumentError, 'Timeout should be a positive int' if !config['timeout'].nil? && (!config['timeout'].is_a?(Integer) || config['timeout'] <= 0) @routes[route]['timeout'] = config['timeout'] end end |
#handle(request, context = {}) ⇒ Object
142 143 144 |
# File 'lib/blest.rb', line 142 def handle(request, context = {}) handle_request(@routes, request, context) end |
#merge(router) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/blest.rb', line 96 def merge(router) raise ArgumentError, 'Router is required' unless router.is_a?(Router) new_routes = router.routes.keys existing_routes = @routes.keys raise ArgumentError, 'No routes to merge' if new_routes.empty? new_routes.each do |route| if existing_routes.include?(route) raise ArgumentError, 'Cannot merge duplicate routes: ' + route else @routes[route] = { **router.routes[route], handler: @middleware + router.routes[route][:handler] + @afterware, timeout: router.routes[route][:timeout] || @timeout } end end end |
#namespace(prefix, router) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/blest.rb', line 117 def namespace(prefix, router) raise ArgumentError, 'Router is required' unless router.is_a?(Router) prefix_error = validate_route(prefix, false) raise ArgumentError, prefix_error if prefix_error new_routes = router.routes.keys existing_routes = @routes.keys raise ArgumentError, 'No routes to namespace' if new_routes.empty? new_routes.each do |route| ns_route = "#{prefix}/#{route}" if existing_routes.include?(ns_route) raise ArgumentError, 'Cannot merge duplicate routes: ' + ns_route else @routes[ns_route] = { **router.routes[route], handler: @middleware + router.routes[route][:handler] + @afterware, timeout: router.routes[route].fetch('timeout', @timeout) } end end end |
#route(route, &handler) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/blest.rb', line 50 def route(route, &handler) route_error = validate_route(route, false) raise ArgumentError, route_error if route_error raise ArgumentError, 'Route already exists' if @routes.key?(route) raise ArgumentError, 'Handler should be a function' unless handler.respond_to?(:call) @routes[route] = { handler: [*@middleware, handler, *@afterware], description: nil, schema: nil, visible: @introspection, validate: false, timeout: @timeout } end |