prop 1.0.2 → 1.1.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
  SHA1:
3
- metadata.gz: c6876580ba3b335c884fbf9f549931b1a200f8be
4
- data.tar.gz: 062fa3bf0d3f237e09ad258a1257559581bdfcf5
3
+ metadata.gz: c5753d3ad85cbb0e81257570a59b12362e7da611
4
+ data.tar.gz: 7ff4f5c528bfe42bc8b15a0a7e9193e6e13238bb
5
5
  SHA512:
6
- metadata.gz: 75bf0a8a9c115a60e1ffaafa00aaf14c28837174a4a6efbb84183838963bc022c53dc5625bdfdb9ea71145b63a4edfd3127d6684744e5ad993c3b4d09d9231b2
7
- data.tar.gz: ed67700ce432878bd7790145779f309d6a7b82192421cd5bbefe99133f6d99e3f385a5ec7a17360e79de8a05835cc60c5cda3bc0dacf0e5b8b15f47234ac7ebd
6
+ metadata.gz: b1b0d1f4239241457b30e60bd80db2301d47b38c1f6de67cbacce29f16de80d777d91ba21f1043f5f73f6f532fbdc487076d3fc97f8f5ffa20d4da3299a8a380
7
+ data.tar.gz: 160b608c60ed35e72426f2c053d4a8d77643cd1506dfe9a742819b23005373ac5550a4bb7f9bcab67f406ac70d04594f085027e17fc6da45990cce44bf76d6a8
data/README.md CHANGED
@@ -142,10 +142,37 @@ Prop.throttle!(:mails_per_hour, nil)
142
142
  The default (and smallest possible) increment is 1, you can set that to any integer value using :increment which is handy for building time based throttles:
143
143
 
144
144
  ```ruby
145
- Prop.setup(:execute_time, :threshold => 10, :interval => 1.minute)
145
+ Prop.configure(:execute_time, :threshold => 10, :interval => 1.minute)
146
146
  Prop.throttle!(:execute_time, account.id, :increment => (Benchmark.realtime { execute }).to_i)
147
147
  ```
148
148
 
149
+ ## Optional configuration
150
+
151
+ You can add optional configuration to a prop and retrieve it using `Prop.configurations[:foo]`:
152
+
153
+ ```ruby
154
+ Prop.configure(:api_query, :threshold => 10, :interval => 1.minute, :category => :api)
155
+ Prop.configure(:api_insert, :threshold => 50, :interval => 1.minute, :category => :api)
156
+ Prop.configure(:password_failure, :threshold => 5, :interval => 1.minute, :category => :auth)
157
+ ```
158
+
159
+ ```
160
+ Prop.configurations[:api_query][:category]
161
+ ```
162
+
163
+ You can use `Prop::RateLimited#config` to distinguish between errors:
164
+
165
+ ```ruby
166
+ rescue Prop::RateLimited => e
167
+ case e.config[:category]
168
+ when :api
169
+ raise APIRateLimit
170
+ when :auth
171
+ raise AuthFailure
172
+ ...
173
+ end
174
+ ```
175
+
149
176
  ## License
150
177
 
151
178
  Copyright 2013 Zendesk
data/lib/prop.rb CHANGED
@@ -2,12 +2,12 @@ require "prop/limiter"
2
2
  require "forwardable"
3
3
 
4
4
  module Prop
5
- VERSION = "1.0.2"
5
+ VERSION = "1.1.0"
6
6
 
7
7
  # Short hand for accessing Prop::Limiter methods
8
8
  class << self
9
9
  extend Forwardable
10
- def_delegators :"Prop::Limiter", :read, :write, :configure, :disabled, :before_throttle
10
+ def_delegators :"Prop::Limiter", :read, :write, :configure, :configurations, :disabled, :before_throttle
11
11
  def_delegators :"Prop::Limiter", :throttle, :throttle!, :throttled?, :count, :query, :reset
12
12
  end
13
13
  end
data/lib/prop/limiter.rb CHANGED
@@ -127,6 +127,11 @@ module Prop
127
127
  end
128
128
  alias :query :count
129
129
 
130
+ def handles
131
+ @handles ||= {}
132
+ end
133
+ alias :configurations :handles
134
+
130
135
  private
131
136
 
132
137
  def at_threshold?(mark, threshold)
@@ -146,7 +151,6 @@ module Prop
146
151
 
147
152
  [ options, cache_key ]
148
153
  end
149
-
150
154
  end
151
155
  end
152
156
  end
@@ -15,5 +15,9 @@ module Prop
15
15
  self.cache_key = cache_key
16
16
  self.retry_after = interval - Time.now.to_i % interval
17
17
  end
18
+
19
+ def config
20
+ Prop.configurations[@handle]
21
+ end
18
22
  end
19
23
  end
data/prop.gemspec CHANGED
@@ -1,7 +1,7 @@
1
- Gem::Specification.new "prop", "1.0.2" do |s|
1
+ Gem::Specification.new "prop", "1.1.0" do |s|
2
2
  s.name = 'prop'
3
- s.version = '1.0.2'
4
- s.date = '2012-04-24'
3
+ s.version = '1.1.0'
4
+ s.date = '2014-12-31'
5
5
  s.rubyforge_project = 'prop'
6
6
  s.license = "Apache License Version 2.0"
7
7
 
data/test/test_limiter.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require 'helper'
2
2
 
3
-
4
3
  describe Prop::Limiter do
5
4
  before do
6
5
  @store = {}
data/test/test_prop.rb CHANGED
@@ -111,12 +111,14 @@ describe Prop do
111
111
 
112
112
  describe "#throttle!" do
113
113
  it "increment counter correctly" do
114
+ Prop.configure(:hello, :threshold => 20, :interval => 20)
114
115
  3.times do |i|
115
116
  assert_equal (i + 1), Prop.throttle!(:hello, nil, :threshold => 10, :interval => 10)
116
117
  end
117
118
  end
118
119
 
119
120
  it "reset counter when time window is passed" do
121
+ Prop.configure(:hello, :threshold => 20, :interval => 20)
120
122
  3.times do |i|
121
123
  assert_equal (i + 1), Prop.throttle!(:hello, nil, :threshold => 10, :interval => 10)
122
124
  end
@@ -192,4 +194,12 @@ describe Prop do
192
194
  end
193
195
  end
194
196
 
197
+ describe "#configurations" do
198
+ it "returns the configuration" do
199
+ Prop.configure(:something, :threshold => 100, :interval => 30)
200
+ config = Prop.configurations[:something]
201
+ assert_equal 100, config[:threshold]
202
+ assert_equal 30, config[:interval]
203
+ end
204
+ end
195
205
  end
@@ -1,19 +1,27 @@
1
1
  require 'helper'
2
2
 
3
3
  describe Prop::RateLimited do
4
- describe "#initialize" do
5
- before do
6
- time = Time.at(1333685680)
7
- Time.stubs(:now).returns(time)
4
+ before do
5
+ time = Time.at(1333685680)
6
+ Time.stubs(:now).returns(time)
8
7
 
9
- @error = Prop::RateLimited.new(:handle => "foo", :threshold => 10, :interval => 60, :cache_key => "wibble", :description => "Boom!")
10
- end
8
+ Prop.configure :foo, :threshold => 10, :interval => 60, :category => :api
9
+
10
+ @error = Prop::RateLimited.new(
11
+ :handle => :foo,
12
+ :threshold => 10,
13
+ :interval => 60,
14
+ :cache_key => "wibble",
15
+ :description => "Boom!"
16
+ )
17
+ end
11
18
 
12
- it "return an error instance" do
19
+ describe "#initialize" do
20
+ it "returns an error instance" do
13
21
  assert @error.is_a?(StandardError)
14
22
  assert @error.is_a?(Prop::RateLimited)
15
23
 
16
- assert_equal "foo", @error.handle
24
+ assert_equal :foo, @error.handle
17
25
  assert_equal "wibble", @error.cache_key
18
26
  assert_equal "Boom!", @error.description
19
27
  assert_equal "foo threshold of 10 tries per 60s exceeded for key 'nil', hash wibble", @error.message
@@ -21,4 +29,11 @@ describe Prop::RateLimited do
21
29
  end
22
30
  end
23
31
 
32
+ describe "#config" do
33
+ it "returns the original configuration" do
34
+ assert_equal 10, @error.config[:threshold]
35
+ assert_equal 60, @error.config[:interval]
36
+ assert_equal :api, @error.config[:category]
37
+ end
38
+ end
24
39
  end
metadata CHANGED
@@ -1,69 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prop
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Morten Primdahl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2012-04-24 00:00:00.000000000 Z
11
+ date: 2014-12-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: mocha
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  description: Gem for implementing rate limits.
@@ -72,10 +72,10 @@ executables: []
72
72
  extensions: []
73
73
  extra_rdoc_files: []
74
74
  files:
75
- - .document
76
- - .gemtest
77
- - .gitignore
78
- - .travis.yml
75
+ - ".document"
76
+ - ".gemtest"
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
79
  - Gemfile
80
80
  - LICENSE
81
81
  - README.md
@@ -104,17 +104,17 @@ require_paths:
104
104
  - lib
105
105
  required_ruby_version: !ruby/object:Gem::Requirement
106
106
  requirements:
107
- - - '>='
107
+ - - ">="
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
110
  required_rubygems_version: !ruby/object:Gem::Requirement
111
111
  requirements:
112
- - - '>='
112
+ - - ">="
113
113
  - !ruby/object:Gem::Version
114
114
  version: '0'
115
115
  requirements: []
116
116
  rubyforge_project: prop
117
- rubygems_version: 2.0.14
117
+ rubygems_version: 2.2.2
118
118
  signing_key:
119
119
  specification_version: 4
120
120
  summary: Gem for implementing rate limits.