Class: HttpRouter::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/http_router/node.rb,
lib/http_router/node/glob.rb,
lib/http_router/node/regex.rb,
lib/http_router/node/request.rb,
lib/http_router/node/variable.rb,
lib/http_router/node/arbitrary.rb,
lib/http_router/node/free_regex.rb,
lib/http_router/node/glob_regex.rb,
lib/http_router/node/spanning_regex.rb

Direct Known Subclasses

Arbitrary, FreeRegex, Glob, Regex, Request, Variable

Defined Under Namespace

Classes: Arbitrary, FreeRegex, Glob, GlobRegex, Regex, Request, SpanningRegex, Variable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(router) ⇒ Node

Returns a new instance of Node.



14
15
16
# File 'lib/http_router/node.rb', line 14

def initialize(router)
  @router = router
end

Instance Attribute Details

#priorityObject (readonly)

Returns the value of attribute priority.



12
13
14
# File 'lib/http_router/node.rb', line 12

def priority
  @priority
end

#routerObject (readonly)

Returns the value of attribute router.



12
13
14
# File 'lib/http_router/node.rb', line 12

def router
  @router
end

Instance Method Details

#[](request) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/http_router/node.rb', line 18

def [](request)
  destination(request, false)
  unless request.path.empty?
    linear(request)
    lookup(request)
    variable(request)
    glob(request)
  end
  destination(request)
end

#add_arbitrary(blk, allow_partial, param_names) ⇒ Object



130
131
132
133
134
# File 'lib/http_router/node.rb', line 130

def add_arbitrary(blk, allow_partial, param_names)
  @arbitrary ||= []
  @arbitrary << Arbitrary.new(@router, allow_partial, blk, param_names)
  @arbitrary.last
end

#add_destination(route) ⇒ Object



150
151
152
153
# File 'lib/http_router/node.rb', line 150

def add_destination(route)
  @destination ||= []
  @destination << route
end

#add_free_match(regexp) ⇒ Object



144
145
146
147
148
# File 'lib/http_router/node.rb', line 144

def add_free_match(regexp)
  @linear ||= []
  @linear << FreeRegex.new(@router, regexp)
  @linear.last
end

#add_globObject



90
91
92
# File 'lib/http_router/node.rb', line 90

def add_glob
  @glob ||= Glob.new(@router)
end

#add_lookup(part) ⇒ Object



155
156
157
158
# File 'lib/http_router/node.rb', line 155

def add_lookup(part)
  @lookup ||= {}
  @lookup[part] ||= Node.new(@router)
end

#add_match(regexp, matching_indicies = [0], priority = 0, splitting_indicies = nil) ⇒ Object



136
137
138
# File 'lib/http_router/node.rb', line 136

def add_match(regexp, matching_indicies = [0], priority = 0, splitting_indicies = nil)
  add_prioritized_match(Regex.new(@router, regexp, matching_indicies, priority, splitting_indicies))
end

#add_request(opts) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/http_router/node.rb', line 94

def add_request(opts)
  @request ||= Request.new(@router)
  next_requests = [@request]
  Request.request_methods.each do |method|
    method_index = Request.request_methods.index(method)
    next_requests.map! do |next_request|
      if opts[method].nil? && next_request.request_method.nil?
        next_request
      else
        next_request_index = next_request.request_method && Request.request_methods.index(next_request.request_method)
        rank = next_request_index ? method_index <=> next_request_index : 0
        case rank
        when 0
          next_request.request_method = method
          (opts[method].nil? ? [nil] : Array(opts[method])).map do |request_matcher|
            case request_matcher
            when nil
              next_request.add_catchall
            when String
              next_request.add_lookup(request_matcher)
            when Regexp
              next_request.add_linear(request_matcher)
            end
          end
        when -1
          next_request
        when 1
          next_request.transform_to(method)
        end
      end
    end
    next_requests.flatten!
  end
  next_requests
end

#add_spanning_match(regexp, matching_indicies = [0], priority = 0, splitting_indicies = nil) ⇒ Object



140
141
142
# File 'lib/http_router/node.rb', line 140

def add_spanning_match(regexp, matching_indicies = [0], priority = 0, splitting_indicies = nil)
  add_prioritized_match(SpanningRegex.new(@router, regexp, matching_indicies, priority, splitting_indicies))
end

#add_variableObject



86
87
88
# File 'lib/http_router/node.rb', line 86

def add_variable
  @variable ||= Variable.new(@router)
end

#arbitrary(request) ⇒ Object



52
53
54
# File 'lib/http_router/node.rb', line 52

def arbitrary(request)
  @arbitrary && @arbitrary.each{|n| n[request]}
end

#destination(request_obj, match_partially = true) ⇒ Object



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

def destination(request_obj, match_partially = true)
  request(request_obj)
  arbitrary(request_obj)
  if match_partially or request_obj.path.empty?
    @destination && @destination.each do |d|
      if request_obj.path.empty? or d.route.match_partially? or (@router.ignore_trailing_slash? and request_obj.path.size == 1 and request_obj.path.last == '')
        if request_obj.perform_call
          env = request_obj.rack_request.dup.env
          env['router.params'] ||= {}
          env['router.params'].merge!(d.hashify_params(request_obj.params))
          matched = if d.route.match_partially?
            env['PATH_INFO'] = "/#{request_obj.path.join('/')}"
            env['SCRIPT_NAME'] += request_obj.rack_request.path_info[0, request_obj.rack_request.path_info.size - env['PATH_INFO'].size]
          else
            env["PATH_INFO"] = ''
            env["SCRIPT_NAME"] += request_obj.rack_request.path_info
          end
          throw :success, d.route.dest.call(env)
        else
          throw :success, Response.new(request_obj, d)
        end
      end
    end
  end
end

#glob(request) ⇒ Object



44
45
46
# File 'lib/http_router/node.rb', line 44

def glob(request)
  @glob && @glob[request]
end

#join_whole_path(request) ⇒ Object



160
161
162
# File 'lib/http_router/node.rb', line 160

def join_whole_path(request)
  request.path * '/'
end

#linear(request) ⇒ Object



29
30
31
# File 'lib/http_router/node.rb', line 29

def linear(request)
  @linear && @linear.each{|n| n[request]}
end

#lookup(request) ⇒ Object



33
34
35
36
37
38
# File 'lib/http_router/node.rb', line 33

def lookup(request)
  if @lookup && @lookup[request.path.first]
    request = request.clone
    @lookup[request.path.shift][request]
  end
end

#request(request) ⇒ Object



48
49
50
# File 'lib/http_router/node.rb', line 48

def request(request)
  @request && @request[request]
end

#unescape(val) ⇒ Object



56
57
58
# File 'lib/http_router/node.rb', line 56

def unescape(val)
  val.to_s.gsub(/((?:%[0-9a-fA-F]{2})+)/n){ [$1.delete('%')].pack('H*') }
end

#variable(request) ⇒ Object



40
41
42
# File 'lib/http_router/node.rb', line 40

def variable(request)
  @variable && @variable[request]
end