limiter-ruby 0.1.15 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/limiter/client.rb +12 -17
- data/lib/limiter/configuration.rb +16 -0
- data/lib/limiter/error_handler.rb +2 -15
- data/lib/limiter/points.rb +6 -3
- data/lib/limiter/{rate_limit_response.rb → response_handler.rb} +11 -6
- data/lib/limiter/version.rb +1 -1
- data/lib/limiter.rb +14 -3
- data/limiter.gemspec +1 -1
- metadata +6 -5
- /data/lib/{limiter_ruby.rb → limiter-ruby.rb} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e0f3a57515e275f8742e8657b68fc2df8e20f58f79a34825a2e36ddd3f34e403
|
4
|
+
data.tar.gz: dd01a79150ae330c918b6a4407c877b002bbc098f3f670cd3f558131bcf04675
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e67358db6e6481608a1dc5af8f3ce27c4430d47edfa819fac58348be1f22294d8443f232970911ac647d663e3450ae21f8df7199b62ffb656395be5525b8e08
|
7
|
+
data.tar.gz: 7a6c65468b78af623fd36a93c877df5161a4d8b21af274030b5f8e1a5d1d25b9e5f33e2ec0c6a22d47f4001ded723157a0f6abeaacf5625bff1c53eae4b4386b
|
data/lib/limiter/client.rb
CHANGED
@@ -3,24 +3,26 @@
|
|
3
3
|
module Limiter
|
4
4
|
class Client
|
5
5
|
|
6
|
-
|
6
|
+
attr_reader :namespace, :limit, :interval, :identifier, :token, :response
|
7
|
+
delegate :exhausted?, :allowed?, :exceeded?, :resets_in, :to => :response
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
def initialize(namespace:, limit:, interval:, token: ENV["LIMITER_TOKEN"])
|
9
|
+
def initialize(namespace:, limit:, interval:)
|
11
10
|
@namespace = namespace
|
12
11
|
@limit = limit
|
13
12
|
@interval = interval.to_i
|
14
|
-
@token = token || ENV["LIMITER_TOKEN"]
|
15
13
|
@logger = Limiter.logger
|
16
14
|
|
17
|
-
ErrorHandler.error("
|
15
|
+
ErrorHandler.error("API Token is not set") if Limiter.configuration.api_token.nil?
|
18
16
|
end
|
19
17
|
|
20
18
|
def check(identifier)
|
21
19
|
@identifier = identifier
|
22
|
-
@logger.info("
|
23
|
-
|
20
|
+
@logger.info("check: #{namespace}/#{limit}/#{interval}/#{identifier}")
|
21
|
+
@response = ResponseHandler.new(request)
|
22
|
+
self
|
23
|
+
rescue => e
|
24
|
+
ErrorHandler.error("check failed: #{e.message}")
|
25
|
+
self
|
24
26
|
end
|
25
27
|
|
26
28
|
def formatted_interval
|
@@ -39,18 +41,11 @@ module Limiter
|
|
39
41
|
end
|
40
42
|
|
41
43
|
def limiter_path
|
42
|
-
"/
|
44
|
+
"/ctr/#{namespace}/#{limit}/#{formatted_interval}/#{identifier}"
|
43
45
|
end
|
44
46
|
|
45
47
|
def request(params = {})
|
46
|
-
|
47
|
-
conn.request :authorization, :Bearer, token
|
48
|
-
conn.response :json
|
49
|
-
conn.adapter Faraday.default_adapter
|
50
|
-
conn.headers["User-Agent"] = "Limiter-Ruby/#{Limiter::VERSION}"
|
51
|
-
end
|
52
|
-
|
53
|
-
@_conn.get(limiter_path, params)
|
48
|
+
HTTP.auth("Bearer #{Limiter.configuration.api_token}").get(url, params: params)
|
54
49
|
end
|
55
50
|
end
|
56
51
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Limiter
|
2
|
+
class Configuration
|
3
|
+
API_BASE_URL = "https://api.limiter.dev"
|
4
|
+
|
5
|
+
attr_reader :api_token, :raise_errors
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@api_token = ENV["LIMITER_TOKEN"]
|
9
|
+
@raise_errors = true
|
10
|
+
end
|
11
|
+
|
12
|
+
def raise_errors?
|
13
|
+
@raise_errors
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -13,24 +13,11 @@ module Limiter
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def handle_error
|
16
|
-
if
|
16
|
+
if Limiter.configuration.raise_errors?
|
17
17
|
raise Error, @error
|
18
18
|
else
|
19
|
-
|
19
|
+
Limiter.logger.error(@error)
|
20
20
|
end
|
21
21
|
end
|
22
|
-
|
23
|
-
def production?
|
24
|
-
if defined?(Rails)
|
25
|
-
Rails.env.production?
|
26
|
-
else
|
27
|
-
(ENV["RACK_ENV"] || ENV["RAILS_ENV"] ) == "production"
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def log(error)
|
32
|
-
@logger ||= Limiter.logger
|
33
|
-
@logger.error(error)
|
34
|
-
end
|
35
22
|
end
|
36
23
|
end
|
data/lib/limiter/points.rb
CHANGED
@@ -3,12 +3,15 @@
|
|
3
3
|
module Limiter
|
4
4
|
class Points < Client
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
delegate :remaining, :points, :to => :response
|
7
|
+
|
8
|
+
def limiter_path
|
9
|
+
"/pts/#{namespace}/#{limit}/#{formatted_period}/#{identifier}"
|
8
10
|
end
|
9
11
|
|
10
12
|
def used(points)
|
11
|
-
|
13
|
+
@response = ResponseHandler.new(request({ used: points.to_i }))
|
14
|
+
self
|
12
15
|
end
|
13
16
|
end
|
14
17
|
end
|
@@ -1,23 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Limiter
|
4
|
-
class
|
4
|
+
class ResponseHandler
|
5
5
|
|
6
6
|
attr_reader :response
|
7
7
|
|
8
8
|
def initialize(response)
|
9
9
|
@response = response
|
10
|
-
@logger = Limiter.logger
|
11
10
|
end
|
12
11
|
|
13
12
|
def exhausted?
|
14
|
-
@logger.info("Limiter response: #{@response.status}")
|
15
13
|
signed_request? && @response.status == 429
|
16
14
|
end
|
17
15
|
alias_method :exceeded?, :exhausted?
|
18
16
|
|
19
17
|
def allowed?
|
20
|
-
@logger.info("Limiter response: #{@response.status}")
|
21
18
|
signed_request? && @response.status == 200
|
22
19
|
end
|
23
20
|
|
@@ -29,17 +26,25 @@ module Limiter
|
|
29
26
|
end
|
30
27
|
end
|
31
28
|
|
29
|
+
def remaining
|
30
|
+
response_data["remaining"]
|
31
|
+
end
|
32
|
+
|
33
|
+
def points
|
34
|
+
response_data["points"]
|
35
|
+
end
|
36
|
+
|
32
37
|
def signed_request?
|
33
38
|
@response.headers["X-Limiter-Signed"].to_s == "true"
|
34
39
|
end
|
35
40
|
|
36
41
|
private
|
37
|
-
def
|
42
|
+
def response_data
|
38
43
|
@_body ||= @response.parse
|
39
44
|
end
|
40
45
|
|
41
46
|
def resets_at
|
42
|
-
|
47
|
+
response_data["resetsAt"]
|
43
48
|
end
|
44
49
|
end
|
45
50
|
end
|
data/lib/limiter/version.rb
CHANGED
data/lib/limiter.rb
CHANGED
@@ -2,18 +2,29 @@
|
|
2
2
|
|
3
3
|
require_relative "limiter/client"
|
4
4
|
require_relative "limiter/points"
|
5
|
-
require_relative "limiter/
|
5
|
+
require_relative "limiter/response_handler"
|
6
6
|
require_relative "limiter/error_handler"
|
7
7
|
require_relative "limiter/version"
|
8
|
+
require "logger"
|
8
9
|
|
9
10
|
module Limiter
|
10
11
|
class Error < StandardError; end
|
11
12
|
|
12
13
|
class << self
|
13
|
-
attr_writer :logger
|
14
|
+
attr_writer :logger, :configuration
|
14
15
|
end
|
15
16
|
|
16
17
|
def self.logger
|
17
|
-
@logger ||= Logger.new(
|
18
|
+
@logger ||= Logger.new($stdout).tap do |logger|
|
19
|
+
logger.progname = "Limiter"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.configuration
|
24
|
+
@configuration ||= Configuration.new
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.configure
|
28
|
+
yield(configuration)
|
18
29
|
end
|
19
30
|
end
|
data/limiter.gemspec
CHANGED
@@ -31,7 +31,7 @@ Gem::Specification.new do |spec|
|
|
31
31
|
spec.require_paths = ["lib"]
|
32
32
|
|
33
33
|
# Uncomment to register a new dependency of your gem
|
34
|
-
spec.add_dependency "
|
34
|
+
spec.add_dependency "http"
|
35
35
|
|
36
36
|
# For more information and examples about making a new gem, check out our
|
37
37
|
# guide at: https://bundler.io/guides/creating_gem.html
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: limiter-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
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-12-
|
11
|
+
date: 2024-12-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: http
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
@@ -37,13 +37,14 @@ files:
|
|
37
37
|
- LICENSE.txt
|
38
38
|
- README.md
|
39
39
|
- Rakefile
|
40
|
+
- lib/limiter-ruby.rb
|
40
41
|
- lib/limiter.rb
|
41
42
|
- lib/limiter/client.rb
|
43
|
+
- lib/limiter/configuration.rb
|
42
44
|
- lib/limiter/error_handler.rb
|
43
45
|
- lib/limiter/points.rb
|
44
|
-
- lib/limiter/
|
46
|
+
- lib/limiter/response_handler.rb
|
45
47
|
- lib/limiter/version.rb
|
46
|
-
- lib/limiter_ruby.rb
|
47
48
|
- limiter.gemspec
|
48
49
|
homepage: https://limiter.dev
|
49
50
|
licenses:
|
File without changes
|