Class: Nice::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/nice/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



24
25
26
# File 'lib/nice/middleware.rb', line 24

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/nice/middleware.rb', line 28

def call(env)
  @doc = nil
  @referer = nil

  status, @headers, @body = @app.call(env)
  
  @is_js = Rack::Request.new(env).xhr?

  if html? || @is_js
  	 @referer = env["HTTP_REFERER"]
  	 @method = env["REQUEST_METHOD"]
  	 @path = env["PATH_INFO"]

  	 # in case 2+3 the response will be plain javascript
  	 if @is_js
  	 	@headers = {"Content-Type" => "text/javascript"}
  	 end

    [status, @headers, self]
  else
    [status, @headers, @body]
  end
end

#each(&block) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/nice/middleware.rb', line 52

def each(&block)
  if html? || @is_js
    block.call("<!-- previous state: #{@referer} -->\n")
    block.call( Nice::Logic.run( @method, @path, @referer, doc, @is_js) )
  else
    block.call(@body)
  end
end