Class: Tep::Route

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verb, pattern, handler) ⇒ Route

Returns a new instance of Route.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/tep/router.rb', line 14

def initialize(verb, pattern, handler)
  @r_verb    = verb
  @r_pat     = pattern
  @r_handler = handler
  @r_params  = []
  pattern.split("/").each do |part|
    if part.length > 0 && part[0] == ":"
      @r_params.push(part[1, part.length - 1])
    end
  end
end

Instance Attribute Details

#r_handlerObject

Returns the value of attribute r_handler.



12
13
14
# File 'lib/tep/router.rb', line 12

def r_handler
  @r_handler
end

#r_paramsObject

Returns the value of attribute r_params.



12
13
14
# File 'lib/tep/router.rb', line 12

def r_params
  @r_params
end

#r_patObject

Returns the value of attribute r_pat.



12
13
14
# File 'lib/tep/router.rb', line 12

def r_pat
  @r_pat
end

#r_verbObject

Returns the value of attribute r_verb.



12
13
14
# File 'lib/tep/router.rb', line 12

def r_verb
  @r_verb
end

Instance Method Details

#fold_captures(req) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/tep/router.rb', line 62

def fold_captures(req)
  if @r_handler.is_regex?
    caps = @r_handler.re_capture(req.path)
    i = 0
    while i < caps.length
      req.params[(i + 1).to_s] = caps[i]
      i += 1
    end
    return
  end
  pat = @r_pat.split("/")
  rp  = req.path.split("/")
  pi  = 0
  i = 0
  while i < pat.length
    pp = pat[i]
    if pp.length > 0 && pp[0] == ":"
      name = @r_params[pi]
      req.params[name] = Url.unescape(rp[i])
      pi += 1
    end
    i += 1
  end
end

#handlerObject



26
# File 'lib/tep/router.rb', line 26

def handler; @r_handler; end

#matches?(req_verb, req_path) ⇒ Boolean

Returns:

  • (Boolean)


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
53
54
55
56
57
58
59
60
# File 'lib/tep/router.rb', line 28

def matches?(req_verb, req_path)
  if req_verb != @r_verb
    return false
  end
  if @r_handler.is_regex?
    return @r_handler.re_match?(req_path)
  end
  pat = @r_pat.split("/")
  req = req_path.split("/")
  if pat.length != req.length
    return false
  end
  i = 0
  while i < pat.length
    pp = pat[i]
    rp = req[i]
    if pp.length > 0 && pp[0] == ":"
      if rp.length == 0
        return false
      end
    elsif pp == "*"
      if rp.length == 0
        return false
      end
    else
      if pp != rp
        return false
      end
    end
    i += 1
  end
  true
end