grape-throttler 1.0.0 → 1.2.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: ff44f961112b47773a75e7c195b909e33938d0ada801d01ddd981f1f8ca8c854
4
- data.tar.gz: e3422fd5c83dbec806d859f62c1bc5a6560fbe0da905b1fc4d6acbe13bb101b7
3
+ metadata.gz: 23649e5064815579ccbe89ac57e8cd0c89801e4b0d61408f6c22316306c460ce
4
+ data.tar.gz: 54c9cc293c8282098623d99a90ad269cbf8b93cfd9213a3bb973e46826cfeb89
5
5
  SHA512:
6
- metadata.gz: 3ee5867e71b9de391fea4a5e37385b522abe52c724aa261f4f48510b30b076c417bd76eab1b1de27c1ee742096b3e493c7dcc92453242708574780873a8a216e
7
- data.tar.gz: 47544b35bd03f981dd5c7dffa4f5c8cc8a9e155968507ded18c60e9971b9626f45e4f965382fc55b6af9e7c35be0d1c58e29eea0285df2f27a3c5bf932a6a054
6
+ metadata.gz: 2bd7d4a974958130752b80b70a9fd478381935bd900f86314d056035014fffcb4600b89cf69ee88dc4b443f941c229a311a2ddeda152947a5b7e1158dd57b326
7
+ data.tar.gz: a53096891e5edab11b5c9f132c9a2aa81e636b4dabe0ffe02a1b75e7a3c6c80bd3c1c4f4faaac5458f37ba3ec49553424482a9d6c540bc8e260386054017b059
data/.fasterer.yml CHANGED
@@ -1,19 +1,19 @@
1
1
  speedups:
2
- rescue_vs_respond_to: false
3
- module_eval: true
4
- shuffle_first_vs_sample: true
5
- for_loop_vs_each: true
2
+ block_vs_symbol_to_proc: true
6
3
  each_with_index_vs_while: false
7
- map_flatten_vs_flat_map: true
8
- reverse_each_vs_reverse_each: true
9
- select_first_vs_detect: true
10
- sort_vs_sort_by: true
11
4
  fetch_with_argument_vs_block: true
12
- keys_each_vs_each_key: true
5
+ for_loop_vs_each: true
6
+ getter_vs_attr_reader: true
7
+ gsub_vs_tr: true
13
8
  hash_merge_bang_vs_hash_brackets: true
14
- block_vs_symbol_to_proc: true
9
+ keys_each_vs_each_key: true
10
+ map_flatten_vs_flat_map: true
11
+ module_eval: true
15
12
  proc_call_vs_yield: true
16
- gsub_vs_tr: true
13
+ rescue_vs_respond_to: true
14
+ reverse_each_vs_reverse_each: true
15
+ select_first_vs_detect: true
17
16
  select_last_vs_reverse_detect: true
18
- getter_vs_attr_reader: true
19
17
  setter_vs_attr_writer: true
18
+ shuffle_first_vs_sample: true
19
+ sort_vs_sort_by: true
data/.gitignore CHANGED
@@ -1,13 +1,11 @@
1
1
  /.bundle/
2
2
  /.yardoc
3
- /Gemfile.lock
4
3
  /_yardoc/
5
4
  /coverage/
6
5
  /doc/
7
6
  /pkg/
8
7
  /spec/reports/
9
8
  /tmp/
10
- /.irbrc_history
11
9
 
12
10
  # rspec failure tracking
13
11
  .rspec_status
data/.rubocop.yml CHANGED
@@ -1,39 +1,47 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rake
4
+ - rubocop-rspec
1
5
  AllCops:
6
+ TargetRubyVersion: 3.0
7
+ NewCops: enable
2
8
  DisplayCopNames: true
3
9
  DisplayStyleGuide: true
4
- TargetRubyVersion: 2.5
10
+ Gemspec/RequiredRubyVersion:
11
+ Enabled: false
12
+ Layout/EmptyLinesAroundAttributeAccessor:
13
+ Enabled: true
14
+ Layout/EmptyLinesAroundBlockBody:
5
15
  Exclude:
6
- - 'lib/**/**/templates/*'
7
16
  - 'spec/**/**/*'
8
17
  Layout/EmptyLinesAroundClassBody:
9
- Enabled: false
18
+ EnforcedStyle: empty_lines_except_namespace
10
19
  Layout/EmptyLinesAroundModuleBody:
11
- Enabled: false
12
- LineLength:
20
+ EnforcedStyle: empty_lines_except_namespace
21
+ Layout/LineLength:
13
22
  Max: 100
14
- Lint/AssignmentInCondition:
15
- Enabled: false
16
- Lint/RescueException:
17
- Enabled: false
18
- Lint/ScriptPermission:
19
- Enabled: false
20
- Metrics/AbcSize:
21
- Enabled: false
22
- Metrics/CyclomaticComplexity:
23
- Enabled: false
24
- Metrics/MethodLength:
23
+ Layout/SpaceAroundMethodCallOperator:
24
+ Enabled: true
25
+ Lint/RaiseException:
26
+ Enabled: true
27
+ Lint/StructNewOverride:
28
+ Enabled: true
29
+ Metrics/BlockLength:
30
+ Exclude:
31
+ - 'spec/**/**/*'
32
+ - '*.gemspec'
33
+ Naming/FileName:
25
34
  Enabled: false
26
- Metrics/PerceivedComplexity:
35
+ RSpec/ExampleLength:
27
36
  Enabled: false
28
- Naming/FileName:
37
+ RSpec/FilePath:
38
+ Exclude:
39
+ - 'spec/grape-throttler/version_spec.rb'
40
+ RSpec/MultipleExpectations:
29
41
  Enabled: false
30
- Style/BracesAroundHashParameters:
42
+ Style/ArgumentsForwarding:
31
43
  Enabled: false
32
44
  Style/Documentation:
33
45
  Enabled: false
34
46
  Style/ExpandPathArguments:
35
47
  Enabled: false
36
- Style/RescueModifier:
37
- Enabled: false
38
- Style/RescueStandardError:
39
- Enabled: false
data/.travis.yml CHANGED
@@ -1,5 +1,25 @@
1
1
  sudo: false
2
2
  language: ruby
3
+ cache: bundler
3
4
  rvm:
4
- - 2.5.1
5
- before_install: gem install bundler
5
+ - 2.5
6
+ - 2.6
7
+ - 2.7
8
+ - ruby-head
9
+ matrix:
10
+ fast_finish: true
11
+ allow_failures:
12
+ - rvm: ruby-head
13
+ before_install:
14
+ - gem update --system
15
+ - gem install bundler
16
+ install:
17
+ - bundle install --jobs=3 --retry=3
18
+ script:
19
+ - bundle exec rspec
20
+ - bundle exec rubocop
21
+ - bundle exec fasterer
22
+ notifications:
23
+ email: false
24
+ slack:
25
+ secure: WN6Tc8kREPwVy7a+Pk1TpzKkzM8Hk++8Z8znAS51mkpvkv8g95HO9TysTZtQ+gv05L4l9V9tJsjVF2vCvJgrWQ84ilhpgBG62X+L2E/2SPB3TX0Hxh+VInCkmsg1h/FsnoZ1S9H4WjW6jbunCWl+ecytCJXNFvKtkmsbUAIVZY2jji6oKacsQZMeL+BvsKAnny7/1JJ3sSEuGZ4LXeQ1TL+mI74awmeJIuGWJfwNH6fW/z6sN4f/25ZNtiLEMZ+0rzzE7mO4k3GBv+tPPwqL1547/2KTEwHCSmPDHjpmzdA0yQ/razrA4n/Iqb5vS0ZCS6WYg6udiAh5aWwUg+g4VAzMegTqw0kGWpnTHNTuMzRGh1uKTXF5MH4T+a1FGOxnrFkZUZhCJPa2+NTbuHHxl6pycFxA+D7sGvEwaEbRgJ26y0IGk9ZnhJAhmtEddsCsdcPE4pb+10+ppbOZI1fW56UT3/f6l75Zj2QclmE2EjEi4+MqgLX8FJvT5iHhsBH7/MFNlOGftH/l2iiRFu9MmFC/BroAOF541DSP/n/Eocu7f2tiHV3Km9beHOWwYLL7nGhPhp2XlzaYA2eTQtzF3foswL440OO0PMfmmT2AbjK8uagvPPQkPFv40L40KLOnsKo+/tli8IjSqWDdwKrR69X0EmAflpu8UBzez8pCe9A=
data/CHANGELOG.md ADDED
@@ -0,0 +1,26 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [1.2.0] - 2021-07-19
10
+ ### Added
11
+ - Added Ruby 3.0 support
12
+
13
+ ## [1.1.1] - 2021-07-05
14
+ ### Changed
15
+ - Linter fixes
16
+
17
+ ## [1.1.0] - 2019-11-25
18
+ ### Added
19
+ - Added CHANGELOG.md
20
+ - Added ruby 2.7 support
21
+ ### Changed
22
+ - Refactor code to clean up lint errors
23
+
24
+ ## [1.0.1] - 2019-11-25
25
+ ### NOTE
26
+ - Previous version without CHANGELOG.md
data/CODE_OF_CONDUCT.md CHANGED
@@ -1,13 +1,74 @@
1
- # Contributor Code of Conduct
1
+ # Contributor Covenant Code of Conduct
2
2
 
3
- As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
3
+ ## Our Pledge
4
4
 
5
- We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
6
11
 
7
- Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
12
+ ## Our Standards
8
13
 
9
- Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
10
16
 
11
- Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
12
22
 
13
- This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at j.gomez@drexed.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile CHANGED
@@ -2,7 +2,5 @@
2
2
 
3
3
  source 'https://rubygems.org'
4
4
 
5
- git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
-
7
- # Specify your gem's dependencies in hacklines_extractors.gemspec
5
+ # Specify your gem's dependencies in grape-throttler.gemspec
8
6
  gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,130 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ grape-throttler (1.2.0)
5
+ grape (>= 0.16.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (6.1.4)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 1.6, < 2)
13
+ minitest (>= 5.1)
14
+ tzinfo (~> 2.0)
15
+ zeitwerk (~> 2.3)
16
+ ast (2.4.2)
17
+ builder (3.2.4)
18
+ colorize (0.8.1)
19
+ concurrent-ruby (1.1.9)
20
+ diff-lcs (1.4.4)
21
+ dry-configurable (0.12.1)
22
+ concurrent-ruby (~> 1.0)
23
+ dry-core (~> 0.5, >= 0.5.0)
24
+ dry-container (0.8.0)
25
+ concurrent-ruby (~> 1.0)
26
+ dry-configurable (~> 0.1, >= 0.1.3)
27
+ dry-core (0.7.1)
28
+ concurrent-ruby (~> 1.0)
29
+ dry-inflector (0.2.1)
30
+ dry-logic (1.2.0)
31
+ concurrent-ruby (~> 1.0)
32
+ dry-core (~> 0.5, >= 0.5)
33
+ dry-types (1.5.1)
34
+ concurrent-ruby (~> 1.0)
35
+ dry-container (~> 0.3)
36
+ dry-core (~> 0.5, >= 0.5)
37
+ dry-inflector (~> 0.1, >= 0.1.2)
38
+ dry-logic (~> 1.0, >= 1.0.2)
39
+ fakeredis (0.8.0)
40
+ redis (~> 4.1)
41
+ fasterer (0.9.0)
42
+ colorize (~> 0.7)
43
+ ruby_parser (>= 3.14.1)
44
+ grape (1.5.3)
45
+ activesupport
46
+ builder
47
+ dry-types (>= 1.1)
48
+ mustermann-grape (~> 1.0.0)
49
+ rack (>= 1.3.0)
50
+ rack-accept
51
+ i18n (1.8.10)
52
+ concurrent-ruby (~> 1.0)
53
+ minitest (5.14.4)
54
+ mustermann (1.1.1)
55
+ ruby2_keywords (~> 0.0.1)
56
+ mustermann-grape (1.0.1)
57
+ mustermann (>= 1.0.0)
58
+ parallel (1.20.1)
59
+ parser (3.0.2.0)
60
+ ast (~> 2.4.1)
61
+ rack (2.2.3)
62
+ rack-accept (0.4.5)
63
+ rack (>= 0.4)
64
+ rack-test (1.1.0)
65
+ rack (>= 1.0, < 3)
66
+ rainbow (3.0.0)
67
+ rake (13.0.6)
68
+ redis (4.3.1)
69
+ regexp_parser (2.1.1)
70
+ rexml (3.2.5)
71
+ rspec (3.10.0)
72
+ rspec-core (~> 3.10.0)
73
+ rspec-expectations (~> 3.10.0)
74
+ rspec-mocks (~> 3.10.0)
75
+ rspec-core (3.10.1)
76
+ rspec-support (~> 3.10.0)
77
+ rspec-expectations (3.10.1)
78
+ diff-lcs (>= 1.2.0, < 2.0)
79
+ rspec-support (~> 3.10.0)
80
+ rspec-mocks (3.10.2)
81
+ diff-lcs (>= 1.2.0, < 2.0)
82
+ rspec-support (~> 3.10.0)
83
+ rspec-support (3.10.2)
84
+ rubocop (1.18.3)
85
+ parallel (~> 1.10)
86
+ parser (>= 3.0.0.0)
87
+ rainbow (>= 2.2.2, < 4.0)
88
+ regexp_parser (>= 1.8, < 3.0)
89
+ rexml
90
+ rubocop-ast (>= 1.7.0, < 2.0)
91
+ ruby-progressbar (~> 1.7)
92
+ unicode-display_width (>= 1.4.0, < 3.0)
93
+ rubocop-ast (1.8.0)
94
+ parser (>= 3.0.1.1)
95
+ rubocop-performance (1.11.4)
96
+ rubocop (>= 1.7.0, < 2.0)
97
+ rubocop-ast (>= 0.4.0)
98
+ rubocop-rake (0.6.0)
99
+ rubocop (~> 1.0)
100
+ rubocop-rspec (2.4.0)
101
+ rubocop (~> 1.0)
102
+ rubocop-ast (>= 1.1.0)
103
+ ruby-progressbar (1.11.0)
104
+ ruby2_keywords (0.0.5)
105
+ ruby_parser (3.16.0)
106
+ sexp_processor (~> 4.15, >= 4.15.1)
107
+ sexp_processor (4.15.3)
108
+ tzinfo (2.0.4)
109
+ concurrent-ruby (~> 1.0)
110
+ unicode-display_width (2.0.0)
111
+ zeitwerk (2.4.2)
112
+
113
+ PLATFORMS
114
+ ruby
115
+
116
+ DEPENDENCIES
117
+ bundler
118
+ fakeredis
119
+ fasterer
120
+ grape-throttler!
121
+ rack-test
122
+ rake
123
+ rspec
124
+ rubocop
125
+ rubocop-performance
126
+ rubocop-rake
127
+ rubocop-rspec
128
+
129
+ BUNDLED WITH
130
+ 2.2.24
data/README.md CHANGED
@@ -1,40 +1,38 @@
1
- Grape Throttle
2
- ==============
1
+ # GrapeThrottler
3
2
 
4
- [![Gem Version](https://badge.fury.io/rb/grape-throttle.svg)](https://badge.fury.io/rb/grape-throttle)
5
- [![Build Status](https://travis-ci.org/xevix/grape-throttle.svg)](https://travis-ci.org/xevix/grape-throttle)
3
+ [![Gem Version](https://badge.fury.io/rb/grape-throttler.svg)](http://badge.fury.io/rb/grape-throttler)
4
+ [![Build Status](https://travis-ci.org/drexed/grape-throttler.svg?branch=master)](https://travis-ci.org/drexed/grape-throttler)
6
5
 
7
- **Deprecation Warning**
6
+ GrapeThrottler provides a simple endpoint-specific throttling mechanism for Grape.
8
7
 
9
- This gem is no longer being actively maintained. For similar throttle functionality see [rack-attack](https://github.com/kickstarter/rack-attack). Integration of this gem with rack-attack is a future consideration.
8
+ ## Installation
10
9
 
11
- **Description**
10
+ Add this line to your application's Gemfile:
12
11
 
13
- The grape-throttle gem provides a simple endpoint-specific throttling mechanism for Grape.
14
-
15
- ## Requirements
12
+ ```ruby
13
+ gem 'grape-throttler'
14
+ ```
16
15
 
17
- * Grape >= 0.10.0
18
- * Redis
16
+ And then execute:
19
17
 
20
- ## Usage
18
+ $ bundle
21
19
 
22
- ### Build and Install
20
+ Or install it yourself as:
23
21
 
24
- To use, just install the gem from RubyGems or via Bundler by requiring it in your Gemfile.
22
+ $ gem install grape-throttler
25
23
 
26
- ```
27
- gem 'grape-throttle'
28
- ```
24
+ ## Table of Contents
29
25
 
30
- ### Middleware Setup
26
+ * [Configuration](#configuration)
27
+ * [Endpoint](#endpoint)
31
28
 
32
- Then in your Grape API, install the middleware which will do the throttling. At a minimum, it requires a Redis instance for caching as the `cache` parameter.
29
+ ## Configuration
30
+ In your Grape API, install the middleware which will do the throttling. At a minimum, it requires a Redis instance for caching as the `cache` parameter.
33
31
 
34
32
  **Simple Case**
35
33
 
36
34
  ```ruby
37
- use Grape::Middleware::ThrottleMiddleware, cache: Redis.new
35
+ use GrapeThrottler::Middleware::ThrottleMiddleware, cache: Redis.new
38
36
  ```
39
37
 
40
38
  In this simple case, you just set up the middleware, and pass it a Redis instance.
@@ -42,7 +40,7 @@ In this simple case, you just set up the middleware, and pass it a Redis instanc
42
40
  **Advanced Case**
43
41
 
44
42
  ```ruby
45
- use Grape::Middleware::ThrottleMiddleware, cache: $redis, user_key: ->(env) do
43
+ use GrapeThrottler::Middleware::ThrottleMiddleware, cache: $redis, user_key: ->(env) do
46
44
  # Use the current_user's id as an identifier
47
45
  user = current_user
48
46
  user.nil? ? nil : user.id
@@ -55,13 +53,13 @@ The `user_key` parameter is a function that can be used to determine a custom id
55
53
 
56
54
  **Logging**
57
55
 
58
- The gem will log errors to STDOUT by default. If you prefer a different logger you can use the `logger` option to pass in your own logger.
56
+ The gem will log errors to $stdout by default. If you prefer a different logger you can use the `logger` option to pass in your own logger.
59
57
 
60
58
  ```ruby
61
- use Grape::Middleware::ThrottleMiddleware, cache: Redis.new, logger: Logger.new('my_custom_log.log')
59
+ use GrapeThrottler::Middleware::ThrottleMiddleware, cache: Redis.new, logger: Logger.new('my_custom_log.log')
62
60
  ```
63
61
 
64
- ### Endpoint Usage
62
+ ## Endpoint
65
63
 
66
64
  This gem adds a `throttle` DSL-like method that can be used to throttle different endpoints differently.
67
65
 
@@ -72,56 +70,65 @@ Supported predefined periods are: `:hourly`, `:daily`, `:monthly`.
72
70
  Example:
73
71
 
74
72
  ```ruby
75
- class API < Grape::API
73
+ class API < GrapeThrottler::API
76
74
  resources :users do
77
75
 
78
76
  # Allow start of competition only every 10 minutes
79
- desc "Start competition"
77
+ desc 'Start competition'
80
78
  throttle period: 10.minutes, limit: 1
81
79
  params do
82
- requires :id, type: Integer, desc: "id"
80
+ requires :id, type: Integer, desc: 'id'
83
81
  end
84
- post "/:id/competition" do
82
+ post '/:id/competition' do
85
83
  User.find(params[:id]).start_competition
86
84
  end
87
85
 
88
86
  # 3 times a day max
89
- desc "Fetch a user"
87
+ desc 'Fetch a user'
90
88
  throttle daily: 3
91
89
  params do
92
- requires :id, type: Integer, desc: "id"
90
+ requires :id, type: Integer, desc: 'id'
93
91
  end
94
- get "/:id" do
92
+ get '/:id' do
95
93
  User.find(params[:id])
96
94
  end
97
95
 
98
96
  # Once a month or the user will go crazy
99
- desc "Poke a user"
97
+ desc 'Poke a user'
100
98
  throttle monthly: 1
101
99
  params do
102
- requires :id, type: Integer, desc: "id"
100
+ requires :id, type: Integer, desc: 'id'
103
101
  end
104
- post "/:id/poke" do
102
+ post '/:id/poke' do
105
103
  User.find(params[:id]).poke
106
104
  end
107
105
 
108
106
  # No limit to the amount we can annoy users
109
- desc "Annoy a user"
107
+ desc 'Annoy a user'
110
108
  params do
111
- requires :id, type: Integer, desc: "id"
109
+ requires :id, type: Integer, desc: 'id'
112
110
  end
113
- post "/:id/annoy" do
111
+ post '/:id/annoy' do
114
112
  User.find(params[:id]).annoy
115
113
  end
116
114
  end
117
115
  end
118
116
  ```
119
117
 
120
- ## TODO
118
+ ## Development
119
+
120
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
121
+
122
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
123
+
124
+ ## Contributing
125
+
126
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/grape-throttler. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
127
+
128
+ ## License
121
129
 
122
- * Custom error handling and error strings, status etc.
123
- * Allow use of something other than Redis for caching
130
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
124
131
 
125
- ## Thanks
132
+ ## Code of Conduct
126
133
 
127
- Thanks to the awesome Grape community, and to @dblock for all the help getting this thing going.
134
+ Everyone interacting in the Lite::Ruby project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/grape-throttler/blob/master/CODE_OF_CONDUCT.md).
data/_config.yml ADDED
@@ -0,0 +1 @@
1
+ theme: jekyll-theme-leap-day
data/bin/console CHANGED
@@ -8,8 +8,8 @@ require 'grape-throttler'
8
8
  # with your gem easier. You can also use a different console, if you like.
9
9
 
10
10
  # (If you use this, don't forget to add pry to your Gemfile!)
11
- # require 'pry'
11
+ # require "pry"
12
12
  # Pry.start
13
13
 
14
14
  require 'irb'
15
- IRB.start
15
+ IRB.start(__FILE__)
data/bin/setup CHANGED
@@ -1,6 +1,7 @@
1
- #!/bin/bash
1
+ #!/usr/bin/env bash
2
2
  set -euo pipefail
3
3
  IFS=$'\n\t'
4
+ set -vx
4
5
 
5
6
  bundle install
6
7
 
@@ -10,25 +10,43 @@ Gem::Specification.new do |spec|
10
10
  spec.authors = ['Juan Gomez']
11
11
  spec.email = ['j.gomez@drexed.com']
12
12
 
13
- spec.summary = 'Gem for throttling grape requests.'
14
- spec.description = 'A middleware for Grape to add endpoint-specific throttling.'
13
+ spec.summary = 'Middleware for Grape to add endpoint-specific throttling.'
15
14
  spec.homepage = 'http://drexed.github.io/grape-throttler'
16
15
  spec.license = 'MIT'
17
16
 
18
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata.merge(
21
+ 'allowed_push_host' => 'https://rubygems.org',
22
+ 'changelog_uri' => 'https://github.com/drexed/grape-throttler/blob/master/CHANGELOG.md',
23
+ 'homepage_uri' => spec.homepage,
24
+ 'source_code_uri' => 'https://github.com/drexed/grape-throttler'
25
+ )
26
+ else
27
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
28
+ 'public gem pushes.'
29
+ end
30
+
31
+ # Specify which files should be added to the gem when it is released.
32
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
33
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
34
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
35
+ end
19
36
  spec.bindir = 'exe'
20
37
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
38
  spec.require_paths = %w[lib]
22
39
 
23
40
  spec.add_runtime_dependency 'grape', '>= 0.16.0'
24
- spec.add_runtime_dependency 'rails'
25
41
 
26
42
  spec.add_development_dependency 'bundler'
27
43
  spec.add_development_dependency 'fakeredis'
28
44
  spec.add_development_dependency 'fasterer'
29
45
  spec.add_development_dependency 'rack-test'
30
46
  spec.add_development_dependency 'rake'
31
- spec.add_development_dependency 'reek'
32
47
  spec.add_development_dependency 'rspec'
33
48
  spec.add_development_dependency 'rubocop'
49
+ spec.add_development_dependency 'rubocop-performance'
50
+ spec.add_development_dependency 'rubocop-rake'
51
+ spec.add_development_dependency 'rubocop-rspec'
34
52
  end
@@ -4,11 +4,13 @@ module GrapeThrottler
4
4
  module Middleware
5
5
  class ThrottleMiddleware < Grape::Middleware::Base
6
6
 
7
- COUNTER_START ||= 0
7
+ COUNTER_START = 0
8
8
 
9
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
10
+ # rubocop:disable Metrics/PerceivedComplexity, Lint/AssignmentInCondition
9
11
  def before
10
12
  endpoint = env['api.endpoint']
11
- logger = options[:logger] || Logger.new(STDOUT)
13
+ logger = options[:logger] || Logger.new($stdout)
12
14
 
13
15
  return unless throttle_options = endpoint.route_setting(:throttle)
14
16
 
@@ -29,17 +31,19 @@ module GrapeThrottler
29
31
  current = redis.get(rate_key).to_i
30
32
 
31
33
  if !current.nil? && current >= limit
32
- endpoint.error!('too many requests, please try again later', 429)
34
+ endpoint.error!('Too Many Requests', 429)
33
35
  else
34
36
  redis.multi do
35
- redis.set(rate_key, COUNTER_START, { nx: true, ex: period.to_i })
37
+ redis.set(rate_key, COUNTER_START, nx: true, ex: period.to_i)
36
38
  redis.incr(rate_key)
37
39
  end
38
40
  end
39
- rescue Exception => e
41
+ rescue StandardError => e
40
42
  logger.warn(e.message)
41
43
  end
42
44
  end
45
+ # rubocop:enable Metrics/PerceivedComplexity, Lint/AssignmentInCondition
46
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
43
47
 
44
48
  private
45
49
 
@@ -49,19 +53,21 @@ module GrapeThrottler
49
53
  raise ArgumentError, 'Please set a period and limit (see documentation)'
50
54
  end
51
55
 
56
+ # rubocop:disable Lint/AssignmentInCondition
52
57
  def find_limit_and_period(throttle_options)
53
58
  if limit = throttle_options[:hourly]
54
- period = 1.hour
59
+ period = 3_600
55
60
  elsif limit = throttle_options[:daily]
56
- period = 1.day
61
+ period = 86_400
57
62
  elsif limit = throttle_options[:monthly]
58
- period = 1.month
63
+ period = 2_629_746
59
64
  elsif period = throttle_options[:period]
60
65
  limit = throttle_options[:limit]
61
66
  end
62
67
 
63
68
  [limit, period]
64
69
  end
70
+ # rubocop:enable Lint/AssignmentInCondition
65
71
 
66
72
  def find_user_value(options)
67
73
  user_key = options[:user_key]
@@ -71,8 +77,8 @@ module GrapeThrottler
71
77
  end
72
78
 
73
79
  def generate_rate_key(endpoint, user_value)
74
- epoint = endpoint.routes.first
75
- "#{epoint.request_method}:#{epoint.path}:#{user_value}"
80
+ endpoint_route = endpoint.routes.first
81
+ "#{endpoint_route.request_method}:#{endpoint_route.path}:#{user_value}"
76
82
  end
77
83
 
78
84
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GrapeThrottler
4
- VERSION ||= '1.0.0'
4
+
5
+ VERSION = '1.2.0'
6
+
5
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-throttler
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Gomez
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-09-18 00:00:00.000000000 Z
11
+ date: 2021-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape
@@ -25,13 +25,13 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.16.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: rails
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
- type: :runtime
34
+ type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: bundler
42
+ name: fakeredis
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: fakeredis
56
+ name: fasterer
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: fasterer
70
+ name: rack-test
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -81,7 +81,7 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: rack-test
84
+ name: rake
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -95,7 +95,7 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: rake
98
+ name: rspec
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="
@@ -109,7 +109,7 @@ dependencies:
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
- name: reek
112
+ name: rubocop
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - ">="
@@ -123,7 +123,7 @@ dependencies:
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  - !ruby/object:Gem::Dependency
126
- name: rspec
126
+ name: rubocop-performance
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - ">="
@@ -137,7 +137,21 @@ dependencies:
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
139
  - !ruby/object:Gem::Dependency
140
- name: rubocop
140
+ name: rubocop-rake
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop-rspec
141
155
  requirement: !ruby/object:Gem::Requirement
142
156
  requirements:
143
157
  - - ">="
@@ -150,7 +164,7 @@ dependencies:
150
164
  - - ">="
151
165
  - !ruby/object:Gem::Version
152
166
  version: '0'
153
- description: A middleware for Grape to add endpoint-specific throttling.
167
+ description:
154
168
  email:
155
169
  - j.gomez@drexed.com
156
170
  executables: []
@@ -158,20 +172,18 @@ extensions: []
158
172
  extra_rdoc_files: []
159
173
  files:
160
174
  - ".fasterer.yml"
161
- - ".gitattributes"
162
175
  - ".gitignore"
163
- - ".irbrc"
164
- - ".reek.yml"
165
176
  - ".rspec"
166
177
  - ".rubocop.yml"
167
- - ".ruby-version"
168
178
  - ".travis.yml"
179
+ - CHANGELOG.md
169
180
  - CODE_OF_CONDUCT.md
170
181
  - Gemfile
182
+ - Gemfile.lock
171
183
  - README.md
172
184
  - Rakefile
185
+ - _config.yml
173
186
  - bin/console
174
- - bin/rake
175
187
  - bin/setup
176
188
  - grape-throttler.gemspec
177
189
  - lib/grape-throttler.rb
@@ -182,7 +194,7 @@ homepage: http://drexed.github.io/grape-throttler
182
194
  licenses:
183
195
  - MIT
184
196
  metadata: {}
185
- post_install_message:
197
+ post_install_message:
186
198
  rdoc_options: []
187
199
  require_paths:
188
200
  - lib
@@ -197,9 +209,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
197
209
  - !ruby/object:Gem::Version
198
210
  version: '0'
199
211
  requirements: []
200
- rubyforge_project:
201
- rubygems_version: 2.7.7
202
- signing_key:
212
+ rubygems_version: 3.2.24
213
+ signing_key:
203
214
  specification_version: 4
204
- summary: Gem for throttling grape requests.
215
+ summary: Middleware for Grape to add endpoint-specific throttling.
205
216
  test_files: []
data/.gitattributes DELETED
@@ -1,2 +0,0 @@
1
- # Auto detect text files and perform LF normalization
2
- * text=auto
data/.irbrc DELETED
@@ -1,44 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Awesome print
4
- begin
5
- require 'awesome_print'
6
-
7
- AwesomePrint.irb!
8
- rescue LoadError => err
9
- warn "Couldn't load awesome_print: #{err}"
10
- end
11
-
12
- # IRB
13
- require 'irb/completion'
14
-
15
- ARGV.concat %w[--readline --prompt-mode simple]
16
-
17
- IRB.conf[:PROMPT_MODE] = :SIMPLE
18
- IRB.conf[:EVAL_HISTORY] = 1000
19
- IRB.conf[:SAVE_HISTORY] = 1000
20
- IRB.conf[:HISTORY_FILE] = File.expand_path('.irbrc_history')
21
-
22
- # Rails
23
- railsrc_path = File.expand_path('.irbrc_rails')
24
-
25
- if (ENV['RAILS_ENV'] || defined?(Rails)) && File.exist?(railsrc_path)
26
- begin
27
- load railsrc_path
28
- rescue Exception => err
29
- warn "Could not load: #{railsrc_path} because of #{err}"
30
- end
31
- end
32
-
33
- # Object
34
- class Object
35
-
36
- def interesting_methods
37
- case self.class
38
- when Class then public_methods.sort - Object.public_methods
39
- when Module then public_methods.sort - Module.public_methods
40
- else public_methods.sort - Object.new.public_methods
41
- end
42
- end
43
-
44
- end
data/.reek.yml DELETED
@@ -1,22 +0,0 @@
1
- ---
2
- detectors:
3
- Attribute:
4
- enabled: false
5
- IrresponsibleModule:
6
- enabled: false
7
- MissingSafeMethod:
8
- enabled: false
9
- NilCheck:
10
- enabled: false
11
- TooManyInstanceVariables:
12
- enabled: false
13
- TooManyStatements:
14
- max_statements: 10
15
- exclude:
16
- - 'GrapeThrottler::Middleware::ThrottleMiddleware#before'
17
- UncommunicativeVariableName:
18
- accept:
19
- - '/^.$/'
20
- - '/[0-9]$/'
21
- UtilityFunction:
22
- enabled: false
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 2.5.1
data/bin/rake DELETED
@@ -1,15 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- # This file was generated by Bundler.
5
- #
6
- # The application 'rake' is installed as part of a gem, and
7
- # this file is here to facilitate running it.
8
-
9
- require 'pathname'
10
- ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', Pathname.new(__FILE__).realpath)
11
-
12
- require 'rubygems'
13
- require 'bundler/setup'
14
-
15
- load Gem.bin_path('rake', 'rake')