Parent

Files

HttpRouter::Route

Attributes

conditions[R]
default_values[R]
dest[R]
match_partially[R]
match_partially?[R]
matches_with[R]
named[R]
original_path[R]
path[R]
regex[R]
regex?[R]
router[R]

Public Class Methods

new(router, path, opts = {}) click to toggle source
# File lib/http_router/route.rb, line 7
def initialize(router, path, opts = {})
  @router, @original_path, @opts = router, path, opts
  if @original_path
    @match_partially = true and path.slice!(-1) if @original_path[/[^\\]\*$/]
    @original_path[0, 0] = '/'                  if @original_path[0] != //
  else
    @match_partially = true
  end
  process_opts
end

Public Instance Methods

add_to_contitions(name, *vals) click to toggle source
# File lib/http_router/route.rb, line 74
def add_to_contitions(name, *vals)
  ((@conditions ||= {})[name] ||= []).concat(vals.flatten)
  self
end
arbitrary(blk = nil, &blk2) click to toggle source
# File lib/http_router/route.rb, line 121
def arbitrary(blk = nil, &blk2)
  arbitrary_with_continue { |req, params|
    req.continue[(blk || blk2)[req, params]]
  }
end
arbitrary_with_continue(blk = nil, &blk2) click to toggle source
# File lib/http_router/route.rb, line 127
def arbitrary_with_continue(blk = nil, &blk2)
  (@arbitrary ||= []) << (blk || blk2)
  self
end
as_options() click to toggle source
# File lib/http_router/route.rb, line 32
def as_options
  {:__matching__ => @matches_with, :__conditions__ => @conditions, :__default_values__ => @default_values, :__name__ => @named, :__partial__ => @partially_match, :__arbitrary__ => @arbitrary}
end
clone(new_router) click to toggle source
# File lib/http_router/route.rb, line 137
def clone(new_router)
  Route.new(new_router, @original_path.dup, as_options)
end
compiled?() click to toggle source
# File lib/http_router/route.rb, line 36
def compiled?
  !@paths.nil?
end
conenct() click to toggle source
# File lib/http_router/route.rb, line 119
def conenct; request_method('CONNECT');         end
default(defaults) click to toggle source
# File lib/http_router/route.rb, line 84
def default(defaults)
  (@default_values ||= {}).merge!(defaults)
  self
end
delete() click to toggle source
# File lib/http_router/route.rb, line 114
def delete;  request_method('DELETE');          end
get() click to toggle source
# File lib/http_router/route.rb, line 112
def get;     request_method('GET');             end
head() click to toggle source
# File lib/http_router/route.rb, line 115
def head;    request_method('HEAD');            end
host(*host) click to toggle source
# File lib/http_router/route.rb, line 62
def host(*host)
  add_to_contitions(:host, host)
end
matching(matchers) click to toggle source
# File lib/http_router/route.rb, line 79
def matching(matchers)
  @matches_with.merge!(matchers.is_a?(Array) ? Hash[*matchers] : matchers)
  self
end
matching_path(params, other_hash = nil) click to toggle source
# File lib/http_router/route.rb, line 162
def matching_path(params, other_hash = nil)
  return @paths.first if @paths.size == 1
  case params
  when Array
    significant_keys = other_hash && significant_variable_names & other_hash.keys
    @paths.find { |path| path.param_names.size == (significant_keys ? params.size + significant_keys.size : params.size) }
  when Hash
    @paths.find { |path| (params && !params.empty? && (path.param_names & params.keys).size == path.param_names.size) || path.param_names.empty? }
  end
end
name(n) click to toggle source
# File lib/http_router/route.rb, line 51
def name(n)
  @named = n
  @router.named_routes[n] << self
  @router.named_routes[n].sort!{|r1, r2| r2.significant_variable_names.size <=> r1.significant_variable_names.size }
  self
end
options() click to toggle source
# File lib/http_router/route.rb, line 116
def options; request_method('OPTIONS');         end
partial(match_partially = true) click to toggle source
# File lib/http_router/route.rb, line 40
def partial(match_partially = true)
  @match_partially = match_partially
  self
end
patch() click to toggle source
# File lib/http_router/route.rb, line 117
def patch;   request_method('PATCH');           end
post() click to toggle source
# File lib/http_router/route.rb, line 111
def post;    request_method('POST');            end
process_opts() click to toggle source
# File lib/http_router/route.rb, line 18
def process_opts
  @default_values = @opts[:__default_values__] || @opts[:default_values] || {}
  @arbitrary = @opts[:__arbitrary__] || @opts[:arbitrary]
  @matches_with = significant_variable_names.include?(:matching) ? @opts : @opts[:__matching__] || @opts[:matching] || {}
  significant_variable_names.each do |name|
    @matches_with[name] = @opts[name] if @opts.key?(name) && !@matches_with.key?(name)
  end
  @conditions = @opts[:__conditions__] || @opts[:conditions] || {}
  @match_partially = @opts[:__partial__] if @match_partially.nil? && !@opts[:__partial__].nil?
  @match_partially = @opts[:partial] if @match_partially.nil? && !@opts[:partial].nil?
  name(@opts[:__name__] || @opts[:name]) if @opts.key?(:__name__) || @opts.key?(:name)
  @needed_keys = significant_variable_names - @default_values.keys
end
put() click to toggle source
# File lib/http_router/route.rb, line 113
def put;     request_method('PUT');             end
redirect(path, status = 302) click to toggle source

Sets the destination of this route to redirect to an arbitrary URL.

# File lib/http_router/route.rb, line 90
def redirect(path, status = 302)
  raise ArgumentError, "Status has to be an integer between 300 and 399" unless (300..399).include?(status)
  to { |env|
    params = env['router.params']
    response = ::Rack::Response.new
    response.redirect(eval(%"#{path}"|), status)
    response.finish
  }
  self
end
request_method(*method) click to toggle source
# File lib/http_router/route.rb, line 58
def request_method(*method)
  add_to_contitions(:request_method, method)
end
scheme(*scheme) click to toggle source
# File lib/http_router/route.rb, line 66
def scheme(*scheme)
  add_to_contitions(:scheme, scheme)
end
significant_variable_names() click to toggle source
# File lib/http_router/route.rb, line 158
def significant_variable_names
  @significant_variable_names ||= @original_path.nil? ? [] : @original_path.scan(/(^|[^\\])[:\*]([a-zA-Z0-9_]+)/).map{|p| p.last.to_sym}
end
static(root) click to toggle source

Sets the destination of this route to serve static files from either a directory or a single file.

# File lib/http_router/route.rb, line 102
def static(root)
  if File.directory?(root)
    partial.to ::Rack::File.new(root)
  else
    to {|env| env['PATH_INFO'] = File.basename(root); ::Rack::File.new(File.dirname(root)).call(env) }
  end
  self
end
to(dest = nil, &dest2) click to toggle source
# File lib/http_router/route.rb, line 45
def to(dest = nil, &dest2)
  @dest = dest || dest2
  add_path_to_tree
  self
end
to_s() click to toggle source
# File lib/http_router/route.rb, line 173
def to_s
  "#<HttpRouter:Route #{object_id} @original_path=#{@original_path.inspect} @conditions=#{@conditions.inspect} @arbitrary=#{@arbitrary.inspect}>"
end
trace() click to toggle source
# File lib/http_router/route.rb, line 118
def trace;   request_method('TRACE');           end
url(*args) click to toggle source
# File lib/http_router/route.rb, line 132
def url(*args)
  result, extra_params = url_with_params(*args)
  append_querystring(result, extra_params)
end
url_args_processing(args) click to toggle source
# File lib/http_router/route.rb, line 149
def url_args_processing(args)
  options = args.last.is_a?(Hash) ? args.pop : nil
  options = options.nil? ? default_values.dup : default_values.merge(options) if default_values
  options.delete_if{ |k,v| v.nil? } if options
  result, params = yield args, options
  mount_point = router.url_mount && router.url_mount.url(options)
  mount_point ? [File.join(mount_point, result), params] : [result, params]
end
url_with_params(*a) click to toggle source
# File lib/http_router/route.rb, line 141
def url_with_params(*a)
  url_args_processing(a) do |args, options|
    path = args.empty? ? matching_path(options) : matching_path(args, options)
    raise InvalidRouteException unless path
    path.url(args, options)
  end
end
user_agent(*user_agent) click to toggle source
# File lib/http_router/route.rb, line 70
def user_agent(*user_agent)
  add_to_contitions(:user_agent, user_agent)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.