Class: Rack::DevMark::Middleware

Inherits:
Object
  • Object
show all
Includes:
Utils, Utils
Defined in:
lib/rack/dev-mark/middleware.rb

Instance Method Summary collapse

Methods included from Utils

#camelize

Constructor Details

#initialize(app, themes = [:title, :github_fork_ribbon]) ⇒ Middleware

Returns a new instance of Middleware.



7
8
9
10
11
12
13
14
# File 'lib/rack/dev-mark/middleware.rb', line 7

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

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rack/dev-mark/middleware.rb', line 16

def call(env)
  Rack::DevMark.tmp_disabled = false

  status, headers, response = @app.call(env)

  read_headers = defined?(Rack::Headers) ? Rack::Headers.new(headers) : HeaderHash.new(headers)

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

  redirect = 300 <= status.to_i && status.to_i < 400
  if !redirect && !Rack::DevMark.tmp_disabled && read_headers['Content-Type'].to_s =~ %r{\btext/html\b}i
    new_body = ''
    response.each do |b|
      begin
        new_body << insert_dev_marks(b)
      rescue => e
        $stderr.write %Q|Failed to insert dev marks: #{e.message}\n  #{e.backtrace.join("  \n")}|
      end
    end
    response.close if response.respond_to?(:close)
    headers['Content-Length'] = new_body.bytesize.to_s if read_headers['Content-Length']
    response = [new_body]
  end

  [status, headers, response]
end