limiter-ruby 0.1.0 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 89a952e1a844303c615335021b9d5b8dc63d02ef09036f73aa28cd16b191875f
4
- data.tar.gz: 8475379bf65884d9376d2d37544900847712c8434b0ef1197d3427d7d5ccc276
3
+ metadata.gz: 718993a35bb1f7a95a16d2a6a17af233d9d0909fc070895172c80e5afbfbb7d0
4
+ data.tar.gz: '026853b283dddcd996539c3d255250e25ee4871e4cccf8ac424707ffac18ea77'
5
5
  SHA512:
6
- metadata.gz: d035c3075ca83f56ecd77afa1f190e0ea76bfdb7266fcf934f50b49e72be0e5c3ad7bd3440d16ad4b4177a7d3b92060f082dea62345afd4a9968f2ed9e3e199b
7
- data.tar.gz: 0604d0feffafebcb9462424fc24160ffd025f695d7a4b8e4237a34f93ba1fcb50cf2e075784e9ce1a59fcbecdda60abde265c2f58c15582fe4a1dfc7aba510c0
6
+ metadata.gz: a45d10cd11eb9500f9dc7fb77f379e509d49d1f91bd59ffe0f0bba698b88fba2f3a9edf57a1e5020ffa803032cf7d436d3bf166768c3e79fa8a65ae4a0159ca9
7
+ data.tar.gz: 9c3d29e2a42422b4bc02767e7c7f3248f462bab8d228fee536732481e5a737081a8aa99e512f37a199031ec2fbd139f904f05bdc6a5c4fbb84c0bd208ba211e9
data/README.md CHANGED
@@ -17,10 +17,10 @@ LIMITER_TOKEN: [GENERATED TOKEN]
17
17
  In the `Gemfile` add the following
18
18
 
19
19
  ```ruby
20
- gem "limiter"
20
+ gem "limiter-ruby"
21
21
  ```
22
22
 
23
- ## Integrate
23
+ ## Simple Rate Limit Example
24
24
 
25
25
  Assuming this is a Ruby on Rails app within ActiveJob
26
26
 
@@ -45,3 +45,29 @@ class ExpensiveJob < ApplicationJob
45
45
  end
46
46
  end
47
47
  ```
48
+
49
+ ## Point Based Rate Limit Example
50
+
51
+ ```ruby
52
+ class ExpensiveJob < ApplicationJob
53
+
54
+ def perform
55
+ rate_limit = limiter.check(shop.id) # A unique identifier
56
+
57
+ if rate_limit.exhausted? # Rate limit got hit
58
+ self.class.set(wait: rate_limit.resets_in).perform_later(..args)
59
+ return
60
+ else
61
+ # Good to continue expensive operation
62
+ # ...
63
+ # Perform query
64
+ limiter.used(250) # Points deducted for last query
65
+ end
66
+ end
67
+
68
+ private
69
+ def limiter
70
+ Limiter::Points.new(namespace: "shopify", limit: 1000, period: 1.minute)
71
+ end
72
+ end
73
+ ```
@@ -35,11 +35,11 @@ module Limiter
35
35
  end
36
36
 
37
37
  def url
38
- "#{BASE_DOMAIN}/#{namespace}/#{limit}/#{formatted_period}/#{identifier}"
38
+ "#{BASE_DOMAIN}/ns/#{namespace}/#{limit}/#{formatted_period}/#{identifier}"
39
39
  end
40
40
 
41
- def request
42
- HTTP.get(url, params: { token: token})
41
+ def request(params = {})
42
+ HTTP.get(url, params: { token: token}.merge(params))
43
43
  end
44
44
  end
45
45
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Limiter
4
+ class Points < Client
5
+
6
+ def url
7
+ "#{BASE_DOMAIN}/points/#{namespace}/#{limit}/#{formatted_period}/#{identifier}"
8
+ end
9
+
10
+ def used(points)
11
+ RateLimitResponse.new(request({ used: points.to_i }))
12
+ end
13
+ end
14
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Limiter
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.5"
5
5
  end
data/lib/limiter.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "http"
4
4
  require_relative "limiter/client"
5
+ require_relative "limiter/points"
5
6
  require_relative "limiter/rate_limit_response"
6
7
  require_relative "limiter/error_handler"
7
8
  require_relative "limiter/version"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: limiter-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bilal Budhani
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-05-13 00:00:00.000000000 Z
11
+ date: 2024-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -40,6 +40,7 @@ files:
40
40
  - lib/limiter.rb
41
41
  - lib/limiter/client.rb
42
42
  - lib/limiter/error_handler.rb
43
+ - lib/limiter/points.rb
43
44
  - lib/limiter/rate_limit_response.rb
44
45
  - lib/limiter/version.rb
45
46
  - limiter.gemspec