Module: QNA::RoutingMethods

Included in:
Request
Defined in:
lib/qna/routing.rb

Constant Summary collapse

@@regexp_cache =
{}

Instance Method Summary collapse

Instance Method Details

#is(route = '/', &block) ⇒ Object



36
37
38
39
40
# File 'lib/qna/routing.rb', line 36

def is(route = '/', &block)
  return unless @__routing_path__ == route

  route_found(&block)
end

#on(route = nil, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/qna/routing.rb', line 23

def on(route = nil, &block)
  @__routing_path__ ||= path

  if route
    regexp = (@@regexp_cache[route] ||= /^\/#{route}(\/.*)?/)
    return unless @__routing_path__ =~ regexp

    @__routing_path__ = Regexp.last_match(1) || '/'
  end
  
  route_found(&block)
end

#on_accept(accept, &block) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/qna/routing.rb', line 79

def on_accept(accept, &block)
  if accept.is_a?(Regexp)
    return unless headers['accept'] =~ accept
  else
    return unless headers['accept'] == accept
  end

  route_found(&block)
end

#on_get(route = nil, &block) ⇒ Object



48
49
50
51
52
# File 'lib/qna/routing.rb', line 48

def on_get(route = nil, &block)
  return unless method == 'get'
  
  on(route, &block)
end

#on_options(route = nil, &block) ⇒ Object



60
61
62
63
64
# File 'lib/qna/routing.rb', line 60

def on_options(route = nil, &block)
  return unless method == 'options'
  
  on(route, &block)
end

#on_post(route = nil, &block) ⇒ Object



54
55
56
57
58
# File 'lib/qna/routing.rb', line 54

def on_post(route = nil, &block)
  return unless method == 'post'
  
  on(route, &block)
end

#on_query_param(key) ⇒ Object



72
73
74
75
76
77
# File 'lib/qna/routing.rb', line 72

def on_query_param(key)
  value = query[key]
  return unless value

  route_found { yield value }
end

#on_root(&block) ⇒ Object



42
43
44
45
46
# File 'lib/qna/routing.rb', line 42

def on_root(&block)
  return unless @__routing_path__ == '/'

  route_found(&block)
end

#on_upgrade(protocol, &block) ⇒ Object



66
67
68
69
70
# File 'lib/qna/routing.rb', line 66

def on_upgrade(protocol, &block)
  return unless upgrade_protocol == protocol

  route_found(&block)
end

#route(&block) ⇒ Object



5
6
7
8
9
10
# File 'lib/qna/routing.rb', line 5

def route(&block)
  res = catch(:stop) { yield self }
  return if res == :found
  
  respond(nil, ':status' => 404)
end

#route_found(&block) ⇒ Object



12
13
14
15
# File 'lib/qna/routing.rb', line 12

def route_found(&block)
  catch(:stop, &block)
  throw :stop, :found
end

#routing_pathObject



19
20
21
# File 'lib/qna/routing.rb', line 19

def routing_path
  @__routing_path__
end

#stop_routingObject



89
90
91
92
# File 'lib/qna/routing.rb', line 89

def stop_routing
  yield if block_given?
  throw :stop, :found
end