limiter-ruby 0.1.15 → 0.2.1
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 +15 -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: be6797974aa664c4b6dcc7db96803ca1f09dddec82ae5d4e8399b0c765b405c9
|
4
|
+
data.tar.gz: d9f651b84e46d8548bdf165aa87b9bd354b4fb99f0539ae6f998d9af4c1727ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 36fff074641761c6d1503f27c8e776b49c0516265b1a3d3b9ac8cc04b2a2ab9f2aa8024365ae26bae0a94a561a6a88b94e3c64f5fa5401e721f4fbc472eda890
|
7
|
+
data.tar.gz: e304ec1299c69c11b908bcfc31c79a1850296ab054dace630337baa9f7fe1f4022e32f577944313a5a2ee1b477c38e33eb2ea2e3ed2dc4af429168d96405a7cc
|
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
@@ -1,19 +1,31 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative "limiter/configuration"
|
3
4
|
require_relative "limiter/client"
|
4
5
|
require_relative "limiter/points"
|
5
|
-
require_relative "limiter/
|
6
|
+
require_relative "limiter/response_handler"
|
6
7
|
require_relative "limiter/error_handler"
|
7
8
|
require_relative "limiter/version"
|
9
|
+
require "logger"
|
8
10
|
|
9
11
|
module Limiter
|
10
12
|
class Error < StandardError; end
|
11
13
|
|
12
14
|
class << self
|
13
|
-
attr_writer :logger
|
15
|
+
attr_writer :logger, :configuration
|
14
16
|
end
|
15
17
|
|
16
18
|
def self.logger
|
17
|
-
@logger ||= Logger.new(
|
19
|
+
@logger ||= Logger.new($stdout).tap do |logger|
|
20
|
+
logger.progname = "Limiter"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.configuration
|
25
|
+
@configuration ||= Configuration.new
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.configure
|
29
|
+
yield(configuration)
|
18
30
|
end
|
19
31
|
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.1
|
4
|
+
version: 0.2.1
|
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
|