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 +4 -4
- data/lib/samcart_api/api_request.rb +37 -4
- data/lib/samcart_api/client.rb +2 -2
- data/lib/samcart_api/order.rb +13 -16
- data/lib/samcart_api/paginator.rb +29 -0
- data/lib/samcart_api/product.rb +10 -16
- data/lib/samcart_api/samcart_object.rb +4 -1
- data/lib/samcart_api/version.rb +5 -0
- data/lib/samcart_api.rb +2 -1
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3b46630dcf4afb835065d0ac482989a60d029a88573138006787dbf1b1fa4fc2
|
4
|
+
data.tar.gz: 7104d2969a0f4e98263f6d84ee2e3d6f8b1208add3d45ce3d1a103ba4b0415cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d360a67d1037e3585ed0b26c4e41c890dc04cff07c98937efd60207bf847be9d087ab346cafd149013889af001f4e02e6a08c41947d1c450e9f7af7ebc77641c
|
7
|
+
data.tar.gz: 7d5b39108e8d9dbde7fb0d782342ea468196d432a9836f618b3dd0963635f2809c64cf30d0dc4c9a3ae21fa47d0545ae02b51d47494ce029d29b72ba68d7d242
|
@@ -1,16 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module
|
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 =
|
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
|
-
|
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:
|
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
|
data/lib/samcart_api/client.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module
|
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 ||
|
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
|
|
data/lib/samcart_api/order.rb
CHANGED
@@ -1,30 +1,27 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module
|
3
|
+
module SamcartAPI
|
4
4
|
class Order
|
5
5
|
RESOURCE_PATH = '/orders'
|
6
6
|
|
7
7
|
class << self
|
8
8
|
def find(id)
|
9
|
-
|
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
|
-
|
13
|
+
def client
|
14
|
+
SamcartAPI::Client.new
|
15
|
+
end
|
23
16
|
|
24
|
-
|
17
|
+
def all(filters: {}, limit: 100)
|
18
|
+
params = filters.merge(limit:)
|
19
|
+
SamcartAPI::Paginator.new(client, RESOURCE_PATH, params)
|
20
|
+
end
|
25
21
|
|
26
|
-
|
27
|
-
|
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
|
data/lib/samcart_api/product.rb
CHANGED
@@ -1,30 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module
|
3
|
+
module SamcartAPI
|
4
4
|
class Product
|
5
5
|
RESOURCE_PATH = '/products'
|
6
6
|
|
7
7
|
class << self
|
8
8
|
def find(id)
|
9
|
-
|
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
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
13
|
+
def client
|
14
|
+
SamcartAPI::Client.new
|
15
|
+
end
|
23
16
|
|
24
|
-
|
17
|
+
def all(filters: {}, limit: 100)
|
18
|
+
params = filters.merge(limit:)
|
25
19
|
|
26
|
-
|
27
|
-
|
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
|
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
|
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
|
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.
|
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-
|
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:
|
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
|
152
|
+
summary: Ruby wrapper for the SamCart API
|
151
153
|
test_files: []
|