Class: Utopia::Middleware::Filter

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

Overview

This class filters a incoming request and only executes a given block if it matches the given filter path.

Instance Method Summary collapse

Constructor Details

#initialize(app, filter, &block) ⇒ Filter

Returns a new instance of Filter.



11
12
13
14
15
16
17
# File 'lib/utopia/middleware/filter.rb', line 11

def initialize(app, filter, &block)
  @app = app
  @filter = filter
  branch = Rack::Builder.new(&block)
  branch.run(@app)
  @process = branch.to_app
end

Instance Method Details

#applicable(request) ⇒ Object



19
20
21
# File 'lib/utopia/middleware/filter.rb', line 19

def applicable(request)
  return request.path.index(@filter) != nil
end

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/utopia/middleware/filter.rb', line 23

def call(env)
  request = Rack::Request.new(env)

  if applicable(request)
    @process.call(env)
  else
    @app.call(env)
  end
end