Class: PathRouter::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/path_router/router.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



8
9
10
# File 'lib/path_router/router.rb', line 8

def initialize
  reset
end

Class Method Details

.instanceObject



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”, … }



26
27
28
29
# File 'lib/path_router/router.rb', line 26

def backends=(backends)
  backends.default = backends.values.first
  @backends = backends
end

#connect(backend, *patterns) ⇒ Object

Connect patterns to the given backend.



32
33
34
# File 'lib/path_router/router.rb', line 32

def connect(backend, *patterns)
  patterns.each { |p| @routes[p] = backend }
end

#load_routes(file = "path_router.rb") ⇒ Object



18
19
20
21
22
23
# File 'lib/path_router/router.rb', line 18

def load_routes(file = "path_router.rb")
  puts "Loading routes from #{file}"
  load file
rescue LoadError => e
  puts "Failed to load routes: " << e.message
end

#lookup(method, path) ⇒ Object

method e.g. “GET”, currently unused path e.g. “/some/path?including=query+string”



38
39
40
41
42
43
# File 'lib/path_router/router.rb', line 38

def lookup(method, path)
  _, backend = @routes.detect do |pattern, _|
    path =~ pattern
  end
  @backends[backend]
end

#resetObject



12
13
14
15
16
# File 'lib/path_router/router.rb', line 12

def reset
  @backends = {}
  @routes = {}
  self
end