rails_throttle 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0ac0a027c97f8d22db38bedc1700b9f0834475000d44dd23819296f32bfc8a1e
4
+ data.tar.gz: a6280ac0701ce08f4bb085608b9e834f509aedc414e9079d076c297fc99fb865
5
+ SHA512:
6
+ metadata.gz: b2c92b80a09e7176a2e8d0179987484ce45f22eeb64e7b49d3943d15cfa735aed7836e1b4487574ce59423d5fead2ade6eb8711a169c4a0ac5cdd79a2ef00526
7
+ data.tar.gz: cdc44864b318e5c91655e4befc1b3bcecc8ae4d1ee9cd6cef638bea22099e22191bbd367ace6314b2993596d91176b39cda57ea7d15a5a3c8b7bbfae98e2c52f
@@ -0,0 +1,42 @@
1
+ version: 2
2
+ jobs:
3
+ build:
4
+ docker:
5
+ - image: circleci/ruby:2.6.1-node-browsers
6
+
7
+ working_directory: ~/repo
8
+
9
+ steps:
10
+ - checkout
11
+
12
+ - restore_cache:
13
+ keys:
14
+ - v1-dependencies-{{ checksum "Gemfile.lock" }}
15
+ - v1-dependencies-
16
+
17
+ - run:
18
+ name: Install dependencies
19
+ command: |
20
+ bundle install --jobs=4 --retry=3 --path vendor/bundle
21
+
22
+ - save_cache:
23
+ paths:
24
+ - ./vendor/bundle
25
+ key: v1-dependencies-{{ checksum "Gemfile.lock" }}
26
+
27
+ - run:
28
+ name: Run tests
29
+ command: |
30
+ bundle exec rake test
31
+
32
+ - run:
33
+ name: Rubocop
34
+ command:
35
+ bundle install rubocop
36
+ bundle exec rubocop
37
+
38
+ - store_test_results:
39
+ path: /tmp/test-results
40
+ - store_artifacts:
41
+ path: /tmp/test-results
42
+ destination: test-results
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.rubocop.yml ADDED
@@ -0,0 +1,47 @@
1
+ Metrics/LineLength:
2
+ Max: 120
3
+
4
+ Style/Documentation:
5
+ Enabled: false
6
+
7
+ Style/MixinUsage:
8
+ Enabled: false
9
+
10
+ Metrics/AbcSize:
11
+ Max: 40
12
+
13
+ Metrics/MethodLength:
14
+ Max: 40
15
+
16
+ Metrics/BlockLength:
17
+ Exclude:
18
+ - db/schema.rb
19
+ Max: 50
20
+
21
+ Metrics/ClassLength:
22
+ Max: 200
23
+ Exclude:
24
+ - test/**/*.rb
25
+
26
+ Metrics/CyclomaticComplexity:
27
+ Max: 10
28
+
29
+ Metrics/PerceivedComplexity:
30
+ Max: 15
31
+
32
+ Style/MultilineTernaryOperator:
33
+ Enabled: false
34
+
35
+ Style/DoubleNegation:
36
+ Enabled: false
37
+
38
+ Style/NumericLiterals:
39
+ Exclude:
40
+ - 'db/schema.rb'
41
+
42
+ Lint/PercentStringArray:
43
+ Exclude:
44
+ - 'config/initializers/csp.rb'
45
+
46
+ Style/StringLiterals:
47
+ EnforcedStyle: double_quotes
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_throttle.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,68 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rails_throttle (0.1.0)
5
+ activesupport
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (5.2.3)
11
+ actionview (= 5.2.3)
12
+ activesupport (= 5.2.3)
13
+ rack (~> 2.0)
14
+ rack-test (>= 0.6.3)
15
+ rails-dom-testing (~> 2.0)
16
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
17
+ actionview (5.2.3)
18
+ activesupport (= 5.2.3)
19
+ builder (~> 3.1)
20
+ erubi (~> 1.4)
21
+ rails-dom-testing (~> 2.0)
22
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
23
+ activesupport (5.2.3)
24
+ concurrent-ruby (~> 1.0, >= 1.0.2)
25
+ i18n (>= 0.7, < 2)
26
+ minitest (~> 5.1)
27
+ tzinfo (~> 1.1)
28
+ builder (3.2.3)
29
+ concurrent-ruby (1.1.5)
30
+ crass (1.0.4)
31
+ declarative_minitest (0.1.1)
32
+ minitest (>= 5.0)
33
+ erubi (1.8.0)
34
+ i18n (1.6.0)
35
+ concurrent-ruby (~> 1.0)
36
+ loofah (2.2.3)
37
+ crass (~> 1.0.2)
38
+ nokogiri (>= 1.5.9)
39
+ mini_portile2 (2.4.0)
40
+ minitest (5.11.3)
41
+ nokogiri (1.10.3)
42
+ mini_portile2 (~> 2.4.0)
43
+ rack (2.0.7)
44
+ rack-test (1.1.0)
45
+ rack (>= 1.0, < 3)
46
+ rails-dom-testing (2.0.3)
47
+ activesupport (>= 4.2.0)
48
+ nokogiri (>= 1.6)
49
+ rails-html-sanitizer (1.0.4)
50
+ loofah (~> 2.2, >= 2.2.2)
51
+ rake (10.5.0)
52
+ thread_safe (0.3.6)
53
+ tzinfo (1.2.5)
54
+ thread_safe (~> 0.1)
55
+
56
+ PLATFORMS
57
+ ruby
58
+
59
+ DEPENDENCIES
60
+ actionpack
61
+ bundler (~> 1.17)
62
+ declarative_minitest (~> 0.1.1)
63
+ minitest (~> 5.0)
64
+ rails_throttle!
65
+ rake (~> 10.0)
66
+
67
+ BUNDLED WITH
68
+ 1.17.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Peter Zhu
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,57 @@
1
+ # RailsThrottle
2
+
3
+ RailsThrottle is a simple library used to throttle code in your Rails application.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "rails_throttle", "~> 0.1.0"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rails_throttle
20
+
21
+ ## Usage
22
+
23
+ - See full documentation here:
24
+ - Example configuration (for production, you can configure other environments in a similar way):
25
+ ```ruby
26
+ # In config/environments/production.rb
27
+ RailsThrottle.backend = ActiveSupport::Cache::RedisStore.new "localhost:6379/0"
28
+
29
+ # Alternatively, also supports any of the other caching strategies of ActiveSupport::Cache.
30
+ # For details, see https://api.rubyonrails.org/classes/ActiveSupport/Cache.html.
31
+ ```
32
+ - Example usage:
33
+ ```ruby
34
+ RailsThrottle::Throttle.increment("foo", limit: 4, period: 2.seconds) # => 1
35
+ RailsThrottle::Throttle.increment("foo", limit: 4, period: 2.seconds) # => 2
36
+ RailsThrottle::Throttle.decrement("foo", limit: 4, period: 2.seconds) # => 1
37
+ RailsThrottle::Throttle.reset("foo")
38
+ RailsThrottle::Throttle.increment("foo", limit: 4, period: 2.seconds, increment: 4) # => 4
39
+ RailsThrottle::Throttle.increment("foo", limit: 4, period: 2.seconds) # => RailsThrottle::ThrottleError
40
+ RailsThrottle::Throttle.throttled?("foo", limit: 5) # => true
41
+
42
+ # ... wait 2 seconds ...
43
+
44
+ RailsThrottle::Throttle.throttled?("foo", limit: 5) # => false
45
+ ```
46
+
47
+ ## Development
48
+
49
+ Just the usual clone and `bundle` to install dependencies. Run `rake test` to run tests.
50
+
51
+ ## Contributing
52
+
53
+ Bug reports and pull requests are welcome on GitHub at https://github.com/peterzhu2118/rails_throttle.
54
+
55
+ ## License
56
+
57
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rails_throttle"
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,92 @@
1
+ module RailsThrottle
2
+ class Throttle
3
+ # Increments the counter for a given key.
4
+ #
5
+ # @param [String] key The key that uniquely identifies this throttle operation.
6
+ # @param [Hash] options The options to this increment.
7
+ # @option options [Integer] :increment The amount to increment the counter by, defaults to 1 if not provided.
8
+ # @option options [Integer] :limit The maximum value the key can take before it is throttled (i.e. this key will
9
+ # be throttled once exceeded this limit).
10
+ # @option options [Integer] :period The period of time (in seconds) which this throttle applies, the throttle will
11
+ # expire after this number of seconds.
12
+ # @yield [key, value] Yields up to two parameters (you can choose whether you want zero, one, or two of these
13
+ # parameters).
14
+ # @yieldparam [String] key The key that was passed in.
15
+ # @yieldparam [Integer] value The current value of the throttle counter.
16
+ # @return [Integer] The current value of the throttle counter.
17
+ def self.increment(key, options = {}, &block)
18
+ raise "Key cannot be blank" if key.blank?
19
+
20
+ increment = options[:increment] || 1
21
+ limit = options[:limit]
22
+ period = options[:period]
23
+
24
+ raise "Must specify :limit in the parameters" if limit.nil?
25
+ raise "Must specify :period in the parameters" if period.nil?
26
+
27
+ value = RailsThrottle.backend.increment(key, increment)
28
+
29
+ if value.nil?
30
+ RailsThrottle.backend.write(key, increment, expires_in: period)
31
+
32
+ value = increment
33
+ elsif value > limit
34
+ raise RailsThrottle::ThrottleError, "Limit exceeded for key `#{key}` with limit of #{limit}"
35
+ end
36
+
37
+ unless block.nil?
38
+ if block.arity.zero?
39
+ block.call
40
+ elsif block.arity == 1
41
+ block.call(key)
42
+ else
43
+ block.call(key, value)
44
+ end
45
+ end
46
+
47
+ value
48
+ end
49
+
50
+ # Decrements the counter for a given key.
51
+ #
52
+ # @param [String] key The key that uniquely identifies this throttle operation.
53
+ # @param [Hash] options The options to this increment.
54
+ # @option options [Integer] :decrement The amount to decrement the counter by, defaults to 1 if not provided.
55
+ # @return [Integer] The current value of the throttle counter.
56
+ def self.decrement(key, options = {})
57
+ raise "Key cannot be blank." if key.blank?
58
+
59
+ decrement = options[:decrement] || 1
60
+
61
+ RailsThrottle.backend.decrement(key, decrement)
62
+ end
63
+
64
+ # Returns true if the key is throttled, false otherwise.
65
+ #
66
+ # @param [String] key The key that uniquely identifies this throttle operation.
67
+ # @param [Hash] options The options to this increment.
68
+ # @option options [Integer] :limit The maximum value the key can take before it is throttled (i.e. this key will
69
+ # be throttled once exceeded this limit).
70
+ # @return [Boolean] True if the key is throttled, false otherwise.
71
+ def self.throttled?(key, options = {})
72
+ raise "Key cannot be blank." if key.blank?
73
+
74
+ limit = options[:limit]
75
+
76
+ raise "Must specify :limit in the parameters" if limit.nil?
77
+
78
+ value = RailsThrottle.backend.read(key) || 0
79
+
80
+ value >= limit
81
+ end
82
+
83
+ # Resets a key so that it is no longer throttled.
84
+ #
85
+ # @param [String] key The key that uniquely identifies this throttle operation.
86
+ def self.reset(key)
87
+ raise "Key cannot be blank." if key.blank?
88
+
89
+ RailsThrottle.backend.delete(key)
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,3 @@
1
+ module RailsThrottle
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1,16 @@
1
+ require "active_support/cache"
2
+
3
+ require "rails_throttle/version"
4
+ require "rails_throttle/throttle"
5
+
6
+ module RailsThrottle
7
+ mattr_accessor :backend
8
+
9
+ class ThrottleError < StandardError
10
+ def initialize(message = nil)
11
+ super(message)
12
+ end
13
+ end
14
+ end
15
+
16
+ RailsThrottle.backend ||= ActiveSupport::Cache::MemoryStore.new
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "rails_throttle/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rails_throttle"
7
+ spec.version = RailsThrottle::VERSION
8
+ spec.authors = ["Peter Zhu"]
9
+ spec.email = ["peter@peterzhu.ca"]
10
+
11
+ spec.summary = "Throttle code in your Rails application."
12
+ spec.homepage = "https://github.com/peterzhu2118/rails_throttle"
13
+ spec.license = "MIT"
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| 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_dependency "activesupport"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.17"
27
+ spec.add_development_dependency "declarative_minitest", "~> 0.1.1"
28
+ spec.add_development_dependency "minitest", "~> 5.0"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_throttle
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Peter Zhu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-04-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: declarative_minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
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'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ description:
84
+ email:
85
+ - peter@peterzhu.ca
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".circleci/config.yml"
91
+ - ".gitignore"
92
+ - ".rubocop.yml"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/rails_throttle.rb
101
+ - lib/rails_throttle/throttle.rb
102
+ - lib/rails_throttle/version.rb
103
+ - rails_throttle.gemspec
104
+ homepage: https://github.com/peterzhu2118/rails_throttle
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.7.6
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Throttle code in your Rails application.
128
+ test_files: []