Class: Rack::Lacquer

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/lacquer.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Lacquer

Returns a new instance of Lacquer.



6
7
8
# File 'lib/rack/lacquer.rb', line 6

def initialize app
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



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

def call env
  @env = env.deep_dup
  status, headers, response = @app.call env
  response.map! { |part| process_includes part } if headers['Content-Type'] =~ /text\/html/ && response.respond_to?( :map! )
  [status, headers, response]
end

#env_for(path_with_query) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/rack/lacquer.rb', line 27

def env_for path_with_query
  path_info, query_string = path_with_query.split('?')
  @env.deep_dup.merge({
    'PATH_INFO' => path_info,
    'REQUEST_URI' => path_with_query,
    'QUERY_STRING' => query_string.to_s
  })
end

#process_includes(part) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/rack/lacquer.rb', line 17

def process_includes part
  document = Nokogiri::HTML part.to_s
  document.css('include').each do |esi|
    status, headers, response = @app.call env_for(esi['src'])
    esi.after response.join
    esi.remove
  end
  document.to_s
end