Class: Mongrel::URIClassifier

Inherits:
Object
  • Object
show all
Defined in:
lib/mongrel/uri_classifier.rb

Defined Under Namespace

Classes: RegistrationError, UsageError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeURIClassifier

Returns a new instance of URIClassifier.



17
18
19
20
21
# File 'lib/mongrel/uri_classifier.rb', line 17

def initialize
  @handler_map = {}
  @matcher = //
  @root_handler = nil
end

Instance Attribute Details

#handler_mapObject (readonly)

Returns the value of attribute handler_map.



10
11
12
# File 'lib/mongrel/uri_classifier.rb', line 10

def handler_map
  @handler_map
end

Instance Method Details

#register(uri, handler) ⇒ Object

Register a handler object at a particular URI. The handler can be whatever you want, including an array. It’s up to you what to do with it.

Registering a handler is not necessarily threadsafe, so be careful if you go mucking around once the server is running.

Raises:



28
29
30
31
32
33
34
# File 'lib/mongrel/uri_classifier.rb', line 28

def register(uri, handler)
  raise RegistrationError, "#{uri.inspect} is already registered" if @handler_map[uri]
  raise RegistrationError, "URI is empty" if !uri or uri.empty?
  raise RegistrationError, "URI must begin with a \"#{Const::SLASH}\"" unless uri[0..0] == Const::SLASH
  @handler_map[uri.dup] = handler
  rebuild
end

#resolve(request_uri) ⇒ Object

Resolve a request URI by finding the best partial match in the registered handler URIs.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mongrel/uri_classifier.rb', line 46

def resolve(request_uri)
  if @root_handler
    # Optimization for the pathological case of only one handler on "/"; e.g. Rails
    [Const::SLASH, request_uri, @root_handler]
  elsif match = @matcher.match(request_uri)
    uri = match.to_s
    # A root mounted ("/") handler must resolve such that path info matches the original URI.
    [uri, (uri == Const::SLASH ? request_uri : match.post_match), @handler_map[uri]]
  else
    [nil, nil, nil]
  end
end

#unregister(uri) ⇒ Object

Unregister a particular URI and its handler.

Raises:



37
38
39
40
41
42
# File 'lib/mongrel/uri_classifier.rb', line 37

def unregister(uri)
  handler = @handler_map.delete(uri)
  raise RegistrationError, "#{uri.inspect} was not registered" unless handler
  rebuild
  handler
end

#urisObject

Returns the URIs that have been registered with this classifier so far.



13
14
15
# File 'lib/mongrel/uri_classifier.rb', line 13

def uris
  @handler_map.keys
end