run_no_mo 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 25ea1d8b1acba1a0d62306275ea7cdc268f19b93
4
+ data.tar.gz: 0e3e346d46ac9f00eea24cb02d7da60589967de1
5
+ SHA512:
6
+ metadata.gz: 82867323afaf5efb93c651d1a655016d6b631181f51cf48df9f88b378143cae38759cf36ae6f54ac8a289a680a47c257175b9cee71e0d60ed90f829886b3e3cc
7
+ data.tar.gz: 3b323d6363bdf3b0c9e287bf66b3b595ee8e9773e6519c9a146584eefeafea25b9de5e8791918011c18a97d0fefd9b49cca4a0331d59525f313fc63b84d3c6fe
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
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 run_no_mo.gemspec
6
+ gemspec
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ run_no_mo (1.0.0)
5
+ activesupport (~> 5.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (5.2.4.2)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 0.7, < 2)
13
+ minitest (~> 5.1)
14
+ tzinfo (~> 1.1)
15
+ concurrent-ruby (1.1.6)
16
+ i18n (1.8.2)
17
+ concurrent-ruby (~> 1.0)
18
+ minitest (5.14.0)
19
+ rake (13.0.1)
20
+ thread_safe (0.3.6)
21
+ tzinfo (1.2.7)
22
+ thread_safe (~> 0.1)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ bundler (~> 1.16)
29
+ minitest (~> 5.0)
30
+ rake (~> 13.0)
31
+ run_no_mo!
32
+
33
+ BUNDLED WITH
34
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Joel Bandi
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.
@@ -0,0 +1,93 @@
1
+ # RunNoMo
2
+
3
+ Helps you rate limit the execution of code!
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'run_no_mo'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install run_no_mo
20
+
21
+ ## Usage (look at the test files for usage)
22
+
23
+ ```ruby
24
+ require "test_helper"
25
+
26
+ class RunNoMoTest < Minitest::Test
27
+ def test_that_it_has_a_version_number
28
+ refute_nil ::RunNoMo::VERSION
29
+ end
30
+
31
+ def test_limiter_limits__count
32
+ # create a limiter using factory
33
+ limiter = RunNoMo.limit times: 5, raises: true
34
+
35
+ # run 4 times
36
+ 4.times do
37
+ limiter.run { noop }
38
+ end
39
+
40
+ # run 1 more time
41
+ limiter.run { noop }
42
+
43
+ # run once again NO-MO
44
+ assert_raises RunNoMo::LimitExceeded do
45
+ # does not get executed
46
+ limiter.run { noop }
47
+ end
48
+ end
49
+
50
+ def test_limiter__limiters__count__within
51
+ limiter = RunNoMo::limit times: 2, within: 3.seconds, raises: true
52
+
53
+ assert_raises RunNoMo::LimitExceeded do
54
+ 3.times do
55
+ limiter.run { noop }
56
+ end
57
+ end
58
+
59
+ limiter = RunNoMo::limit times: 2, within: 1.second, raises: true
60
+
61
+ 3.times do
62
+ limiter.run { sleep 1 }
63
+ end
64
+ end
65
+
66
+ def test_limiter_no_throw
67
+ limiter = RunNoMo::limit times: 3
68
+
69
+ count = 0
70
+
71
+ 5.times do
72
+ limiter.run { count += 1 }
73
+ end
74
+
75
+ assert_equal 3, count
76
+ end
77
+
78
+ private
79
+
80
+ def noop; end
81
+ end
82
+
83
+ ```
84
+
85
+ ## Development
86
+
87
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
88
+
89
+ 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).
90
+
91
+ ## License
92
+
93
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "run_no_mo"
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__)
@@ -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,9 @@
1
+ require "run_no_mo/version"
2
+ require "run_no_mo/limiter"
3
+ require "active_support/all"
4
+
5
+ module RunNoMo
6
+ def self.limit(times:, within: nil, raises: false)
7
+ Limiter.new(times: times, within: within, raises: raises)
8
+ end
9
+ end
@@ -0,0 +1,45 @@
1
+ module RunNoMo
2
+ class LimitExceeded < StandardError; end
3
+
4
+ class Limiter
5
+ def initialize(times:, within:, raises:)
6
+ @times = times
7
+ @count = 0
8
+
9
+ @within = within
10
+ @last_run = nil
11
+
12
+ @raises = raises
13
+ end
14
+
15
+ def run(&block)
16
+ if reset_counter?
17
+ @count = 0
18
+ else
19
+ @count+= 1
20
+ end
21
+
22
+ @last_run = Time.now
23
+
24
+ return limit_exceeded! if @count > @times
25
+
26
+ block.call
27
+ end
28
+
29
+ private
30
+
31
+ def limit_exceeded!
32
+ raise LimitExceeded.new if @raises
33
+ end
34
+
35
+ def reset_counter?
36
+ return false if @within.nil?
37
+
38
+ return false if @last_run.nil?
39
+
40
+ return false if (Time.now - @last_run) < @within
41
+
42
+ true
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module RunNoMo
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,24 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "run_no_mo/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "run_no_mo"
8
+ spec.version = RunNoMo::VERSION
9
+ spec.authors = ["Joel Bandi"]
10
+ spec.email = ["joelvivekbandi@gmail.com"]
11
+ spec.license = "MIT"
12
+ spec.summary = "Rate limit execution of code."
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
+ f.match(%r{^(test|spec|features)/})
16
+ end
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+ spec.add_dependency 'activesupport', '~> 5.0'
21
+ spec.add_development_dependency "bundler", "~> 1.16"
22
+ spec.add_development_dependency "rake", "~> 13.0"
23
+ spec.add_development_dependency "minitest", "~> 5.0"
24
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: run_no_mo
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Joel Bandi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-05-09 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: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.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.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
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
+ description:
70
+ email:
71
+ - joelvivekbandi@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - lib/run_no_mo.rb
85
+ - lib/run_no_mo/limiter.rb
86
+ - lib/run_no_mo/version.rb
87
+ - run_no_mo.gemspec
88
+ homepage:
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.6.14
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Rate limit execution of code.
112
+ test_files: []