Sha256: 374e283340daf303045f1a59570d3eb0735c98dcf901ce220446560fa3f18c7d
Contents?: true
Size: 1.57 KB
Versions: 2
Compression:
Stored size: 1.57 KB
Contents
require 'journey' module NYNY class Router attr_reader :scope_class, :journey, :before_hooks, :after_hooks, :fallback def initialize options @scope_class = options[:scope_class] @before_hooks = options[:before_hooks] @after_hooks = options[:after_hooks] @fallback = options[:fallback] prepare_for_journey(options[:route_defs]) end def call env response = journey.call(env) if response[0] == 404 and fallback fallback.call(env) else response end end private def prepare_for_journey route_defs @journey = Journey::Router.new(Journey::Routes.new, { :parameters_key => 'nyny.params' }) route_defs.each do |path, options, handler| pat = Journey::Path::Pattern.new(path) constraints = options.fetch(:constraints, {}) defaults = options.fetch(:defaults, {}) @journey.routes.add_route compile(handler), pat, constraints, defaults end end def compile handler Proc.new do |env| request = Request.new(env) request.params.merge! env["nyny.params"] request.params.default_proc = lambda do |h, k| h.fetch(k.to_s, nil) || h.fetch(k.to_sym, nil) end scope = scope_class.new(request) response = catch (:halt) do before_hooks.each {|h| scope.instance_eval &h } scope.apply_to &handler end catch (:halt) do after_hooks.each {|h| scope.instance_eval &h } end response end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
nyny-3.2.1 | lib/nyny/router.rb |
nyny-3.2.0 | lib/nyny/router.rb |