EasyThrottle 0.0.2 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8f426466c8bb697b331c603be2d79d73da8d19c5d50fdcb5af75081c8f508eae
4
- data.tar.gz: 503b7e3980210f7de6bea6d66b8d2a974fcc14abbaf2e02e8dda668cf5fdf744
3
+ metadata.gz: 9332a078cd5d2315222eb5c21cb7b962eab5675fc37dcb3b735a363965df105e
4
+ data.tar.gz: 68bb1a74c62fb3c6a7fbaf9e943c7b1bca0c825c39c6a15b85ddd2c8349f3692
5
5
  SHA512:
6
- metadata.gz: f35adeade221e897e0bc6a6a00306bdea290c737ab0cb27eadc1b3321744fec3fc547cabdb4681abf9b0e91e0885239f3eaf6d8db30745b5d233fff98100b807
7
- data.tar.gz: 482cc0a5e109f9938e04031dc5dc7a793aea47d0cb769e112824252375dd71a7d887331efdd6b4ff7a8aadea1a0f9684d1659b230dffb0e4edfd6dd73a5373eb
6
+ metadata.gz: 42612634bbab62c840994e2f5b4593211cce22d19c69efe1ded7e6ed2b82a21e6209b725b1db71852a3175298824899dee5af6d6825de64ec34cb3c109346239
7
+ data.tar.gz: 8395d6b5e34d5db37b530a5805fb8696c88fb9e13600aa9d6537570c24ea601687446f30af4986ee24b09df37396852fd71dc4c0a4dfdc8b7b93f6874d71aa18
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ Gemfile.lock
2
+ .tool-versions
3
+ EasyThrottle-*.gem
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
14
14
 
15
15
  s.add_runtime_dependency 'redis', '~> 4.0'
16
16
 
17
- s.files = `git ls-files`.split("\n")
17
+ s.files = `git ls-files`.split("\n")
18
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
19
  s.require_paths = ['lib']
20
20
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EasyThrottle
4
+ class Configuration
5
+ attr_accessor :interval, :limit, :burst, :errors, :limits
6
+
7
+ def initialize
8
+ @interval = 60
9
+ @limit = 1
10
+ @burst = 20
11
+ @errors = [AmzSpApi::ApiError]
12
+ @limits = {}
13
+ end
14
+
15
+ def add_error(error)
16
+ @errors << error
17
+ end
18
+
19
+ # Options example: { interval: 10, limit: 1, burst: 1 }
20
+ def add_limit(endpoint, options = {})
21
+ @limits[endpoint] = options
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EasyThrottle
4
+ class << self
5
+ # Instantiate the Configuration singleton
6
+ # or return it. Remember that the instance
7
+ # has attribute readers so that we can access
8
+ # the configured values
9
+ def configuration
10
+ @configuration ||= Configuration.new
11
+ end
12
+
13
+ # This is the configure block definition.
14
+ # The configuration method will return the
15
+ # Configuration singleton, which is then yielded
16
+ # to the configure block. Then it's just a matter
17
+ # of using the attribute accessors we previously defined
18
+ def configure
19
+ yield(configuration)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EasyThrottle
4
+ class Error < StandardError
5
+ def initialize(msg: 'Rate limit exceeded', error: nil)
6
+ super
7
+ end
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EasyThrottle
4
- VERSION = '0.0.2'
4
+ VERSION = '0.1.0'
5
5
  end
data/lib/easy_throttle.rb CHANGED
@@ -1,24 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'yaml'
4
3
  require 'redis'
5
4
 
6
5
  class EasyThrottle
7
6
  DEFAULT_OPTIONS = { interval: 60, limit: 1, burst: 20 }.freeze
8
- LIMITS = {
9
- # 'endpointName' => { interval: 10, limit: 1, burst: 1 },
10
- }.freeze
11
7
 
12
8
  def self.with_throttling(endpoint, prefix, options = DEFAULT_OPTIONS, &block)
13
9
  new(endpoint, prefix, options).with_throttling { block.call }
14
10
  end
15
11
 
16
12
  def initialize(endpoint, prefix, options = DEFAULT_OPTIONS)
17
- @config = YAML.load_file('lib/config.yml')
18
13
  @endpoint = endpoint
19
14
  @prefix = prefix
20
15
  @redis = Redis.new
21
- @options = LIMITS.fetch(endpoint, options)
16
+ @options = EasyThrottle.configuration.limits.fetch(endpoint, options)
22
17
  end
23
18
 
24
19
  def with_throttling(&block)
@@ -34,7 +29,7 @@ class EasyThrottle
34
29
  end
35
30
  end
36
31
  result
37
- rescue StandardError => e
32
+ rescue EasyThrottle.configuration.errors => e
38
33
  if e.code == 429
39
34
  extend_key_duration
40
35
  retry
@@ -46,7 +41,7 @@ class EasyThrottle
46
41
  retry
47
42
  end
48
43
 
49
- raise e
44
+ raise EasyThrottle::Error.new(msg: e.message, error: e)
50
45
  end
51
46
 
52
47
  def pool
@@ -70,10 +65,4 @@ class EasyThrottle
70
65
  def extend_key_duration
71
66
  redis.psetex(burst_key, (options[:interval] + 1) * options[:burst] * 1000, true)
72
67
  end
73
-
74
- def constantize(str)
75
- str.split('::').inject(Object) do |mod, class_name|
76
- mod.const_get(class_name)
77
- end
78
- end
79
68
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: EasyThrottle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damian Szalbierz
@@ -31,13 +31,13 @@ executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
- - ".tool-versions"
35
- - EasyThrottle-0.0.1.gem
34
+ - ".gitignore"
36
35
  - Gemfile
37
- - Gemfile.lock
38
36
  - easy_throttle.gemspec
39
- - lib/config.yml
40
37
  - lib/easy_throttle.rb
38
+ - lib/easy_throttle/configuration.rb
39
+ - lib/easy_throttle/easy_throttle.rb
40
+ - lib/easy_throttle/error.rb
41
41
  - lib/easy_throttle/version.rb
42
42
  - test/test_easy_throttle.rb
43
43
  homepage: https://github.com/Szalbik/easy_throttle
data/.tool-versions DELETED
@@ -1 +0,0 @@
1
- ruby 3.1.2
Binary file
data/Gemfile.lock DELETED
@@ -1,18 +0,0 @@
1
- GEM
2
- remote: https://rubygems.org/
3
- specs:
4
- connection_pool (2.4.1)
5
- redis (5.1.0)
6
- redis-client (>= 0.17.0)
7
- redis-client (0.21.0)
8
- connection_pool
9
-
10
- PLATFORMS
11
- ruby
12
- x86_64-darwin-23
13
-
14
- DEPENDENCIES
15
- redis (>= 4.0)
16
-
17
- BUNDLED WITH
18
- 2.5.6
data/lib/config.yml DELETED
@@ -1,3 +0,0 @@
1
- errors:
2
- - 'AmzSpApi::ApiError'
3
-