Class: RackTidyFFI

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-tidy-ffi.rb,
lib/rack-tidy-ffi/version.rb

Constant Summary collapse

VERSION =
"0.2.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RackTidyFFI

Returns a new instance of RackTidyFFI.



7
8
9
# File 'lib/rack-tidy-ffi.rb', line 7

def initialize(app)
  @app = app
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



5
6
7
# File 'lib/rack-tidy-ffi.rb', line 5

def headers
  @headers
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rack-tidy-ffi.rb', line 11

def call(env)
  @status, @headers, @response = @app.call(env)
  if @headers["Content-Type"].include? "text/html"
    body = if @response.is_a? Array
      @response.first
    elsif @response.is_a? Rack::BodyProxy
      @response.body.first
    else
      @response.body
    end
    response = TidyMachine.new(body).tidy
    response = "<!DOCTYPE HTML>\n#{response}"
    @headers["Content-Length"] = response.length.to_s
    @response = [response]
  end
  [@status, @headers, @response]
end