grape-throttle 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/grape/middleware/throttle_middleware.rb +5 -0
- data/spec/simple_api_spec.rb +40 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ed50ef26f42b2a23a70ca4980f489a0aa84054d
|
4
|
+
data.tar.gz: 9637edba76b7b8962e6c94d0d45cc97e3fb9820d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53d9d218043ac41857ba6cbde85eca9069a557ef4374b57dc3882f08447d12f68ddcf6c5626a464de02eeb4d27d023136b532e32d669501e95c4e48e8517e1b7
|
7
|
+
data.tar.gz: 636e71131d1aca85d3ade3eb523654127aec25a7b6a8cfd26823e640bd7e64122c88c3756dfbfb5cbbb24ff7180b0872b303e3d3dac8811f70a293189fe2da8f
|
@@ -11,6 +11,11 @@ module Grape
|
|
11
11
|
period = 1.day
|
12
12
|
elsif limit = throttle_options[:monthly]
|
13
13
|
period = 1.month
|
14
|
+
elsif period = throttle_options[:period]
|
15
|
+
limit = throttle_options[:limit]
|
16
|
+
end
|
17
|
+
if limit.nil? || period.nil?
|
18
|
+
raise ArgumentError.new('Please set a period and limit (see documentation)')
|
14
19
|
end
|
15
20
|
|
16
21
|
user_key = options[:user_key]
|
data/spec/simple_api_spec.rb
CHANGED
@@ -9,6 +9,20 @@ describe "ThrottleHelper" do
|
|
9
9
|
get('/throttle') do
|
10
10
|
"step on it"
|
11
11
|
end
|
12
|
+
|
13
|
+
get('/no-throttle') do
|
14
|
+
"step on it"
|
15
|
+
end
|
16
|
+
|
17
|
+
throttle period: 10.minutes, limit: 3
|
18
|
+
get('/throttle-custom-period') do
|
19
|
+
"step on it"
|
20
|
+
end
|
21
|
+
|
22
|
+
throttle
|
23
|
+
get('/wrong-configuration') do
|
24
|
+
"step on it"
|
25
|
+
end
|
12
26
|
end
|
13
27
|
end
|
14
28
|
|
@@ -26,5 +40,31 @@ describe "ThrottleHelper" do
|
|
26
40
|
4.times { get "/throttle" }
|
27
41
|
expect(last_response.status).to eq(403)
|
28
42
|
end
|
43
|
+
|
44
|
+
describe "with custom period" do
|
45
|
+
|
46
|
+
it "is not throttled within the rate limit" do
|
47
|
+
3.times { get "/throttle-custom-period" }
|
48
|
+
expect(last_response.status).to eq(200)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "is throttled beyond the rate limit" do
|
52
|
+
4.times { get "/throttle-custom-period" }
|
53
|
+
expect(last_response.status).to eq(403)
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
it "throws an error if period or limit is missing" do
|
59
|
+
expect { get("wrong-configuration") }.to raise_exception
|
60
|
+
end
|
61
|
+
|
62
|
+
it "only throttles if explicitly specified" do
|
63
|
+
expect do
|
64
|
+
10.times { get "/no-throttle" }
|
65
|
+
end.not_to raise_exception
|
66
|
+
expect(last_response.status).to eq(200)
|
67
|
+
end
|
68
|
+
|
29
69
|
end
|
30
70
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-throttle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alejandro Wainzinger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05-
|
11
|
+
date: 2015-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack-test
|