rails-action_throttling 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fe356734bdea7706d3062cfbb3db7da1e144ca36cfcacb02a3da7b588d61c78c
4
+ data.tar.gz: 548932e79c8ea177dddfd10b98c2d016281412b8574e9c2b0c0efc783bfa0bb2
5
+ SHA512:
6
+ metadata.gz: 8e7fff81eb30bedb1445bda6065bc00a11d54eab4c08ce7e48f9ea3eb52de654e68910a98d9e1e7f6e5e8cfa91b056646bc46ac53cc79df1db480a640d12a5d1
7
+ data.tar.gz: 13d5f6265c73bdd15d8d8f18ee3a472d80f45e2eb2a8bc4f58bff6efb87ed2fe1895c455d757e4ac6b0626636d27799cab47f6d111f59e35aaf9a4e5c986994f
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in rails-action_throttling.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rails-action_throttling (0.1.0)
5
+ activesupport (~> 5.2)
6
+ http-errors (~> 0.1)
7
+ redis (~> 4.0)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (5.2.0)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 0.7, < 2)
15
+ minitest (~> 5.1)
16
+ tzinfo (~> 1.1)
17
+ concurrent-ruby (1.0.5)
18
+ diff-lcs (1.3)
19
+ http-errors (0.1.3)
20
+ activesupport (~> 5.1)
21
+ i18n (1.0.0)
22
+ concurrent-ruby (~> 1.0)
23
+ minitest (5.11.3)
24
+ rake (10.5.0)
25
+ redis (4.0.1)
26
+ rspec (3.7.0)
27
+ rspec-core (~> 3.7.0)
28
+ rspec-expectations (~> 3.7.0)
29
+ rspec-mocks (~> 3.7.0)
30
+ rspec-core (3.7.1)
31
+ rspec-support (~> 3.7.0)
32
+ rspec-expectations (3.7.0)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.7.0)
35
+ rspec-mocks (3.7.0)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.7.0)
38
+ rspec-support (3.7.1)
39
+ thread_safe (0.3.6)
40
+ timecop (0.9.1)
41
+ tzinfo (1.2.5)
42
+ thread_safe (~> 0.1)
43
+
44
+ PLATFORMS
45
+ ruby
46
+
47
+ DEPENDENCIES
48
+ bundler (~> 1.16)
49
+ rails-action_throttling!
50
+ rake (~> 10.0)
51
+ rspec (~> 3.0)
52
+ timecop (~> 0.9)
53
+
54
+ BUNDLED WITH
55
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Emil Kampp
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # Action throttling
2
+
3
+ ## Usage
4
+
5
+ ```
6
+ gem 'rails-action_throtling'
7
+ ```
8
+
9
+ Configure your bucket. There are no default values for this, since we can't know your application specific implementation, so this step is mandatory.
10
+
11
+ ```ruby
12
+ ActionThrottling.configure do |config|
13
+ # The bucket_key is evaluated inside your application context
14
+ config.bucket_key = Proc.new { current_user.bucket }
15
+
16
+ # Set the interval in which the bucket is regenerated
17
+ config.regenerate_interval = 10.minutes
18
+
19
+ # Sets the number of tokens to be pub back into the bucket
20
+ config.regenerate_amount = 100
21
+
22
+ # (optional) If you're not running on a completely vanilla redis connection,
23
+ # you can supply your own redis instance here.
24
+ config.redis = Redis.new 'http://redis-server.com'
25
+ end
26
+ ```
27
+
28
+ Then call the `cost` method on your actions that you want to protect:
29
+
30
+ ```ruby
31
+ def show
32
+ # Every call will cost one credit
33
+ # Based on the configuration above, this will allow the user to call this
34
+ # endpoint 100 times every minute.
35
+ #
36
+ # This will call the `deduct(1)` method on the configured `config.bucket` (see
37
+ # above)
38
+ cost 1
39
+ # ...
40
+ end
41
+ ```
42
+
43
+ If you have something like a create method, or some complex method that's costly on the server side, you can set the cost appropriately:
44
+
45
+ ```ruby
46
+ def complex_method
47
+ # This will call `deduct(25)` on the `config.bucket` (see above)
48
+ cost 25
49
+ # ...
50
+ end
51
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rails/action_throttling"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,21 @@
1
+ module ActionThrottling
2
+ class Configuration
3
+ attr_accessor :bucket_key, :regenerate_amount, :regenerate_interval, :redis
4
+
5
+ def initialize
6
+ self.redis = Redis.new
7
+ end
8
+ end
9
+
10
+ class << self
11
+ attr_writer :configuration
12
+ end
13
+
14
+ def self.configuration
15
+ @configuration ||= Configuration.new
16
+ end
17
+
18
+ def self.configure
19
+ yield configuration
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module ActionThrottling
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,76 @@
1
+ require "redis"
2
+ require "active_support/all"
3
+ require "http_error"
4
+ require "action_throttling/version"
5
+ require "action_throttling/configuration"
6
+
7
+ module ActionThrottling
8
+ MissingConfiguration = Class.new StandardError
9
+
10
+ module InstanceMethods
11
+ def cost(price)
12
+ # If last time the bucket was called is older than the regeneration limit
13
+ # regenerate the bucket.
14
+ regenerate if last_called < regeneration_time
15
+
16
+ # Deduct the price from the bucket. If it's below zero we raiss a
17
+ # HttpError::ToManyRequests exception/
18
+ if redis.decrby(bucket_key, price) < 0
19
+ raise HttpError::ToManyRequests, "Throttling enabled. Please try again later"
20
+ end
21
+
22
+ # Store when the cost was last called so we can calculate regeneration
23
+ redis.set last_call_key, Time.now.httpdate
24
+ end
25
+
26
+ private
27
+
28
+ def last_call_key
29
+ "#{bucket_key}-last-call"
30
+ end
31
+
32
+ def regenerate
33
+ redis.set bucket_key, ActionThrottling.configuration.regenerate_amount
34
+ end
35
+
36
+ def last_called
37
+ value = redis.get(last_call_key).presence
38
+
39
+ # trigger regeneration when first cost is first called
40
+ value ||= (regeneration_time - 1.second).httpdate
41
+
42
+ Time.parse value
43
+ end
44
+
45
+ def bucket_key
46
+ instance_eval &ActionThrottling.configuration.bucket_key
47
+ end
48
+
49
+ def regeneration_time
50
+ ActionThrottling.configuration.regenerate_interval.ago
51
+ end
52
+
53
+ def redis
54
+ @redis ||= Redis.new
55
+ end
56
+ end
57
+
58
+ def self.included(receiver)
59
+ unless ActionThrottling.configuration.bucket_key
60
+ raise ActionThrottling::MissingConfiguration,
61
+ 'Missing bucket_key configuration. See documentation'
62
+ end
63
+
64
+ unless ActionThrottling.configuration.regenerate_interval
65
+ raise ActionThrottling::MissingConfiguration,
66
+ 'Missing regenerate_interval configuration. See documentation'
67
+ end
68
+
69
+ unless ActionThrottling.configuration.regenerate_amount
70
+ raise ActionThrottling::MissingConfiguration,
71
+ 'Missing regenerate_amount configuration. See documentation'
72
+ end
73
+
74
+ receiver.send :include, InstanceMethods
75
+ end
76
+ end
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "action_throttling/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails-action_throttling"
8
+ spec.version = ActionThrottling::VERSION
9
+ spec.authors = ["Emil Kampp"]
10
+ spec.email = ["emil@kampp.me"]
11
+
12
+ spec.summary = "Rails per-action request throttling"
13
+ spec.description = "Allows request throttling on a per-action basis"
14
+ spec.homepage = "https://github.com/YouWeApS/arctic-gems-rails-action_throttling"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.16"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "timecop", "~> 0.9"
28
+ spec.add_runtime_dependency "http-errors", "~> 0.1"
29
+ spec.add_runtime_dependency "activesupport", "~> 5.2"
30
+ spec.add_runtime_dependency "redis", "~> 4.0"
31
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-action_throttling
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Emil Kampp
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: timecop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: http-errors
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.1'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activesupport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.2'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: redis
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '4.0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '4.0'
111
+ description: Allows request throttling on a per-action basis
112
+ email:
113
+ - emil@kampp.me
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - Gemfile.lock
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - lib/action_throttling.rb
129
+ - lib/action_throttling/configuration.rb
130
+ - lib/action_throttling/version.rb
131
+ - rails-action_throttling.gemspec
132
+ homepage: https://github.com/YouWeApS/arctic-gems-rails-action_throttling
133
+ licenses:
134
+ - MIT
135
+ metadata: {}
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 2.7.3
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: Rails per-action request throttling
156
+ test_files: []