Class: Aikido::Zen::Route
- Inherits:
-
Object
- Object
- Aikido::Zen::Route
- Defined in:
- lib/aikido/zen/route.rb
Overview
Routes keep information about the mapping defined in the current web framework to go from a given HTTP request to the code that handles said request.
Instance Attribute Summary collapse
-
#path ⇒ String
readonly
The URL pattern used to match request paths.
-
#verb ⇒ String
readonly
The HTTP verb used to request this route.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #as_json ⇒ Object
- #hash ⇒ Object
-
#initialize(verb:, path:) ⇒ Route
constructor
A new instance of Route.
- #inspect ⇒ Object
- #match?(other) ⇒ Boolean
-
#sort_key ⇒ Array
Sort routes by wildcard matching order deterministically:.
Constructor Details
#initialize(verb:, path:) ⇒ Route
Returns a new instance of Route.
22 23 24 25 |
# File 'lib/aikido/zen/route.rb', line 22 def initialize(verb:, path:) @verb = verb @path = path end |
Instance Attribute Details
#path ⇒ String (readonly)
Returns the URL pattern used to match request paths. For example “/users/:id”.
20 21 22 |
# File 'lib/aikido/zen/route.rb', line 20 def path @path end |
#verb ⇒ String (readonly)
Returns the HTTP verb used to request this route.
16 17 18 |
# File 'lib/aikido/zen/route.rb', line 16 def verb @verb end |
Class Method Details
.from_json(data) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/aikido/zen/route.rb', line 8 def self.from_json(data) new( verb: data[:method], path: data[:path] ) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
31 32 33 34 35 |
# File 'lib/aikido/zen/route.rb', line 31 def ==(other) other.is_a?(Route) && other.verb == verb && other.path == path end |
#as_json ⇒ Object
27 28 29 |
# File 'lib/aikido/zen/route.rb', line 27 def as_json {method: verb, path: path} end |
#hash ⇒ Object
38 39 40 |
# File 'lib/aikido/zen/route.rb', line 38 def hash [verb, path].hash end |
#inspect ⇒ Object
77 78 79 |
# File 'lib/aikido/zen/route.rb', line 77 def inspect "#<#{self.class.name} #{verb} #{path.inspect}>" end |
#match?(other) ⇒ Boolean
71 72 73 74 75 |
# File 'lib/aikido/zen/route.rb', line 71 def match?(other) other.is_a?(Route) && pattern(verb).match?(other.verb) && pattern(path).match?(other.path) end |
#sort_key ⇒ Array
Sort routes by wildcard matching order deterministically:
1. Exact path before wildcard path
2. Fewer wildcards in path relative to path length
3. Earliest wildcard position in path
4. Exact verb before wildcard verb
5. Lexicographic path (tie-break)
6. Lexicographic verb (tie-break)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/aikido/zen/route.rb', line 52 def sort_key @sort_key ||= begin stars = [] i = -1 while (i = path.index("*", i + 1)) stars << i end [ stars.empty? ? 0 : 1, stars.length - path.length, stars, (verb == "*") ? 1 : 0, path, verb ].freeze end end |