Class: Rack::Tidy::Cleaner

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

Overview

This class is the interface between Rack and the Tidy gem

Constant Summary collapse

DEFAULT_TIDY_OPTS =

Defaults for the Tidy gem config

{
'char-encoding'     => 'utf8',
'indent'            => true,
'indent-spaces'     => 2,
'tidy-mark'         => false,
'wrap'              => 0 }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Cleaner.



21
22
23
24
25
26
# File 'lib/rack/tidy/cleaner.rb', line 21

def initialize(app, options = {})
  ::Tidy.path = TIDY_LIB
  @app = app
  self.ignore_paths = options[:ignore_paths] || []
  self.tidy_options = DEFAULT_TIDY_OPTS.merge(options)
end

Instance Attribute Details

#ignore_pathsObject

Paths to be ignored during Rack::Tidy processing



19
20
21
# File 'lib/rack/tidy/cleaner.rb', line 19

def ignore_paths
  @ignore_paths
end

#tidy_optionsObject



16
17
18
# File 'lib/rack/tidy/cleaner.rb', line 16

def tidy_options
  @tidy_options
end

Instance Method Details

#call(env) ⇒ Object

method required by Rack interface



29
30
31
# File 'lib/rack/tidy/cleaner.rb', line 29

def call(env)
  call! env
end

#call!(env) ⇒ Object

thread safe version using shallow copy of env



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rack/tidy/cleaner.rb', line 34

def call!(env)
  @env = env.dup
  status, @headers, response = @app.call(@env)      
  if should_clean?
    @headers.delete('Content-Length')
    response = Rack::Response.new(
      tidy_markup(response.respond_to?(:body) ? response.body : response),
      status,
      @headers
    )
    response.finish
    response.to_a
  else
    [status, @headers, response]
  end
end