limiter-ruby 0.1.13 → 0.2.0

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: 87a959ee34d3f771f97ce012ab718b8313057c407f6441f4beed90a021a097c5
4
- data.tar.gz: 0271c7731bafb69956cc8f36636d7eab28e67def6c55c69565d911e9f757bb14
3
+ metadata.gz: e0f3a57515e275f8742e8657b68fc2df8e20f58f79a34825a2e36ddd3f34e403
4
+ data.tar.gz: dd01a79150ae330c918b6a4407c877b002bbc098f3f670cd3f558131bcf04675
5
5
  SHA512:
6
- metadata.gz: 214133e80b860d1e87d1005f09cf3e728da406ddee8d34100274f2064729997271dfb481bf227f163910146f4700b96decc1e7b9837d71e7f769417c98b9010a
7
- data.tar.gz: 00c72e252a8d733af4fcde4bdec7c3aa978450c53c26fc5548d992834550b54146aecc5e7ef165e42a50465ab7026c6ac36c73b4ad82ffa9604cc174d84ddbe7
6
+ metadata.gz: 9e67358db6e6481608a1dc5af8f3ce27c4430d47edfa819fac58348be1f22294d8443f232970911ac647d663e3450ae21f8df7199b62ffb656395be5525b8e08
7
+ data.tar.gz: 7a6c65468b78af623fd36a93c877df5161a4d8b21af274030b5f8e1a5d1d25b9e5f33e2ec0c6a22d47f4001ded723157a0f6abeaacf5625bff1c53eae4b4386b
@@ -3,22 +3,26 @@
3
3
  module Limiter
4
4
  class Client
5
5
 
6
- BASE_DOMAIN = "https://api.limiter.dev".freeze
6
+ attr_reader :namespace, :limit, :interval, :identifier, :token, :response
7
+ delegate :exhausted?, :allowed?, :exceeded?, :resets_in, :to => :response
7
8
 
8
- attr_reader :namespace, :limit, :interval, :identifier, :token
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"]
13
+ @logger = Limiter.logger
15
14
 
16
- ErrorHandler.error("token is not set") if @token.nil?
15
+ ErrorHandler.error("API Token is not set") if Limiter.configuration.api_token.nil?
17
16
  end
18
17
 
19
18
  def check(identifier)
20
19
  @identifier = identifier
21
- RateLimitResponse.new(request)
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
22
26
  end
23
27
 
24
28
  def formatted_interval
@@ -37,18 +41,11 @@ module Limiter
37
41
  end
38
42
 
39
43
  def limiter_path
40
- "/rl/#{namespace}/#{limit}/#{formatted_interval}/#{identifier}"
44
+ "/ctr/#{namespace}/#{limit}/#{formatted_interval}/#{identifier}"
41
45
  end
42
46
 
43
47
  def request(params = {})
44
- @_conn ||= Faraday.new(url: BASE_DOMAIN) do |conn|
45
- conn.request :authorization, :Bearer, token
46
- conn.response :json
47
- conn.adapter Faraday.default_adapter
48
- conn.headers["User-Agent"] = "Limiter-Ruby/#{Limiter::VERSION}"
49
- end
50
-
51
- @_conn.get(limiter_path, params)
48
+ HTTP.auth("Bearer #{Limiter.configuration.api_token}").get(url, params: params)
52
49
  end
53
50
  end
54
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 !production?
16
+ if Limiter.configuration.raise_errors?
17
17
  raise Error, @error
18
18
  else
19
- log @error
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 ||= Logger.new(STDOUT)
33
- @logger.error(error)
34
- end
35
22
  end
36
23
  end
@@ -3,12 +3,15 @@
3
3
  module Limiter
4
4
  class Points < Client
5
5
 
6
- def url
7
- "#{BASE_DOMAIN}/points/#{namespace}/#{limit}/#{formatted_period}/#{identifier}"
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
- RateLimitResponse.new(request({ used: points.to_i }))
13
+ @response = ResponseHandler.new(request({ used: points.to_i }))
14
+ self
12
15
  end
13
16
  end
14
17
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Limiter
4
- class RateLimitResponse
4
+ class ResponseHandler
5
5
 
6
6
  attr_reader :response
7
7
 
@@ -12,6 +12,7 @@ module Limiter
12
12
  def exhausted?
13
13
  signed_request? && @response.status == 429
14
14
  end
15
+ alias_method :exceeded?, :exhausted?
15
16
 
16
17
  def allowed?
17
18
  signed_request? && @response.status == 200
@@ -25,17 +26,25 @@ module Limiter
25
26
  end
26
27
  end
27
28
 
29
+ def remaining
30
+ response_data["remaining"]
31
+ end
32
+
33
+ def points
34
+ response_data["points"]
35
+ end
36
+
28
37
  def signed_request?
29
38
  @response.headers["X-Limiter-Signed"].to_s == "true"
30
39
  end
31
40
 
32
41
  private
33
- def rate_limit_data
42
+ def response_data
34
43
  @_body ||= @response.parse
35
44
  end
36
45
 
37
46
  def resets_at
38
- rate_limit_data["resetsAt"]
47
+ response_data["resetsAt"]
39
48
  end
40
49
  end
41
50
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Limiter
4
- VERSION = "0.1.13"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/limiter.rb CHANGED
@@ -2,10 +2,29 @@
2
2
 
3
3
  require_relative "limiter/client"
4
4
  require_relative "limiter/points"
5
- require_relative "limiter/rate_limit_response"
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
12
+
13
+ class << self
14
+ attr_writer :logger, :configuration
15
+ end
16
+
17
+ def self.logger
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)
29
+ end
11
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 "faraday"
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.13
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 00:00:00.000000000 Z
11
+ date: 2024-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: faraday
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/rate_limit_response.rb
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