bottleneck 0.4.1

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
+ SHA1:
3
+ metadata.gz: 3649703aee00044bc91147c7489cf0c80e458a21
4
+ data.tar.gz: 12a5b811e2562ca04f2abc265d5aa0c499729101
5
+ SHA512:
6
+ metadata.gz: db0db32c604752d3c68183fadc2ab74592eeeecd40607708596a9ce9593005fa3b21132ac0384b8603b86ee16091bc5a4fe475d688aa09c40ac5ec9fc827e100
7
+ data.tar.gz: 24632e961b71137446477527fef6a1a013766e241191e9ae609b99077ef2b2745bedabbe65c96b701a0e2dbf2c0548eb57aada8eced21f3c39f0a1ff827ca164
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/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ Style/FrozenStringLiteralComment:
2
+ Enabled: false
3
+ Metrics/LineLength:
4
+ Max: 120
5
+ Style/Documentation:
6
+ Enabled: false
7
+ Metrics/MethodLength:
8
+ Max: 30
9
+ Style/StringLiterals:
10
+ EnforcedStyle: double_quotes
11
+ SupportedStyles:
12
+ - single_quotes
13
+ - double_quotes
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.16.0
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,53 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bottleneck (0.4.0)
5
+ redis
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.0)
11
+ diff-lcs (1.3)
12
+ parallel (1.12.1)
13
+ parser (2.5.0.2)
14
+ ast (~> 2.4.0)
15
+ powerpack (0.1.1)
16
+ rainbow (3.0.0)
17
+ rake (10.5.0)
18
+ redis (4.0.1)
19
+ rspec (3.7.0)
20
+ rspec-core (~> 3.7.0)
21
+ rspec-expectations (~> 3.7.0)
22
+ rspec-mocks (~> 3.7.0)
23
+ rspec-core (3.7.1)
24
+ rspec-support (~> 3.7.0)
25
+ rspec-expectations (3.7.0)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.7.0)
28
+ rspec-mocks (3.7.0)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.7.0)
31
+ rspec-support (3.7.1)
32
+ rubocop (0.52.1)
33
+ parallel (~> 1.10)
34
+ parser (>= 2.4.0.2, < 3.0)
35
+ powerpack (~> 0.1)
36
+ rainbow (>= 2.2.2, < 4.0)
37
+ ruby-progressbar (~> 1.7)
38
+ unicode-display_width (~> 1.0, >= 1.0.1)
39
+ ruby-progressbar (1.9.0)
40
+ unicode-display_width (1.3.0)
41
+
42
+ PLATFORMS
43
+ ruby
44
+
45
+ DEPENDENCIES
46
+ bottleneck!
47
+ bundler (~> 1.16)
48
+ rake (~> 10.0)
49
+ rspec (~> 3.0)
50
+ rubocop
51
+
52
+ BUNDLED WITH
53
+ 1.16.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Marat Galiev
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,43 @@
1
+ # Bottleneck
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/bottleneck`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'bottleneck'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install bottleneck
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bottleneck. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Bottleneck project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/bottleneck/blob/master/CODE_OF_CONDUCT.md).
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 "bottleneck"
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,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "bottleneck/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bottleneck"
8
+ spec.version = Bottleneck::VERSION
9
+ spec.authors = ["Marat Galiev"]
10
+ spec.email = ["kazanlug@gmail.com"]
11
+
12
+ spec.summary = "Rate limiter"
13
+ spec.description = "Simple Redis based Rate Limiter for Rails applications"
14
+ spec.homepage = "https://github.com/maratgaliev/bottleneck"
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 "rubocop", "~> 0.52.1"
28
+ spec.add_dependency "redis", "~> 4.0"
29
+ end
@@ -0,0 +1,7 @@
1
+ module Bottleneck
2
+ class Constants
3
+ SUCCESS_STATUS = 200
4
+ EXPIRED_STATUS = 429
5
+ OK_MESSAGE = "ok".freeze
6
+ end
7
+ end
@@ -0,0 +1,41 @@
1
+ require "bottleneck/constants"
2
+
3
+ module Bottleneck
4
+ class Core
5
+ def initialize(request)
6
+ @ip = request.remote_ip.to_s
7
+ @storage = Bottleneck.storage
8
+ @limits = Bottleneck.config["limits"]
9
+ end
10
+
11
+ def run
12
+ client_ip = @ip
13
+ # create key in redis, based on client's IP
14
+ key = "request_count:#{client_ip}"
15
+ result = { status: Constants::SUCCESS_STATUS, message: Constants::OK_MESSAGE }
16
+ requests_count = @storage.get(key)
17
+ unless requests_count
18
+ @storage.set(key, 0)
19
+ @storage.expire(key, @limits["time_period_seconds"])
20
+ end
21
+ if requests_count.to_i >= @limits["max_requests_count"]
22
+ result[:status] = Constants::EXPIRED_STATUS
23
+ result[:message] = message(period(key))
24
+ else
25
+ @storage.incr(key)
26
+ end
27
+ result
28
+ end
29
+
30
+ private
31
+
32
+ def period(key)
33
+ # get remaining seconds with TTL from redis
34
+ @storage.ttl(key)
35
+ end
36
+
37
+ def message(secs)
38
+ "Rate limit exceeded. Try again in #{secs} seconds"
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module Bottleneck
2
+ VERSION = "0.4.1".freeze
3
+ end
data/lib/bottleneck.rb ADDED
@@ -0,0 +1,35 @@
1
+ require "bottleneck/version"
2
+ require "bottleneck/core"
3
+ require "yaml"
4
+ require "redis"
5
+
6
+ module Bottleneck
7
+ class << self
8
+ def check(request)
9
+ Core.new(request).run
10
+ end
11
+
12
+ def storage
13
+ init_storage
14
+ end
15
+
16
+ # Init redis client from config
17
+ def init_storage
18
+ redis_conf = load_config("redis.yml")
19
+ Redis.new(host: redis_conf["host"], port: redis_conf["port"])
20
+ end
21
+
22
+ # Get limits from config
23
+ def config
24
+ load_config("bottleneck.yml")
25
+ end
26
+
27
+ private
28
+
29
+ def load_config(file)
30
+ path = File.join(Rails.root, "config", file)
31
+ raise "No #{file} file found in your config directory!" unless File.exist?(path)
32
+ YAML.load_file(path)
33
+ end
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bottleneck
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.1
5
+ platform: ruby
6
+ authors:
7
+ - Marat Galiev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-03 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: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.52.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.52.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: redis
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.0'
83
+ description: Simple Redis based Rate Limiter for Rails applications
84
+ email:
85
+ - kazanlug@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".rubocop.yml"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - bottleneck.gemspec
102
+ - lib/bottleneck.rb
103
+ - lib/bottleneck/constants.rb
104
+ - lib/bottleneck/core.rb
105
+ - lib/bottleneck/version.rb
106
+ homepage: https://github.com/maratgaliev/bottleneck
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.6.13
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Rate limiter
130
+ test_files: []