samcart_api 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 06fe1b45c2e3f892bd12685da744ad03e5f2b347714c2f5b0f142aaf6cd16a8d
4
- data.tar.gz: 922fbf5b295f2edbb889128f2a68108c9093516deafd711677d6524be34c966e
3
+ metadata.gz: b1bdda072456e66df0b1d954e208a19fe4046ed013c35507ea8d17a91a9919e4
4
+ data.tar.gz: 58417c4bc3cda196ca59dc8d99d5ac6ca39f6719037386aa777c1d07c9ea1a68
5
5
  SHA512:
6
- metadata.gz: '08ee150d36a9cf379a8f76e96ea40e087ff83e27d8946ff069fc3cc18f3f9bec64fe43b7f514e94334ee36ad248ffba1d22892c8e97ef9de2b2a613af651e524'
7
- data.tar.gz: e2d035a48f85abb24d477881761334ca9cd8a277eacf334e1125d1596076ceea0d87e8cb1885f680fa3ac86a5c1be4ce5267395851861167b94cfcfa5a66b5ad
6
+ metadata.gz: 9cc6675ff58a9ffa7dff671991e321d60261d4106c5fbf840fac0a349d2d69d372e4f797ca1a1da608affc349347601d765a29f3e61a2b24164984c148bc2588
7
+ data.tar.gz: ceb28e4b6a09859b3863f08d45bf680c2045fec382bde8a93a6f87b04bf075928591248f5404e9d4a3c0da24f1e65399378c4985a5b7d67ac4078e40d1cfc826
@@ -2,6 +2,8 @@
2
2
 
3
3
  module SamcartApi
4
4
  class ApiRequest
5
+ RETRY_ATTEMPTS = 3
6
+
5
7
  def initialize(method, path, params, api_key)
6
8
  @method = method
7
9
  @path = path
@@ -11,6 +13,12 @@ module SamcartApi
11
13
  end
12
14
 
13
15
  def perform
16
+ safe_request { make_request }
17
+ end
18
+
19
+ private
20
+
21
+ def make_request
14
22
  response = connection.send(@method) do |req|
15
23
  req.url "/#{@version}#{@path}"
16
24
  req.params = @params if @params
@@ -22,7 +30,30 @@ module SamcartApi
22
30
  handle_response(response)
23
31
  end
24
32
 
25
- private
33
+ def safe_request
34
+ retries = 0
35
+
36
+ begin
37
+ yield
38
+ rescue Faraday::TimeoutError, Faraday::ConnectionFailed => e
39
+ puts "Network error: #{e.message}. Retrying..."
40
+ sleep 2
41
+ retries += 1
42
+ retry if retries < RETRY_ATTEMPTS
43
+ raise 'Too many retries: Network issues'
44
+ rescue ApiError => e
45
+ if e.message.include?('Too Many Requests') # Custom handling for 429
46
+ wait_time = 5 # Default wait
47
+ puts "Rate limited! Retrying in #{wait_time} seconds..."
48
+ sleep wait_time
49
+ retries += 1
50
+ retry if retries < RETRY_ATTEMPTS
51
+ raise 'Too many retries: Rate limited'
52
+ else
53
+ raise
54
+ end
55
+ end
56
+ end
26
57
 
27
58
  def connection
28
59
  @connection ||= Faraday.new(url: SamcartApi.configuration.api_url) do |conn|
@@ -41,6 +72,8 @@ module SamcartApi
41
72
  raise AuthenticationError, 'Access denied'
42
73
  when 404
43
74
  raise ApiError, 'Resource not found'
75
+ when 429
76
+ raise ApiError, 'Too Many Requests'
44
77
  else
45
78
  raise ApiError, "API request failed: #{response.body["message"] || response.body}"
46
79
  end
@@ -6,25 +6,18 @@ module SamcartApi
6
6
 
7
7
  class << self
8
8
  def find(id)
9
- new(id).find
9
+ order = client.get("#{RESOURCE_PATH}/#{id}")
10
+ SamcartApi::SamcartObject.new(order)
10
11
  end
11
- end
12
-
13
- def initialize(id)
14
- @id = id
15
- end
16
-
17
- def find
18
- order = client.get("#{RESOURCE_PATH}/#{id}")
19
- SamcartApi::SamcartObject.new(order)
20
- end
21
-
22
- private
23
12
 
24
- attr_reader :id
13
+ def client
14
+ SamcartApi::Client.new
15
+ end
25
16
 
26
- def client
27
- @client ||= SamcartApi::Client.new
17
+ def all(filters: {}, limit: 100)
18
+ params = filters.merge(limit:)
19
+ SamcartApi::Paginator.new(client, RESOURCE_PATH, params)
20
+ end
28
21
  end
29
22
  end
30
23
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SamcartApi
4
+ class Paginator
5
+ def initialize(client, path, params)
6
+ @client = client
7
+ @next_url = "#{path}?#{params.to_query}"
8
+ @retry_attempts = 3
9
+ end
10
+
11
+ def next_page?
12
+ !@next_url.nil?
13
+ end
14
+
15
+ def next_page
16
+ return unless next_page?
17
+
18
+ response = client.get(@next_url)
19
+ @next_url = response.dig('pagination', 'next')
20
+ response['data']
21
+ end
22
+
23
+ def each_page
24
+ yield next_page while next_page?
25
+ end
26
+
27
+ attr_reader :client
28
+ end
29
+ end
@@ -6,25 +6,19 @@ module SamcartApi
6
6
 
7
7
  class << self
8
8
  def find(id)
9
- new(id).find
9
+ product = client.get("#{RESOURCE_PATH}/#{id}")
10
+ SamcartApi::SamcartObject.new(product)
10
11
  end
11
- end
12
-
13
- def initialize(id)
14
- @id = id
15
- end
16
12
 
17
- def find
18
- product = client.get("#{RESOURCE_PATH}/#{id}")
19
- SamcartApi::SamcartObject.new(product)
20
- end
21
-
22
- private
13
+ def client
14
+ SamcartApi::Client.new
15
+ end
23
16
 
24
- attr_reader :id
17
+ def all(filters: {}, limit: 100)
18
+ params = filters.merge(limit:)
25
19
 
26
- def client
27
- @client ||= SamcartApi::Client.new
20
+ SamcartApi::Paginator.new(client, RESOURCE_PATH, params)
21
+ end
28
22
  end
29
23
  end
30
24
  end
data/lib/samcart_api.rb CHANGED
@@ -18,6 +18,7 @@ module SamcartApi
18
18
  autoload :Order, 'samcart_api/order'
19
19
  autoload :Client, 'samcart_api/client'
20
20
  autoload :ApiRequest, 'samcart_api/api_request'
21
+ autoload :Paginator, 'samcart_api/paginator'
21
22
 
22
23
  class << self
23
24
  def configure
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: samcart_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Olumuyiwa Osiname
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-03-08 00:00:00.000000000 Z
11
+ date: 2025-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -120,6 +120,7 @@ files:
120
120
  - lib/samcart_api/api_request.rb
121
121
  - lib/samcart_api/client.rb
122
122
  - lib/samcart_api/order.rb
123
+ - lib/samcart_api/paginator.rb
123
124
  - lib/samcart_api/product.rb
124
125
  - lib/samcart_api/samcart_object.rb
125
126
  homepage: https://github.com/oluosiname/samcart_api
@@ -137,7 +138,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
137
138
  requirements:
138
139
  - - ">="
139
140
  - !ruby/object:Gem::Version
140
- version: 2.7.0
141
+ version: 3.3.0
141
142
  required_rubygems_version: !ruby/object:Gem::Requirement
142
143
  requirements:
143
144
  - - ">="