Sha256: 99ba8eadf228efed3e1829bf789ad332aea649db94206609506f34f8b767c4ea

Contents?: true

Size: 995 Bytes

Versions: 1

Compression:

Stored size: 995 Bytes

Contents

module Newark
  class Router

    FOUR_O_FOUR = [404, {}, []].freeze

    attr_accessor :request, :response

    def initialize(app, env)
      @app      = app
      @env      = env
      @request  = Request.new(env)
      @response = Response.new
    end

    def route!
      exec_before_hooks
      route = matched_route(@app.routes)
      if route
        request.params.merge!(route.params)
        response.body = instance_exec(&route.handler)
        exec_after_hooks
        response.finish
      else
        FOUR_O_FOUR
      end
    end

    def matched_route(routes)
      routes.find { |route| route.match?(request) }
    end

    def headers
      response.headers
    end

    def params
      request.params
    end

    private

    def exec_before_hooks
      exec_hooks @app.before_hooks
    end

    def exec_after_hooks
      exec_hooks @app.after_hooks
    end

    def exec_hooks(hooks)
      hooks.each do |hook|
        instance_exec(&hook)
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
newark-0.0.1 lib/newark/router.rb