Class: RackInliner

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-roadie/rack_inliner.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ RackInliner



2
3
4
5
# File 'lib/middleman-roadie/rack_inliner.rb', line 2

def initialize(app, options = {})
  @app = app
  @path = /^(\/#{ options[:path] }\/)/
end

Instance Method Details

#call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/middleman-roadie/rack_inliner.rb', line 7

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

  if env['PATH_INFO'].match(@path) && headers.key?('Content-Type') and headers['Content-Type'].start_with?('text/html')
    content = ''

    body.each do |part|
      content << part
    end

    email = transform_html(content)

    headers['Content-Length'] = Rack::Utils.bytesize(email).to_s if headers['Content-Length']
    [status, headers, [email]]
  else
    [status, headers, body]
  end
ensure
  body.close if body.respond_to?(:close)
end