bottleneck 0.4.1 → 0.4.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3649703aee00044bc91147c7489cf0c80e458a21
4
- data.tar.gz: 12a5b811e2562ca04f2abc265d5aa0c499729101
3
+ metadata.gz: 42281ac9ac20f1a15452c335a67fa510bc4cf4a1
4
+ data.tar.gz: 21f2ebab21d35f82ed6a67a5406ae376e71d1ffa
5
5
  SHA512:
6
- metadata.gz: db0db32c604752d3c68183fadc2ab74592eeeecd40607708596a9ce9593005fa3b21132ac0384b8603b86ee16091bc5a4fe475d688aa09c40ac5ec9fc827e100
7
- data.tar.gz: 24632e961b71137446477527fef6a1a013766e241191e9ae609b99077ef2b2745bedabbe65c96b701a0e2dbf2c0548eb57aada8eced21f3c39f0a1ff827ca164
6
+ metadata.gz: 895fc354c07a4d1f775c087ed100ff4e257a6a51d544560fe81b8522ec06242897ad9a29df5257ac4768b6b2a13a969df3007032b7f6ad5861739adaf3d993a6
7
+ data.tar.gz: eae36948a25b82c5f1b3a2f5b86e39bb163a0866c10629de0c78959f7902b7a2d7b486abd04fd3b222281ea9da65130fafcf0f22e102b56b5e3701a5b20e4088
data/.rubocop.yml CHANGED
@@ -10,4 +10,6 @@ Style/StringLiterals:
10
10
  EnforcedStyle: double_quotes
11
11
  SupportedStyles:
12
12
  - single_quotes
13
- - double_quotes
13
+ - double_quotes
14
+ Metrics/BlockLength:
15
+ ExcludedMethods: ['describe', 'context']
data/Gemfile.lock CHANGED
@@ -1,8 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bottleneck (0.4.0)
5
- redis
4
+ bottleneck (0.4.1)
5
+ redis (~> 4.0)
6
+ redis-namespace (~> 1.6.0)
6
7
 
7
8
  GEM
8
9
  remote: https://rubygems.org/
@@ -16,6 +17,8 @@ GEM
16
17
  rainbow (3.0.0)
17
18
  rake (10.5.0)
18
19
  redis (4.0.1)
20
+ redis-namespace (1.6.0)
21
+ redis (>= 3.0.4)
19
22
  rspec (3.7.0)
20
23
  rspec-core (~> 3.7.0)
21
24
  rspec-expectations (~> 3.7.0)
@@ -47,7 +50,7 @@ DEPENDENCIES
47
50
  bundler (~> 1.16)
48
51
  rake (~> 10.0)
49
52
  rspec (~> 3.0)
50
- rubocop
53
+ rubocop (~> 0.52.1)
51
54
 
52
55
  BUNDLED WITH
53
56
  1.16.0
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  # Bottleneck
2
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.
3
+ Bottleneck - simple Redis based requests limiter with two params:
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ * time_period_seconds: 3600
6
+ * max_requests_count: 100
6
7
 
7
8
  ## Installation
8
9
 
@@ -22,22 +23,34 @@ Or install it yourself as:
22
23
 
23
24
  ## Usage
24
25
 
25
- TODO: Write usage instructions here
26
+ Create two yaml files in config dir:
26
27
 
27
- ## Development
28
+ **bottleneck.yml**
28
29
 
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
+ limits:
31
+ time_period_seconds: 3600
32
+ max_requests_count: 100
30
33
 
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).
34
+ **redis.yml**
35
+ host: 'localhost'
36
+ port: 6379
37
+
38
+ Add before action:
39
+ class ApplicationController < ActionController::API
40
+ before_action :check_limit
41
+
42
+ private
43
+
44
+ def check_limit
45
+ result = Bottleneck.check(request)
46
+ render status: result[:status], json: { message: result[:message] }
47
+ end
48
+ end
32
49
 
33
50
  ## Contributing
34
51
 
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.
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/maratgaliev/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
53
 
37
54
  ## License
38
55
 
39
56
  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/bottleneck.gemspec CHANGED
@@ -26,4 +26,5 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency "rspec", "~> 3.0"
27
27
  spec.add_development_dependency "rubocop", "~> 0.52.1"
28
28
  spec.add_dependency "redis", "~> 4.0"
29
+ spec.add_dependency "redis-namespace", "~> 1.6.0"
29
30
  end
@@ -0,0 +1,3 @@
1
+ limits:
2
+ time_period_seconds: 3
3
+ max_requests_count: 3
data/config/redis.yml ADDED
@@ -0,0 +1,2 @@
1
+ host: '127.0.0.1'
2
+ port: 6379
@@ -2,8 +2,8 @@ require "bottleneck/constants"
2
2
 
3
3
  module Bottleneck
4
4
  class Core
5
- def initialize(request)
6
- @ip = request.remote_ip.to_s
5
+ def initialize(ip)
6
+ @ip = ip.to_s
7
7
  @storage = Bottleneck.storage
8
8
  @limits = Bottleneck.config["limits"]
9
9
  end
@@ -1,3 +1,3 @@
1
1
  module Bottleneck
2
- VERSION = "0.4.1".freeze
2
+ VERSION = "0.4.2".freeze
3
3
  end
data/lib/bottleneck.rb CHANGED
@@ -2,23 +2,28 @@ require "bottleneck/version"
2
2
  require "bottleneck/core"
3
3
  require "yaml"
4
4
  require "redis"
5
+ require "redis-namespace"
5
6
 
6
7
  module Bottleneck
7
8
  class << self
8
- def check(request)
9
- Core.new(request).run
9
+ def check(ip)
10
+ Core.new(ip).run
10
11
  end
11
12
 
12
13
  def storage
13
14
  init_storage
14
15
  end
15
16
 
16
- # Init redis client from config
17
- def init_storage
17
+ def redis_conn
18
18
  redis_conf = load_config("redis.yml")
19
19
  Redis.new(host: redis_conf["host"], port: redis_conf["port"])
20
20
  end
21
21
 
22
+ # Init redis client from config
23
+ def init_storage
24
+ Redis::Namespace.new(:bottleneck, redis: redis_conn)
25
+ end
26
+
22
27
  # Get limits from config
23
28
  def config
24
29
  load_config("bottleneck.yml")
@@ -27,7 +32,9 @@ module Bottleneck
27
32
  private
28
33
 
29
34
  def load_config(file)
30
- path = File.join(Rails.root, "config", file)
35
+ # Check for Rails defined, or use current path
36
+ root = (defined?(Rails) && Rails.respond_to?(:root) && Rails.root) || Dir.pwd
37
+ path = "#{root}/config/#{file}"
31
38
  raise "No #{file} file found in your config directory!" unless File.exist?(path)
32
39
  YAML.load_file(path)
33
40
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bottleneck
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marat Galiev
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '4.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: redis-namespace
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.6.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.6.0
83
97
  description: Simple Redis based Rate Limiter for Rails applications
84
98
  email:
85
99
  - kazanlug@gmail.com
@@ -99,6 +113,8 @@ files:
99
113
  - bin/console
100
114
  - bin/setup
101
115
  - bottleneck.gemspec
116
+ - config/bottleneck.yml
117
+ - config/redis.yml
102
118
  - lib/bottleneck.rb
103
119
  - lib/bottleneck/constants.rb
104
120
  - lib/bottleneck/core.rb