Sha256: 182f227b4657e6d2d4f142ed5aa9de288bae34195fa42bd62e04d20ea7baabba

Contents?: true

Size: 1.26 KB

Versions: 8

Compression:

Stored size: 1.26 KB

Contents

module RhetButler
  module Web
    class AssetsApp
      def initialize(file_manager)
        @file_manager = file_manager
      end

      attr_reader :file_manager

      class AssetsContext
        def initialize(template_handler)
          @template_handler = template_handler
        end

        def render(path, locals = nil)
          template = @template_handler.find(path).contents
          if template.respond_to? :render
            template.render(self, locals)
          else
            template
          end
        end
      end

      def template_handler
        file_manager.base_assets(::Tilt::Cache.new)
      end

      def assets_context
        AssetsContext.new(template_handler)
      end

      def call(env)
        asset_path = env["PATH_INFO"]
        asset_path.sub!(/^\//,"")
        extension = asset_path.sub(/.*[.]/, ".")

        mime_type = Rack::Mime.mime_type(extension, "text/plain")
        [200, {'Content-Type' => mime_type}, [assets_context.render(asset_path)]]
      rescue Object => ex
        puts ex
        raise
      end
    end

    class MemoizedAssetsApp < AssetsApp
      def template_handler
        @template_handler ||= super
      end

      def assets_context
        @assets_contenxt ||= super
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rhet-butler-0.14.1 lib/rhet-butler/web/assets-app.rb
rhet-butler-0.14.0 lib/rhet-butler/web/assets-app.rb
rhet-butler-0.13.0 lib/rhet-butler/web/assets-app.rb
rhet-butler-0.12.1 lib/rhet-butler/web/assets-app.rb
rhet-butler-0.11.1 lib/rhet-butler/web/assets-app.rb
rhet-butler-0.11.0 lib/rhet-butler/web/assets-app.rb
rhet-butler-0.10.0 lib/rhet-butler/web/assets-app.rb
rhet-butler-0.9.0 lib/rhet-butler/web/assets-app.rb