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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3aab77fdedb6a77b7a4de1583352c11f8eb73d797fe3bc67f8075436257100ac
4
- data.tar.gz: 67fabd18bd8324a7d2a1f8df795b5a9507d6a3ff6f7ea164c14d582d91f99051
3
+ metadata.gz: 4d7af84a0a0c54a3c7fdf73b4af2be49ee97f1360b0de6d5ce3c2871ecc2f45c
4
+ data.tar.gz: 505fd42572b665a149c7b15c8801fea4f12337fac7326578977183528ae6e8cf
5
5
  SHA512:
6
- metadata.gz: 20ba672eca5e35767111fc8c042cfb870a3677df47ffcc87dba8dc82885d135a8bf1165a92cf966249f38ed7b33152b2733668347e7143be0f8c19b81f013563
7
- data.tar.gz: 7e4fc918914fe7e9d6b1a2aef41a8440cdae79fe405a2012fb34318ae01e4e3c4bf7339b994af0b7c72ebd9fe88d7a64e363889467818629db0cb899366493c0
6
+ metadata.gz: 046b944e3d314770bfd3b467b052871f8b3d3294f3d04a7474cb500b42bf4a612ad0c2433b9d6c17e728886f54999928563c22968ba0e2e362a2f08917e63317
7
+ data.tar.gz: df97788c0d8f9253995035b1ca916a92a60d4a0a9b3ffd19f6def85d77dad98d7aa1791d8d504a8d7513ba9c255a861996787e8be5095dea51cb06b636804a84
@@ -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
- - Dimension reduction support ruby >= 2.3.0
11
+ - Down grade support ruby >= 2.3.0
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  [![Build Status](https://travis-ci.org/OuYangJinTing/grape-throttling.svg)](https://travis-ci.org/OuYangJinTing/grape-throttling)
4
4
  [![Gem Version](https://badge.fury.io/rb/grape-throttling.svg)](https://badge.fury.io/rb/grape-throttling)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/f200c46569aa627315f6/maintainability)](https://codeclimate.com/github/OuYangJinTing/grape-throttling/maintainability)
6
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/f200c46569aa627315f6/test_coverage)](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
- # default configuration
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
 
@@ -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'
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'grape'
4
4
  require 'redis-namespace'
5
+ require 'active_support/all'
5
6
 
6
7
  require 'grape/throttling/counter'
7
8
  require 'grape/throttling/extension/api'
@@ -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
- error!('API rate limit exceeded.', 403, mixin_throttle_headers(count)) if count >= @@throttle.max
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
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Grape
4
4
  module Throttling
5
- VERSION = '0.1.1'
5
+ VERSION = '0.1.2'
6
6
  end
7
7
  end
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.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-05 00:00:00.000000000 Z
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