samcart_api 0.1.2 → 1.0.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: 3b46630dcf4afb835065d0ac482989a60d029a88573138006787dbf1b1fa4fc2
4
+ data.tar.gz: 7104d2969a0f4e98263f6d84ee2e3d6f8b1208add3d45ce3d1a103ba4b0415cb
5
5
  SHA512:
6
- metadata.gz: '08ee150d36a9cf379a8f76e96ea40e087ff83e27d8946ff069fc3cc18f3f9bec64fe43b7f514e94334ee36ad248ffba1d22892c8e97ef9de2b2a613af651e524'
7
- data.tar.gz: e2d035a48f85abb24d477881761334ca9cd8a277eacf334e1125d1596076ceea0d87e8cb1885f680fa3ac86a5c1be4ce5267395851861167b94cfcfa5a66b5ad
6
+ metadata.gz: d360a67d1037e3585ed0b26c4e41c890dc04cff07c98937efd60207bf847be9d087ab346cafd149013889af001f4e02e6a08c41947d1c450e9f7af7ebc77641c
7
+ data.tar.gz: 7d5b39108e8d9dbde7fb0d782342ea468196d432a9836f618b3dd0963635f2809c64cf30d0dc4c9a3ae21fa47d0545ae02b51d47494ce029d29b72ba68d7d242
@@ -1,16 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module SamcartApi
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
8
10
  @params = params
9
11
  @api_key = api_key
10
- @version = SamcartApi.configuration.version
12
+ @version = SamcartAPI.configuration.version
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,10 +30,33 @@ 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
- @connection ||= Faraday.new(url: SamcartApi.configuration.api_url) do |conn|
59
+ @connection ||= Faraday.new(url: SamcartAPI.configuration.api_url) do |conn|
29
60
  conn.response :json
30
61
  conn.adapter Faraday.default_adapter
31
62
  end
@@ -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
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module SamcartApi
3
+ module SamcartAPI
4
4
  # Client for interacting with the SamCart API
5
5
  class Client
6
6
  def initialize(api_key = nil)
7
- @api_key = api_key || SamcartApi.configuration.api_key
7
+ @api_key = api_key || SamcartAPI.configuration.api_key
8
8
  raise ConfigurationError, 'API key is required' unless @api_key
9
9
  end
10
10
 
@@ -1,30 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module SamcartApi
3
+ module SamcartAPI
4
4
  class Order
5
5
  RESOURCE_PATH = '/orders'
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
12
 
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:)
19
+ SamcartAPI::Paginator.new(client, RESOURCE_PATH, params)
20
+ end
25
21
 
26
- def client
27
- @client ||= SamcartApi::Client.new
22
+ def charges(order_id)
23
+ client.get("#{RESOURCE_PATH}/#{order_id}/charges")
24
+ end
28
25
  end
29
26
  end
30
27
  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
@@ -1,30 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module SamcartApi
3
+ module SamcartAPI
4
4
  class Product
5
5
  RESOURCE_PATH = '/products'
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
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module SamcartApi
3
+ module SamcartAPI
4
4
  class SamcartObject
5
5
  def initialize(attributes = {})
6
6
  @attributes = attributes
@@ -12,6 +12,9 @@ module SamcartApi
12
12
 
13
13
  def method_missing(method_name, *args)
14
14
  if @attributes.key?(method_name.to_s)
15
+
16
+ warn '[DEPRECATION] Dot notation (.) is deprecated and ' \
17
+ "will be removed in the next major version. Use hash access ['#{method_name}'] instead."
15
18
  @attributes[method_name.to_s]
16
19
  else
17
20
  super
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SamcartAPI
4
+ VERSION = '1.0.0'
5
+ end
data/lib/samcart_api.rb CHANGED
@@ -5,7 +5,7 @@ require 'json'
5
5
  require 'active_support'
6
6
  require 'active_support/core_ext'
7
7
 
8
- module SamcartApi
8
+ module SamcartAPI
9
9
  VERSION = '0.1.0'
10
10
 
11
11
  class Error < StandardError; end
@@ -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: 1.0.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-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -120,8 +120,10 @@ 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
126
+ - lib/samcart_api/version.rb
125
127
  homepage: https://github.com/oluosiname/samcart_api
126
128
  licenses:
127
129
  - MIT
@@ -137,7 +139,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
137
139
  requirements:
138
140
  - - ">="
139
141
  - !ruby/object:Gem::Version
140
- version: 2.7.0
142
+ version: 3.3.0
141
143
  required_rubygems_version: !ruby/object:Gem::Requirement
142
144
  requirements:
143
145
  - - ">="
@@ -147,5 +149,5 @@ requirements: []
147
149
  rubygems_version: 3.5.3
148
150
  signing_key:
149
151
  specification_version: 4
150
- summary: Ruby gem for interacting with the SamCart API
152
+ summary: Ruby wrapper for the SamCart API
151
153
  test_files: []