grape-throttling 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/README.md +6 -1
- data/grape-throttling.gemspec +1 -0
- data/lib/grape/throttling.rb +1 -0
- data/lib/grape/throttling/configuration.rb +3 -1
- data/lib/grape/throttling/extension/api.rb +8 -2
- data/lib/grape/throttling/version.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d7af84a0a0c54a3c7fdf73b4af2be49ee97f1360b0de6d5ce3c2871ecc2f45c
|
4
|
+
data.tar.gz: 505fd42572b665a149c7b15c8801fea4f12337fac7326578977183528ae6e8cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 046b944e3d314770bfd3b467b052871f8b3d3294f3d04a7474cb500b42bf4a612ad0c2433b9d6c17e728886f54999928563c22968ba0e2e362a2f08917e63317
|
7
|
+
data.tar.gz: df97788c0d8f9253995035b1ca916a92a60d4a0a9b3ffd19f6def85d77dad98d7aa1791d8d504a8d7513ba9c255a861996787e8be5095dea51cb06b636804a84
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,10 @@
|
|
2
2
|
|
3
3
|
## master
|
4
4
|
|
5
|
+
### 0.1.2 (2020-12-24)
|
6
|
+
|
7
|
+
- Allow custom overspeed message ([#1](https://github.com/OuYangJinTing/grape-throttling/pull/1/files))
|
8
|
+
|
5
9
|
### 0.1.1 (2020-11-05)
|
6
10
|
|
7
|
-
-
|
11
|
+
- Down grade support ruby >= 2.3.0
|
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
[](https://travis-ci.org/OuYangJinTing/grape-throttling)
|
4
4
|
[](https://badge.fury.io/rb/grape-throttling)
|
5
|
+
[](https://codeclimate.com/github/OuYangJinTing/grape-throttling/maintainability)
|
6
|
+
[](https://codeclimate.com/github/OuYangJinTing/grape-throttling/test_coverage)
|
5
7
|
|
6
8
|
If your api through [`grape`](https://github.com/ruby-grape/grape) build, you can use `grape-throttling` limit api rate.
|
7
9
|
|
@@ -48,9 +50,12 @@ end
|
|
48
50
|
## Configure
|
49
51
|
|
50
52
|
```ruby
|
51
|
-
#
|
53
|
+
# Default configuration
|
52
54
|
Grape::Throttling.configure do |config|
|
53
55
|
config.redis = ::Redis.new(url: 'redis://localhost:6379/0', driver: :hiredis)
|
56
|
+
# Api overspeed access message custom method.
|
57
|
+
# If doesn't respond,default message is "API rate limit exceeded."
|
58
|
+
config.overspeed_message_method = :overspeed_message
|
54
59
|
end
|
55
60
|
```
|
56
61
|
|
data/grape-throttling.gemspec
CHANGED
@@ -32,6 +32,7 @@ Gem::Specification.new do |spec|
|
|
32
32
|
spec.add_runtime_dependency 'grape', '>= 0.16.1'
|
33
33
|
spec.add_runtime_dependency 'redis-namespace', '>= 0.8.0'
|
34
34
|
spec.add_runtime_dependency 'hiredis'
|
35
|
+
spec.add_runtime_dependency 'activesupport', '>= 3'
|
35
36
|
|
36
37
|
spec.add_development_dependency 'rake'
|
37
38
|
spec.add_development_dependency 'rack-test'
|
data/lib/grape/throttling.rb
CHANGED
@@ -3,10 +3,12 @@
|
|
3
3
|
module Grape
|
4
4
|
module Throttling
|
5
5
|
class Configuration # :nodoc:
|
6
|
-
attr_reader :redis
|
6
|
+
attr_reader :redis, :overspeed_message_method
|
7
|
+
attr_writer :overspeed_message_method
|
7
8
|
|
8
9
|
def initialize
|
9
10
|
@redis = ::Redis.new(url: 'redis://localhost:6379/0', driver: :hiredis)
|
11
|
+
@overspeed_message_method = :overspeed_message
|
10
12
|
end
|
11
13
|
|
12
14
|
def redis=(redis)
|
@@ -8,7 +8,7 @@ module Grape
|
|
8
8
|
|
9
9
|
module ClassMethods # :nodoc:
|
10
10
|
# rubocop:disable Style/ClassVars
|
11
|
-
def use_throttle(max: 60, expire: 1.day, condition: proc { true }, identity: proc { request.ip }) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
|
11
|
+
def use_throttle(max: 60, expire: 1.day, condition: proc { true }, identity: proc { request.ip }) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
12
12
|
max = max.try(:to_i).to_i
|
13
13
|
raise ArgumentError, 'max must be positive number' unless max.positive?
|
14
14
|
raise ArgumentError, 'condition must be Proc' unless condition.is_a?(Proc)
|
@@ -36,7 +36,13 @@ module Grape
|
|
36
36
|
request.id_throttle = instance_eval(&@@throttle.identity)
|
37
37
|
count = @@throttle.get(request.id_throttle)
|
38
38
|
|
39
|
-
|
39
|
+
message = if respond_to?(Throttling.config.overspeed_message_method)
|
40
|
+
send(Throttling.config.overspeed_message_method)
|
41
|
+
else
|
42
|
+
'API rate limit exceeded.'
|
43
|
+
end
|
44
|
+
|
45
|
+
error!(message, 403, mixin_throttle_headers(count)) if count >= @@throttle.max
|
40
46
|
end
|
41
47
|
end
|
42
48
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-throttling
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- OuYangJinTing
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-12-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: grape
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rake
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|