Sha256: da6039a370065e083f7874d63ff080f367824f63257f76a45c7b53773895c2d3

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

require 'jellyfish/urlmap'

module Jellyfish
  class Builder
    def self.app app=nil, to=nil, &block
      new(app, &block).to_app(to)
    end

    def initialize app=nil, &block
      @use, @map, @run, @warmup = [], nil, app, nil
      instance_eval(&block) if block_given?
    end

    def use middleware, *args, &block
      if @map
        current_map, @map = @map, nil
        @use.unshift(lambda{ |app| generate_map(current_map, app) })
      end
      @use.unshift(lambda{ |app| middleware.new(app, *args, &block) })
    end

    def run app
      @run = app
    end

    def warmup lam=nil, &block
      @warmup = lam || block
    end

    def map path, to: nil, host: nil, &block
      key = if host then "http://#{File.join(host, path)}" else path end
      (@map ||= {})[key] = [block, to]
    end

    def listen host, &block
      map('', host: host, &block)
    end

    def rewrite rules, &block
      rules.each do |path, to|
        map(path, :to => to, &block)
      end
    end

    def to_app to=nil
      run = if @map then generate_map(@map, @run) else @run end
      fail 'missing run or map statement' unless run
      app = @use.inject(run){ |a, m| m.call(a) }
      result = if to then Rewrite.new(app, to) else app end
      @warmup.call(result) if @warmup
      result
    end

    private
    def generate_map current_map, app
      mapped = if app then {'' => app} else {} end
      current_map.each do |path, (block, to)|
        mapped[path] = self.class.app(app, to, &block)
      end
      URLMap.new(mapped)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jellyfish-1.2.2 lib/jellyfish/builder.rb
jellyfish-1.2.1 lib/jellyfish/builder.rb