Class: Interfacets::Server::BasicRouter
- Inherits:
-
Object
- Object
- Interfacets::Server::BasicRouter
- Defined in:
- lib/interfacets/server/basic_router.rb
Defined Under Namespace
Classes: Evaluator
Instance Attribute Summary collapse
-
#bus ⇒ Object
readonly
Returns the value of attribute bus.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
Instance Method Summary collapse
- #call(url, query: {}) ⇒ Object
- #evaluator ⇒ Object
-
#initialize(bus:, paths:, default: nil) ⇒ BasicRouter
constructor
A new instance of BasicRouter.
- #klass_for(path) ⇒ Object
- #normalize(path) ⇒ Object
- #normalized_paths ⇒ Object
Constructor Details
#initialize(bus:, paths:, default: nil) ⇒ BasicRouter
Returns a new instance of BasicRouter.
20 21 22 23 24 |
# File 'lib/interfacets/server/basic_router.rb', line 20 def initialize(bus:, paths:, default: nil) @bus = bus @paths = paths @default = default end |
Instance Attribute Details
#bus ⇒ Object (readonly)
Returns the value of attribute bus.
19 20 21 |
# File 'lib/interfacets/server/basic_router.rb', line 19 def bus @bus end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
19 20 21 |
# File 'lib/interfacets/server/basic_router.rb', line 19 def default @default end |
#paths ⇒ Object (readonly)
Returns the value of attribute paths.
19 20 21 |
# File 'lib/interfacets/server/basic_router.rb', line 19 def paths @paths end |
Instance Method Details
#call(url, query: {}) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/interfacets/server/basic_router.rb', line 26 def call(url, query: {}) path = normalize(url) if path.nil? && default klass = klass_for(default) return evaluator.call(klass:, id: nil, query:, resolved_path: default) end if normalized_paths.key?(path) klass = klass_for(path) evaluator.call(klass:, id: nil, query:, resolved_path: path) else *parts, id = path.split("/") resolved_base_path = parts.join("/") if normalized_paths.key?(resolved_base_path) klass = klass_for(resolved_base_path) evaluator.call(klass:, id:, query:, resolved_path: path) elsif default klass = klass_for(default) evaluator.call(klass:, id: nil, query:, resolved_path: default) else raise "unhandled route: #{path}" end end end |
#evaluator ⇒ Object
54 55 56 |
# File 'lib/interfacets/server/basic_router.rb', line 54 def evaluator @evaluator ||= Evaluator.new(bus:) end |
#klass_for(path) ⇒ Object
58 59 60 61 62 |
# File 'lib/interfacets/server/basic_router.rb', line 58 def klass_for(path) val = normalized_paths.fetch(normalize(path)) (val.is_a?(String) ? Object.const_get(val) : val) end |
#normalize(path) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/interfacets/server/basic_router.rb', line 71 def normalize(path) path &.sub(/^#{bus.root_url}/, "/") &.sub(/^/, "/") &.sub(%r{^/*}, "/") end |
#normalized_paths ⇒ Object
64 65 66 67 68 69 |
# File 'lib/interfacets/server/basic_router.rb', line 64 def normalized_paths @normalized_paths ||= ( paths .transform_keys { normalize(_1) } ) end |