rack-throttle 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a65f5195779a2af8c7340f1e52dd0c19e6036e234eeaf934518a307b05a208e4
4
- data.tar.gz: e69a0f3ee004f59b3a6808f9165a2c82b1729dde0ed344e7192998eb5c522f3a
3
+ metadata.gz: ff38a72f8ae91886b4c8db0cf2abab7ed33d908f19bf676b2d013f06a270d846
4
+ data.tar.gz: 3bed6513dc33088624b4abfca5ab9edd56bee3aabfab7fec69d08dc627084efa
5
5
  SHA512:
6
- metadata.gz: 0e0a36ff42888850b8e1532133706342637e070998d815ff5358a7a36235a3c7a4eabe5f0514ad463bdb7fea13e84ce4ce21b63e47f7352be5c03877c4c3ffb1
7
- data.tar.gz: 4d84b627410ff12bf7a81ac3ad19c681397287748d4d9306d1d5f447a918849488b04bb1b33f1057018a4519abbc309460aa109772193c7c4a81e13c23fe65bf
6
+ metadata.gz: 8a158cf9982fe6790481e861974a34a5c496d861c5af791b72d07548375a94bd7228bb9a63ad9736433316d86d0ca552690bd764a3eb758222edaa728213703f
7
+ data.tar.gz: ae8658e3cc10b7ae80ea684f6b8cb4e34876f9c4915416d37f71f556a3e497a02ac5ca2d277dd4fa35c71cc07b368c109af6167b3cf8d11e57beb4406841b43b
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.0
1
+ 0.7.0
@@ -14,7 +14,7 @@ module Rack
14
14
  def rules
15
15
  @rules ||= begin
16
16
  rs = options[:rules]
17
- rs.sort_by { |r| r[:path].to_s }.reverse
17
+ rs.sort_by { |r| [r[:proc].to_s, r[:path].to_s] }.reverse
18
18
  end
19
19
  end
20
20
 
@@ -32,7 +32,7 @@ module Rack
32
32
 
33
33
  def whitelisted?(request)
34
34
  return true if ip_whitelisted?(IPAddr.new(ip(request)))
35
- return true if path_whitelisted?(request)
35
+ return true if rule_whitelisted?(request)
36
36
  false
37
37
  end
38
38
 
@@ -40,7 +40,7 @@ module Rack
40
40
  !!ips.find { |ip| ip.include?(request_ip) }
41
41
  end
42
42
 
43
- def path_whitelisted?(request)
43
+ def rule_whitelisted?(request)
44
44
  rule = rule_for(request)
45
45
  rule ? rule[:whitelisted] : false
46
46
  end
@@ -48,7 +48,8 @@ module Rack
48
48
  def rule_for(request)
49
49
  rules.find do |rule|
50
50
  next unless rule[:method] == request.request_method.to_s
51
- next unless path_matches?(rule, request.path.to_s)
51
+ next if rule[:proc] && rule[:proc].call(request) == false
52
+ next if rule[:path] && !path_matches?(rule, request.path.to_s)
52
53
  rule
53
54
  end
54
55
  end
@@ -66,11 +67,23 @@ module Rack
66
67
 
67
68
  def client_identifier(request)
68
69
  if (rule = rule_for(request))
69
- "#{ip(request)}_#{rule[:method]}_#{rule[:path]}"
70
+ client_identifier_for_rule(request, rule)
70
71
  else
71
72
  ip(request)
72
73
  end
73
74
  end
75
+
76
+ def client_identifier_for_rule(request, rule)
77
+ if rule[:proc]
78
+ "#{rule[:method]}_#{rule[:proc].call(request)}"
79
+ elsif rule[:path]
80
+ "#{ip(request)}_#{rule[:method]}_#{rule[:path]}"
81
+ elsif rule[:method]
82
+ "#{ip(request)}_#{rule[:method]}"
83
+ else
84
+ raise NotImplementedError
85
+ end
86
+ end
74
87
 
75
88
  def ip(request)
76
89
  request.ip.to_s
@@ -1,7 +1,7 @@
1
1
  module Rack; module Throttle
2
2
  module VERSION
3
3
  MAJOR = 0
4
- MINOR = 6
4
+ MINOR = 7
5
5
  TINY = 0
6
6
  EXTRA = nil
7
7
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-throttle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arto Bendiken
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-20 00:00:00.000000000 Z
11
+ date: 2018-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler