Sha256: 5ec3458f1908f6b78b5344e2e2494bda49c94b602b30e1e6b3dc41bea8fdb2d0

Contents?: true

Size: 1.56 KB

Versions: 4

Compression:

Stored size: 1.56 KB

Contents

require 'active_support/concern'
require 'action_dispatch/routing'
require 'action_dispatch/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
      routes = ActionDispatch::Journey::Routes.new
      @journey = ActionDispatch::Journey::Router.new(routes, {
        :parameters_key => 'nyny.params',
        :request_class => NYNY::Request
      })

      route_defs.each do |path, options, handler|
        pat         = ActionDispatch::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|
        scope = scope_class.new(env)

        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.finish
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
nyny-3.4.3 lib/nyny/router.rb
nyny-3.4.2 lib/nyny/router.rb
nyny-3.4.1 lib/nyny/router.rb
nyny-3.4.0 lib/nyny/router.rb