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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 95cbfb0b951d027b39acd6a8a4a00acaac5fd57ba799c4e90ae9d81160a80955
4
- data.tar.gz: a32b6eb90650b7dcd98af36b97ce663fe424f13e252a51d9feff291f1665c8fe
3
+ metadata.gz: e0f3a57515e275f8742e8657b68fc2df8e20f58f79a34825a2e36ddd3f34e403
4
+ data.tar.gz: dd01a79150ae330c918b6a4407c877b002bbc098f3f670cd3f558131bcf04675
5
5
  SHA512:
6
- metadata.gz: 761359a7844639c497dac6136693d82704eb118ba5d915a6227f4b52a70d0932fa93f09a4d2216543fdbcf69c7ee42c96edd7674adc56bff65442e0751317e56
7
- data.tar.gz: 2390594ac176924ed4ea9bae0412ebeccb54af4d6019c3ee1523b45926e3da352c3cd9ed7749d28ebf0f6bb1f638fe2cb7890f3f7580d694bd471eb557a132b6
6
+ metadata.gz: 9e67358db6e6481608a1dc5af8f3ce27c4430d47edfa819fac58348be1f22294d8443f232970911ac647d663e3450ae21f8df7199b62ffb656395be5525b8e08
7
+ data.tar.gz: 7a6c65468b78af623fd36a93c877df5161a4d8b21af274030b5f8e1a5d1d25b9e5f33e2ec0c6a22d47f4001ded723157a0f6abeaacf5625bff1c53eae4b4386b
@@ -3,24 +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"]
15
13
  @logger = Limiter.logger
16
14
 
17
- ErrorHandler.error("token is not set") if @token.nil?
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("Limiter performing request: #{namespace}/#{limit}/#{interval}/#{identifier}")
23
- 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
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
- "/rl/#{namespace}/#{limit}/#{formatted_interval}/#{identifier}"
44
+ "/ctr/#{namespace}/#{limit}/#{formatted_interval}/#{identifier}"
43
45
  end
44
46
 
45
47
  def request(params = {})
46
- @_conn ||= Faraday.new(url: BASE_DOMAIN) do |conn|
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 !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 ||= Limiter.logger
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,23 +1,20 @@
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
 
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 rate_limit_data
42
+ def response_data
38
43
  @_body ||= @response.parse
39
44
  end
40
45
 
41
46
  def resets_at
42
- rate_limit_data["resetsAt"]
47
+ response_data["resetsAt"]
43
48
  end
44
49
  end
45
50
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Limiter
4
- VERSION = "0.1.15"
4
+ VERSION = "0.2.0"
5
5
  end
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/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
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(STDOUT)
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 "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.15
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