Class: Rack::Validate

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-validate.rb,
lib/rack-validate/validator.rb,
lib/rack-validate/response_file.rb

Overview

A rack middleware for validating HTML via w3c validator

Defined Under Namespace

Classes: ResponseFile, Validator

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Validate

Returns a new instance of Validate.



11
12
13
# File 'lib/rack-validate.rb', line 11

def initialize( app )
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rack-validate.rb', line 15

def call( env )
  status, headers, body = @app.call(env)
  resp = Rack::Response.new(body, status, headers)
  
  request = Rack::Request.new( env )
  if request.params['rack-validate'] == "true"
    if headers['Content-Type'] =~ /text\/html|application\/xhtml\+xml/
      body = resp.body.dup
    
      issues = Validator.validate( body )
    
      body.insert( 0, Validator.generate_report( issues ) )
    
      resp.body = ""
      resp.write(body)
    end
  end
  
  resp.to_a
end