EasyThrottle 0.0.1 → 0.0.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
  SHA256:
3
- metadata.gz: f50c56588d5ab29db2d607f2907c5d936cff56b9a6d4b4b2ec46befecc908427
4
- data.tar.gz: 3917275880a09753c25c8487d2fb9094b690a01ab52f28e300fbe141d6cc6524
3
+ metadata.gz: 8f426466c8bb697b331c603be2d79d73da8d19c5d50fdcb5af75081c8f508eae
4
+ data.tar.gz: 503b7e3980210f7de6bea6d66b8d2a974fcc14abbaf2e02e8dda668cf5fdf744
5
5
  SHA512:
6
- metadata.gz: 85e43902ecef6ead3303c67a7fe05d7fa804fcad9eb8509eb89e85a2ff334a8ceb2e06dbf5426065bf9021ccb8edcba15addfa7253875e9af76e2c02142062a6
7
- data.tar.gz: 85ea1906fcb738199be91c8bc1115e709647d63d7f543802a5786dbba1638affa15af9c9a76db574f3e972a0f74e5804719d22c26fca5e320e6f5115c7419256
6
+ metadata.gz: f35adeade221e897e0bc6a6a00306bdea290c737ab0cb27eadc1b3321744fec3fc547cabdb4681abf9b0e91e0885239f3eaf6d8db30745b5d233fff98100b807
7
+ data.tar.gz: 482cc0a5e109f9938e04031dc5dc7a793aea47d0cb769e112824252375dd71a7d887331efdd6b4ff7a8aadea1a0f9684d1659b230dffb0e4edfd6dd73a5373eb
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 3.1.2
Binary file
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem 'redis', '>= 4.0'
data/Gemfile.lock ADDED
@@ -0,0 +1,18 @@
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
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'easy_throttle/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'EasyThrottle'
7
+ s.version = EasyThrottle::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Damian Szalbierz"]
10
+ s.email = ['szalbierz.d.k@gmail.com']
11
+ s.homepage = 'https://github.com/Szalbik/easy_throttle'
12
+ s.summary = "Allows you to throttle requests to an API."
13
+ s.licenses = ['MIT']
14
+
15
+ s.add_runtime_dependency 'redis', '~> 4.0'
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.require_paths = ['lib']
20
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EasyThrottle
4
+ VERSION = '0.0.2'
5
+ end
@@ -3,7 +3,7 @@
3
3
  require 'yaml'
4
4
  require 'redis'
5
5
 
6
- class Throttler
6
+ class EasyThrottle
7
7
  DEFAULT_OPTIONS = { interval: 60, limit: 1, burst: 20 }.freeze
8
8
  LIMITS = {
9
9
  # 'endpointName' => { interval: 10, limit: 1, burst: 1 },
@@ -34,7 +34,7 @@ class Throttler
34
34
  end
35
35
  end
36
36
  result
37
- rescue *config['errors'].map(&:constantize) => e
37
+ rescue StandardError => e
38
38
  if e.code == 429
39
39
  extend_key_duration
40
40
  retry
@@ -46,7 +46,7 @@ class Throttler
46
46
  retry
47
47
  end
48
48
 
49
- raise
49
+ raise e
50
50
  end
51
51
 
52
52
  def pool
@@ -70,4 +70,10 @@ class Throttler
70
70
  def extend_key_duration
71
71
  redis.psetex(burst_key, (options[:interval] + 1) * options[:burst] * 1000, true)
72
72
  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
73
79
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'minitest/autorun'
4
+ require_relative '../lib/throttler'
5
+
6
+ class ThrottlerTest < Minitest::Test
7
+ def setup
8
+ @endpoint = 'getItemOffers'
9
+ @prefix = 'US'
10
+ @redis = Minitest::Mock.new
11
+ @options = Throttler::DEFAULT_OPTIONS
12
+ @burst_key = "#{@prefix}:#{@endpoint}:burst"
13
+ @subject = Throttler.new(@endpoint, @prefix, @options)
14
+ end
15
+
16
+ def test_initialize
17
+ assert_equal @endpoint, @subject.instance_variable_get(:@endpoint)
18
+ assert_equal @prefix, @subject.instance_variable_get(:@prefix)
19
+ assert_equal @options, @subject.instance_variable_get(:@options)
20
+ end
21
+
22
+ def test_with_throttling
23
+ block = proc { 'result' }
24
+ @redis.expect(:pttl, 0)
25
+ @redis.expect(:psetex, true)
26
+ assert_equal 'result', @subject.with_throttling(&block)
27
+ end
28
+
29
+ def test_with_throttling_when_api_error_with_code_429_occurs
30
+ block = proc { raise StandardError.new(code: 429) }
31
+ assert_raises(StandardError) { @subject.with_throttling(&block) }
32
+ assert_mock @redis
33
+ end
34
+
35
+ def test_with_throttling_when_api_error_with_code_500_occurs
36
+ block = proc { raise StandardError.new(code: 500) }
37
+ assert_raises(StandardError) { @subject.with_throttling(&block) }
38
+ assert_mock @redis
39
+ end
40
+ end
metadata CHANGED
@@ -1,38 +1,46 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: EasyThrottle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damian Szalbierz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-15 00:00:00.000000000 Z
11
+ date: 2024-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '4.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4.0'
27
27
  description:
28
- email: szalbierz.d.k@gmail.com
28
+ email:
29
+ - szalbierz.d.k@gmail.com
29
30
  executables: []
30
31
  extensions: []
31
32
  extra_rdoc_files: []
32
33
  files:
34
+ - ".tool-versions"
35
+ - EasyThrottle-0.0.1.gem
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - easy_throttle.gemspec
33
39
  - lib/config.yml
34
- - lib/throttler.rb
35
- homepage:
40
+ - lib/easy_throttle.rb
41
+ - lib/easy_throttle/version.rb
42
+ - test/test_easy_throttle.rb
43
+ homepage: https://github.com/Szalbik/easy_throttle
36
44
  licenses:
37
45
  - MIT
38
46
  metadata: {}
@@ -51,8 +59,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
59
  - !ruby/object:Gem::Version
52
60
  version: '0'
53
61
  requirements: []
54
- rubygems_version: 3.5.5
62
+ rubygems_version: 3.3.7
55
63
  signing_key:
56
64
  specification_version: 4
57
65
  summary: Allows you to throttle requests to an API.
58
- test_files: []
66
+ test_files:
67
+ - test/test_easy_throttle.rb