Sha256: 3027898ecb10cdb6a1160d7ba413513258f0e90067e14b5fb333fb465aebfcb9

Contents?: true

Size: 970 Bytes

Versions: 7

Compression:

Stored size: 970 Bytes

Contents

# encoding: utf-8

require 'liquid/from_file'

class Router
  include FromFile

  attr_accessor :routes

  def initialize(request_handler)
    @request_handler = request_handler
    @routes = []

    @cache = Hash.new do |hash, path|
      hash[path] = nil
      @routes.each do |regex, block, args|
        match = path.match(regex)
        hash[path] = block.curry[match] if match
      end
      hash[path]
    end
  end

  def add(regexp, args, &block)
    @routes << [regexp, block, args]
  end

  # route %r(/foo/(.+)/(\w+)/(\d+)), AnyParser, :matches, :in, :order
  def route(regexp, parser, *args, &block)
    block = lambda do |match, env|
      params = args.each_with_index.inject({}) do |hash, (name, index)|
        hash[name] = match[index+1]
        hash
      end

      return @request_handler.handle(parser, env, params)
    end

    add(regexp, args, &block)
  end

  def handle(path, request)
    @cache[path].call(request) if @cache[path]
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
liquid-ext-3.2.0 lib/liquid/router.rb
liquid-ext-3.1.2 lib/liquid/router.rb
liquid-ext-3.1.1 lib/liquid/router.rb
liquid-ext-3.1.0 lib/liquid/router.rb
liquid-ext-3.0.0 lib/liquid/router.rb
liquid-ext-1.2.5 lib/liquid/router.rb
liquid-ext-1.2.4 lib/liquid/router.rb