Class: Rack::Block

Inherits:
Object
  • Object
show all
Includes:
DSL::Matchers, DSL::Responses
Defined in:
lib/rack/block.rb,
lib/rack/block/dsl.rb,
lib/rack/block/version.rb

Defined Under Namespace

Modules: DSL

Constant Summary collapse

VERSION =
"0.2.0"

Constants included from DSL::Matchers

DSL::Matchers::BUILTIN_BOT_PATTERN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DSL::Responses

#detect_matching_pattern, #dummy_app, #halt, #redirect

Methods included from DSL::Matchers

#bot_access, #ip_pattern, #path, #ua_pattern

Constructor Details

#initialize(app, &b) ⇒ Block

Returns a new instance of Block.



9
10
11
12
13
14
15
16
17
18
# File 'lib/rack/block.rb', line 9

def initialize(app, &b)
  @_current_matching_type       = nil
  @_current_matching_ua_pattern = nil
  @_current_matching_ip_pattern = nil
  @_current_matching_path       = nil
  @ua_matchers = {}
  @ip_matchers = {}
  instance_eval(&b)
  @app = app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



20
21
22
# File 'lib/rack/block.rb', line 20

def app
  @app
end

#ip_matchersObject

Returns the value of attribute ip_matchers.



21
22
23
# File 'lib/rack/block.rb', line 21

def ip_matchers
  @ip_matchers
end

#ua_matchersObject

Returns the value of attribute ua_matchers.



21
22
23
# File 'lib/rack/block.rb', line 21

def ua_matchers
  @ua_matchers
end

Instance Method Details

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rack/block.rb', line 23

def call(env)
  req = Rack::Request.new(env)
  self.ua_matchers.each_pair do |pattern, path_matchers|
    if pattern =~ req.user_agent
      path_matchers.each_pair do |path, action_with_args|
        if path =~ req.path_info
          action, *args = action_with_args
          return send action, req, *args
        end
      end
    end
  end

  self.ip_matchers.each_pair do |pattern, path_matchers|
    if pattern =~ req.ip
      path_matchers.each_pair do |path, action_with_args|
        if path =~ req.path_info
          action, *args = action_with_args
          return send action, req, *args
        end
      end
    end
  end

  app.call(env)
end