module Qeweney::RoutingMethods

Public Instance Methods

default() { || ... } click to toggle source
# File lib/qeweney/routing.rb, line 131
def default
  yield
  throw :stop, :found
end
enter_route(depth = 1) click to toggle source
# File lib/qeweney/routing.rb, line 32
def enter_route(depth = 1)
  @path_parts_idx += depth
end
is(route = '/', &block) click to toggle source
# File lib/qeweney/routing.rb, line 52
def is(route = '/', &block)
  return unless @path_parts[@path_parts_idx] == route && @path_parts_idx >= @path_parts.size

  route_found(&block)
end
leave_route(depth = 1) click to toggle source
# File lib/qeweney/routing.rb, line 36
def leave_route(depth = 1)
  @path_parts_idx -= depth
end
on(route, &block) click to toggle source
# File lib/qeweney/routing.rb, line 40
def on(route, &block)
  return route_found(&block) unless route
  
  route_parts = route.split('/')
  route_length = route_parts.size
  return unless @path_parts[@path_parts_idx, route_length] == route_parts
  
  enter_route(route_length)
  route_found(&block)
  leave_route(route_length)
end
on_accept(accept, &block) click to toggle source
# File lib/qeweney/routing.rb, line 107
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) click to toggle source
# File lib/qeweney/routing.rb, line 76
def on_get(route = nil, &block)
  return unless method == 'get'
  
  on(route, &block)
end
on_host(route, &block) click to toggle source
# File lib/qeweney/routing.rb, line 64
def on_host(route, &block)
  return unless host == route

  route_found(&block)
end
on_options(route = nil, &block) click to toggle source
# File lib/qeweney/routing.rb, line 88
def on_options(route = nil, &block)
  return unless method == 'options'
  
  on(route, &block)
end
on_plain_http(route, &block) click to toggle source
# File lib/qeweney/routing.rb, line 70
def on_plain_http(route, &block)
  return unless scheme == 'http'

  route_found(&block)
end
on_post(route = nil, &block) click to toggle source
# File lib/qeweney/routing.rb, line 82
def on_post(route = nil, &block)
  return unless method == 'post'
  
  on(route, &block)
end
on_query_param(key) { |value| ... } click to toggle source
# File lib/qeweney/routing.rb, line 100
def on_query_param(key)
  value = query[key]
  return unless value

  route_found { yield value }
end
on_root(&block) click to toggle source
# File lib/qeweney/routing.rb, line 58
def on_root(&block)
  return unless @path_parts_idx > @path_parts.size - 1

  route_found(&block)
end
on_upgrade(protocol, &block) click to toggle source
# File lib/qeweney/routing.rb, line 94
def on_upgrade(protocol, &block)
  return unless upgrade_protocol == protocol

  route_found(&block)
end
on_websocket_upgrade(&block) click to toggle source
# File lib/qeweney/routing.rb, line 123
def on_websocket_upgrade(&block)
  on_upgrade('websocket', &block)
end
route() { |self| ... } click to toggle source
# File lib/qeweney/routing.rb, line 9
def route(&block)
  (@path_parts ||= path.split('/'))[@path_parts_idx ||= 1]
  res = catch(:stop) { yield self }
  return if res == :found
  
  respond(nil, ':status' => 404)
end
route_found(&block) click to toggle source
# File lib/qeweney/routing.rb, line 17
def route_found(&block)
  catch(:stop, &block)
  throw :stop, :found
end
route_part() click to toggle source
# File lib/qeweney/routing.rb, line 24
def route_part
  @path_parts[@path_parts_idx]
end
route_relative_path() click to toggle source
# File lib/qeweney/routing.rb, line 28
def route_relative_path
  @path_parts.empty? ? '/' : "/#{@path_parts[@path_parts_idx..-1].join('/')}"
end
stop_routing() click to toggle source
# File lib/qeweney/routing.rb, line 127
def stop_routing
  throw :stop, :found
end