Class: Rack::Minify

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/rack/minify.rb

Constant Summary collapse

FORMAT =
%{[HtmlMini #{HtmlMini::VERSION}] %s - %s - [%s] %s "%s%s %s"\n}

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ Minify

Returns a new instance of Minify.



11
12
13
14
15
# File 'lib/rack/minify.rb', line 11

def initialize(app, opts={})
  @app = app
  @bench = opts[:bench] || true
  @logger = opts[:logger]
end

Instance Method Details

#_call(env) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rack/minify.rb', line 21

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

  headers = HeaderHash.new(headers)

  if !STATUS_WITH_NO_ENTITY_BODY.include?(status) &&
      !headers['transfer-encoding'] &&
      headers['content-type'] &&
      headers['content-type'].include?("text/html")

    body = ""
    response.each { |part| body += part }

    start, size_before = Time.now, body.size if @bench

    html = HtmlMini.minify(body) rescue nil

    stop = Time.now and log(env, (stop-start).seconds, size_before, html.size) if @bench

    headers['Content-Length'] = html.size.to_s
    headers['Html-Mini'] = "version #{HtmlMini::VERSION}, time #{(stop-start).seconds}s, size #{html.size-size_before} bytes" if @bench

    response = [html] unless html.nil?
  end

  [status, headers, response]
end

#call(env) ⇒ Object



17
18
19
# File 'lib/rack/minify.rb', line 17

def call(env)
  dup._call(env)
end