Sha256: 85b0534ca7d33d1ff3bed55082bb716873b8bafdfa393e7881eb1a9b2d9f354c

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

require "rack/mock"
require "fileutils"

module Rack
  class Cat
    def initialize(app, options = {})
      @app         = app
      @bundles     = options[:bundles]     # { "/destination/path" => [ "/array/of", "/source/paths"] }
      @destination = options[:destination] # a directory to write bundles into
      @sources     = options[:sources]     # array with source dirs
      @debug       = options[:debug]       # regenerate bundles on each request
      
      create_bundles unless @debug
    end
    
    def call(env)
      create_bundles(Rack::Request.new(env).path) if @debug
      @app.call(env)
    end
    
    private
    
    def bundle_path(request_path)
      @bundles.keys.detect { |bp| request_path.start_with?(bp) }
    end

    def create_bundles(request_path = nil)
      if request_path
        if bp = bundle_path(request_path)
          create_bundle(bp)
        end
      else
        @bundles.keys.each do |bp|
          create_bundle(bp)
        end
      end
    end
    
    def create_bundle(bundle_path)
      concatenation = @bundles[bundle_path].map do |path|
        read_from_disk(path) || read_from_app(path)
      end.join("\n")
      
      write_to_disk(bundle_path, concatenation)
    end
    
    def write_to_disk(path, content)
      full_path = ::File.join(@destination, path)
      FileUtils.mkdir_p(::File.dirname(full_path))

      ::File.open(full_path, "w") do |f|
        f.write(content)
      end
    end
    
    def read_from_disk(path)
      @sources.each do |source|
        full_path = ::File.join(source, Rack::Utils.unescape(path))
        return ::File.read(full_path) if ::File.file?(full_path) && ::File.readable?(full_path)
      end
      
      return # return nil (instead of @sources) when no file found
    end

    def read_from_app(path)
      Rack::MockRequest.new(@app).get(path).body
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-cat-0.1.2 lib/rack/cat.rb