Class: Flon::Router
- Inherits:
-
Object
- Object
- Flon::Router
- Defined in:
- lib/flon/router.rb
Overview
Handles a set of routes mapped to actions. Note that the Router API is mainly for private Flon use.
Defined Under Namespace
Classes: Match
Constant Summary collapse
- VALID_HTTP_METHODS =
The HTTP methods as symbols that this router will handle.
%i[get post put delete patch].freeze
Instance Attribute Summary collapse
-
#routes ⇒ Array<Mustermann::Sinatra, Hash{Symbol => Object}>
readonly
The current route set.
Instance Method Summary collapse
-
#add_route(method, path, action) ⇒ self
Adds a route to this router’s route set.
-
#initialize ⇒ Router
constructor
Creates a new Router with an empty route set.
-
#match(method, path) ⇒ RouteMatch, ...
Matches a method and path against this router’s route set.
-
#routing_to?(method, path) ⇒ Boolean
True if this router has a route with that method and path in its route set, false otherwise.
Constructor Details
#initialize ⇒ Router
Creates a new Router with an empty route set.
28 29 30 |
# File 'lib/flon/router.rb', line 28 def initialize @routes = [] end |
Instance Attribute Details
#routes ⇒ Array<Mustermann::Sinatra, Hash{Symbol => Object}> (readonly)
Returns the current route set.
24 25 26 |
# File 'lib/flon/router.rb', line 24 def routes @routes end |
Instance Method Details
#add_route(method, path, action) ⇒ self
Adds a route to this router’s route set.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/flon/router.rb', line 40 def add_route(method, path, action) # rubocop:disable Metrics/MethodLength raise ArgumentError, "#{method} is not a valid HTTP method" unless valid_method?(method) raise ArgumentError, "route #{method.upcase} #{path} is already registered" if routing_to?(method, path) route = @routes.find { |it| it[0].to_s == path } if route route[1][method] = action else route = [Mustermann::Sinatra.new(path), { method => action }] @routes << route end route[1][:head] = action if method == :get self end |
#match(method, path) ⇒ RouteMatch, ...
Matches a method and path against this router’s route set.
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/flon/router.rb', line 64 def match(method, path) @routes.each do |route| params = route[0].params(path) next unless params action = route[1][method] return :bad_method unless action return Match.new(action, params.transform_keys(&:to_sym)) end :bad_path end |
#routing_to?(method, path) ⇒ Boolean
Returns true if this router has a route with that method and path in its route set, false otherwise.
80 81 82 |
# File 'lib/flon/router.rb', line 80 def routing_to?(method, path) @routes.any? { |it| it[0].to_s == path && it[1].include?(method) } end |