raterr 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -4
- data/lib/raterr/version.rb +1 -1
- data/lib/raterr.rb +7 -0
- data/raterr.gemspec +1 -0
- metadata +15 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 17ba66bcb168f7edde054062b22e260a53655635
|
4
|
+
data.tar.gz: e74e650451a183e4ff1e051807e5c69e29c1af9a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f16f4bb8cafbc3f99fc2c5356842276244e53c3780561d1df0a2e38e27565bb11671bb34aa7711c30125bfedb96717f8860519e5be893b3645f36332e61caea0
|
7
|
+
data.tar.gz: 8c7cc518d9bc683211cce1aae4bc7865b854da9b4b81360b7f561fb98f28f20d0074448214c5c32bea7078323e5e07f21bf056fbcccbda99fb39c5be19f47759
|
data/README.md
CHANGED
@@ -2,7 +2,8 @@
|
|
2
2
|
[![Build Status](https://travis-ci.org/wizardone/raterr.svg?branch=master)](https://travis-ci.org/wizardone/raterr)
|
3
3
|
[![codecov](https://codecov.io/gh/wizardone/raterr/branch/master/graph/badge.svg)](https://codecov.io/gh/wizardone/raterr)
|
4
4
|
|
5
|
-
`Raterr` allows you to enforce rate limiting restrictions on
|
5
|
+
`Raterr` allows you to enforce rate limiting restrictions based on ip
|
6
|
+
address.
|
6
7
|
|
7
8
|
## Installation
|
8
9
|
|
@@ -35,9 +36,13 @@ To enforce rate limiting use:
|
|
35
36
|
```ruby
|
36
37
|
Raterr.enforce(request, period: :minute, max: 200, code: 429)
|
37
38
|
```
|
39
|
+
`request` might be any data type that has an `ip` method. For Rails
|
40
|
+
applications that would be the request method.
|
41
|
+
|
38
42
|
The result of `Raterr.enforce` is always a pseudo status. In case the
|
39
43
|
rate limit has not been reached you will get a pseudo `200` status + the
|
40
|
-
number of attempts. This allows you to do additional checks
|
44
|
+
number of attempts made. This allows you to do additional checks (say a
|
45
|
+
warning if you are about to reach the threshhold). If
|
41
46
|
it has been reached you will get whatever status you configured, or the
|
42
47
|
default one, which is 429 + a text message.
|
43
48
|
|
@@ -68,9 +73,9 @@ application controller and so on...
|
|
68
73
|
|
69
74
|
You can configure the period error code and the max attempts. The allowed periods
|
70
75
|
are: `:minute, :hour, :day`.
|
76
|
+
The default error code is `429`.
|
77
|
+
The default max attempts is `100`.
|
71
78
|
|
72
|
-
Currently `Raterr` checks the unique ip address of the visitor to
|
73
|
-
determine the amount of visits.
|
74
79
|
## Development
|
75
80
|
|
76
81
|
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.
|
data/lib/raterr/version.rb
CHANGED
data/lib/raterr.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'active_support'
|
1
2
|
require 'raterr/version'
|
2
3
|
require 'raterr/period_builder'
|
3
4
|
require 'raterr/mixin'
|
@@ -7,6 +8,8 @@ require 'raterr/minute'
|
|
7
8
|
|
8
9
|
module Raterr
|
9
10
|
|
11
|
+
InvalidStore = Class.new(StandardError)
|
12
|
+
|
10
13
|
AVAILABLE_PERIODS = [:minute, :hour, :day, :week, :month].freeze
|
11
14
|
DEFAULTS = {
|
12
15
|
max: 100,
|
@@ -20,6 +23,10 @@ module Raterr
|
|
20
23
|
attr_accessor :store
|
21
24
|
|
22
25
|
def enforce(request, **options)
|
26
|
+
unless store.is_a?(Hash) || store.is_a?(::ActiveSupport::Cache::MemoryStore)
|
27
|
+
raise InvalidStore.new('Store is not valid, please refer to the documentation')
|
28
|
+
end
|
29
|
+
|
23
30
|
period = PeriodBuilder.call(request, options)
|
24
31
|
period.allowed? ? period.proceed : period.rate_limit_exceeded
|
25
32
|
end
|
data/raterr.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: raterr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefan Slaveykov
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.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: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
55
69
|
description: A gem allowing you to set rate limiting on your application
|
56
70
|
email:
|
57
71
|
- wizard.oneandonly@gmail.com
|