Sha256: d757574230d8b66da579078dcf2972714c7eb2b881cfc408cb9831f681876941

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

$:.unshift File.dirname(__FILE__)

require 'rack_interface/route'

class Usher
  module Interface
    class RackInterface
      
      RequestMethods = [:method, :host, :port, :scheme]
      Request = Struct.new(:path, *RequestMethods)
      
      attr_accessor :routes
      
      def initialize(&blk)
        @routes = Usher.new(:request_methods => RequestMethods)
        @generator = Usher::Generators::URL.new(@routes)
        instance_eval(&blk) if blk
      end
      
      def add(path, options = nil)
        @routes.add_route(path, options)
      end

      def reset!
        @routes.reset!
      end

      def call(env)
        response = @routes.recognize(Request.new(env['REQUEST_URI'], env['REQUEST_METHOD'].downcase, env['HTTP_HOST'], env['SERVER_PORT'].to_i, env['rack.url_scheme']))
        params = {}
        response.params.each{ |hk| params[hk.first] = hk.last}
        env['usher.params'] = params
        response.path.route.destination.call(env)
      end

      def generate(route, params = nil, options = nil)
        @generator.generate(route, params, options)
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
joshbuddy-usher-0.4.5 lib/usher/interface/rack_interface.rb
joshbuddy-usher-0.4.6 lib/usher/interface/rack_interface.rb
joshbuddy-usher-0.4.7 lib/usher/interface/rack_interface.rb