Sha256: 8eb017bf41bed48ac9c0c5aa66c1bb70d5680f599c13ab798202bb673f838b39

Contents?: true

Size: 906 Bytes

Versions: 1

Compression:

Stored size: 906 Bytes

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)
        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']))
        env['usher.params'] = response.params.inject({}){|h,(k,v)| h[k]=v; h }
        response.path.route.params.first.call(env)
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
joshbuddy-usher-0.4.3 lib/usher/interface/rack_interface.rb