grape-throttler 1.0.0 → 1.0.1

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: c02d5c5ef081337c20539acf09bafd5c9ae35fedeeb16a92c38b16e5839fed04
4
+ data.tar.gz: b49635981a48db4d77ffb3ce56e539b1b33aa42727f931e20e5324dea0320645
5
5
  SHA512:
6
- metadata.gz: 3ee5867e71b9de391fea4a5e37385b522abe52c724aa261f4f48510b30b076c417bd76eab1b1de27c1ee742096b3e493c7dcc92453242708574780873a8a216e
7
- data.tar.gz: 47544b35bd03f981dd5c7dffa4f5c8cc8a9e155968507ded18c60e9971b9626f45e4f965382fc55b6af9e7c35be0d1c58e29eea0285df2f27a3c5bf932a6a054
6
+ metadata.gz: bbcca0e55d60925b0afb472459c88580e8dfe214c27059fa26e92a825b5f72ac3d6e5225d00ed8e38e3ca0f439f99d2334a0d2631907763453ee783d1c6f4fa3
7
+ data.tar.gz: 40644a93838ebe1e019bbfee16cf11606b4bbc375f569c554d5ed52ee99689c2e9da100ec69ad85799caf95180151c75d76ae96c9e0d1b25aef25065102eab35
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
@@ -58,10 +56,10 @@ The `user_key` parameter is a function that can be used to determine a custom id
58
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,57 @@ 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
121
-
122
- * Custom error handling and error strings, status etc.
123
- * Allow use of something other than Redis for caching
118
+ ## Contributing
124
119
 
125
- ## Thanks
120
+ Your contribution is welcome.
126
121
 
127
- Thanks to the awesome Grape community, and to @dblock for all the help getting this thing going.
122
+ 1. Fork it
123
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
124
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
125
+ 4. Push to the branch (`git push origin my-new-feature`)
126
+ 5. Create new Pull Request
@@ -29,7 +29,7 @@ module GrapeThrottler
29
29
  current = redis.get(rate_key).to_i
30
30
 
31
31
  if !current.nil? && current >= limit
32
- endpoint.error!('too many requests, please try again later', 429)
32
+ endpoint.error!('Too Many Requests', 429)
33
33
  else
34
34
  redis.multi do
35
35
  redis.set(rate_key, COUNTER_START, { nx: true, ex: period.to_i })
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GrapeThrottler
4
- VERSION ||= '1.0.0'
4
+ VERSION ||= '1.0.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
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.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Gomez