Class: PathRouter::Router
- Inherits:
-
Object
- Object
- PathRouter::Router
- Defined in:
- lib/path_router/router.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#backends=(backends) ⇒ Object
Hash of backends, { Regexp => “ip:port”, … }.
-
#connect(backend, *patterns) ⇒ Object
Connect patterns to the given backend.
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #load_routes(file = "path_router.rb") ⇒ Object
-
#lookup(method, path) ⇒ Object
method e.g.
- #reset ⇒ Object
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
8 9 10 |
# File 'lib/path_router/router.rb', line 8 def initialize reset end |
Class Method Details
.instance ⇒ Object
4 5 6 |
# File 'lib/path_router/router.rb', line 4 def self.instance @instance ||= new end |
Instance Method Details
#backends=(backends) ⇒ Object
Hash of backends, { Regexp => “ip:port”, … }
27 28 29 30 |
# File 'lib/path_router/router.rb', line 27 def backends=(backends) backends.default = backends.values.first @backends = backends end |
#connect(backend, *patterns) ⇒ Object
Connect patterns to the given backend.
33 34 35 |
# File 'lib/path_router/router.rb', line 33 def connect(backend, *patterns) patterns.each { |p| @routes[p] = backend } end |
#load_routes(file = "path_router.rb") ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/path_router/router.rb', line 18 def load_routes(file = "path_router.rb") file = File.join(Dir.pwd, file) puts "Loading routes from #{file}" load file rescue LoadError => e puts "Failed to load routes: " << e. end |
#lookup(method, path) ⇒ Object
method e.g. “GET”, currently unused path e.g. “/some/path?including=query+string”
39 40 41 42 43 44 |
# File 'lib/path_router/router.rb', line 39 def lookup(method, path) _, backend = @routes.detect do |pattern, _| path =~ pattern end @backends[backend] end |
#reset ⇒ Object
12 13 14 15 16 |
# File 'lib/path_router/router.rb', line 12 def reset @backends = {} @routes = {} self end |