Sha256: 8bd47adf524e8f9eaab1725f0e79a3940016ab4254d73e3d07f67fc27b869a16

Contents?: true

Size: 1.3 KB

Versions: 7

Compression:

Stored size: 1.3 KB

Contents

module Rack
  module DevMark
    class Middleware
      include Rack::Utils
      include Rack::DevMark::Utils

      def initialize(app, themes = [:title, :github_fork_ribbon])
        @app = app
        @themes = [themes].flatten.map do |theme|
          theme = theme.is_a?(Symbol) ? Rack::DevMark::Theme.const_get(camelize(theme.to_s)).new : theme
          theme.setup Rack::DevMark.env, Rack::DevMark.revision
          theme
        end
      end
      
      def call(env)
        status, headers, response = @app.call(env)

        headers = HeaderHash.new(headers)

        headers['X-Rack-Dev-Mark-Env'] = Rack::DevMark.env

        if headers['Content-Type'].to_s =~ %r{^text/html;}i
          new_body = ''
          response.each do |b|
            begin
              new_body << insert_dev_marks(b)
            rescue => e
              $stderr.write "Failed to insert dev marks: #{e.message}\n"
            end
          end
          response.close if response.respond_to?(:close)
          headers['Content-Length'] &&= bytesize(new_body).to_s
          response = [new_body]
        end

        [status, headers, response]
      end

      private

      def insert_dev_marks(body)
        @themes.each do |theme|
          body = theme.insert_into(body)
        end
        body
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rack-dev-mark-0.4.6 lib/rack/dev-mark/middleware.rb
rack-dev-mark-0.4.5 lib/rack/dev-mark/middleware.rb
rack-dev-mark-0.4.4 lib/rack/dev-mark/middleware.rb
rack-dev-mark-0.4.3 lib/rack/dev-mark/middleware.rb
rack-dev-mark-0.4.2 lib/rack/dev-mark/middleware.rb
rack-dev-mark-0.4.1 lib/rack/dev-mark/middleware.rb
rack-dev-mark-0.4.0 lib/rack/dev-mark/middleware.rb