Class: PlainRouter::Method

Inherits:
Object
  • Object
show all
Defined in:
lib/plainrouter/method.rb

Instance Method Summary collapse

Constructor Details

#initializeMethod

Returns a new instance of Method.



4
5
6
7
8
9
# File 'lib/plainrouter/method.rb', line 4

def initialize
  @router
  @data = {}
  @path = []
  @path_seen = {}
end

Instance Method Details

#add(methods, path, opaque = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/plainrouter/method.rb', line 11

def add(methods, path, opaque = nil)
  router = nil
  if @path_seen[path].nil?
    @path.push(path)
    @path_seen[path] = true
  end
  @data[path] ||= []
  methods = [methods] unless methods.instance_of?(Array)
  methods.each do |method|
    @data[path].push([method, opaque])
  end
end

#build_routerObject



47
48
49
50
51
52
53
# File 'lib/plainrouter/method.rb', line 47

def build_router
  router = PlainRouter.new
  @path.each do |path|
    router.add(path, @data[path])
  end
  return router
end

#match(request_method, path) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/plainrouter/method.rb', line 24

def match(request_method, path)
  router ||= self.build_router
  if response = router.match(path)
    patterns, captured = nil, nil
    if response.last.instance_of?(Array)
      patterns = response
    else
      patterns = response[0]
      captured = response[1]
    end
    patterns.each do |pattern|
      if pattern[0].nil? || request_method == pattern[0]
        if captured.nil?
          return pattern[1]
        else
          return pattern[1], captured
        end
      end
    end
  end
  return
end