samcart_api 0.1.2
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 +7 -0
- data/lib/samcart_api/api_request.rb +49 -0
- data/lib/samcart_api/client.rb +41 -0
- data/lib/samcart_api/order.rb +30 -0
- data/lib/samcart_api/product.rb +30 -0
- data/lib/samcart_api/samcart_object.rb +29 -0
- data/lib/samcart_api.rb +40 -0
- metadata +151 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 06fe1b45c2e3f892bd12685da744ad03e5f2b347714c2f5b0f142aaf6cd16a8d
|
4
|
+
data.tar.gz: 922fbf5b295f2edbb889128f2a68108c9093516deafd711677d6524be34c966e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '08ee150d36a9cf379a8f76e96ea40e087ff83e27d8946ff069fc3cc18f3f9bec64fe43b7f514e94334ee36ad248ffba1d22892c8e97ef9de2b2a613af651e524'
|
7
|
+
data.tar.gz: e2d035a48f85abb24d477881761334ca9cd8a277eacf334e1125d1596076ceea0d87e8cb1885f680fa3ac86a5c1be4ce5267395851861167b94cfcfa5a66b5ad
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SamcartApi
|
4
|
+
class ApiRequest
|
5
|
+
def initialize(method, path, params, api_key)
|
6
|
+
@method = method
|
7
|
+
@path = path
|
8
|
+
@params = params
|
9
|
+
@api_key = api_key
|
10
|
+
@version = SamcartApi.configuration.version
|
11
|
+
end
|
12
|
+
|
13
|
+
def perform
|
14
|
+
response = connection.send(@method) do |req|
|
15
|
+
req.url "/#{@version}#{@path}"
|
16
|
+
req.params = @params if @params
|
17
|
+
req.headers['Accept'] = 'application/json'
|
18
|
+
req.headers['sc-api'] = @api_key
|
19
|
+
req.headers['Content-Type'] = 'application/json'
|
20
|
+
end
|
21
|
+
|
22
|
+
handle_response(response)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def connection
|
28
|
+
@connection ||= Faraday.new(url: SamcartApi.configuration.api_url) do |conn|
|
29
|
+
conn.response :json
|
30
|
+
conn.adapter Faraday.default_adapter
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def handle_response(response)
|
35
|
+
case response.status
|
36
|
+
when 200..299
|
37
|
+
response.body
|
38
|
+
when 401
|
39
|
+
raise AuthenticationError, 'Invalid API key'
|
40
|
+
when 403
|
41
|
+
raise AuthenticationError, 'Access denied'
|
42
|
+
when 404
|
43
|
+
raise ApiError, 'Resource not found'
|
44
|
+
else
|
45
|
+
raise ApiError, "API request failed: #{response.body["message"] || response.body}"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SamcartApi
|
4
|
+
# Client for interacting with the SamCart API
|
5
|
+
class Client
|
6
|
+
def initialize(api_key = nil)
|
7
|
+
@api_key = api_key || SamcartApi.configuration.api_key
|
8
|
+
raise ConfigurationError, 'API key is required' unless @api_key
|
9
|
+
end
|
10
|
+
|
11
|
+
def products
|
12
|
+
@products ||= Product.new(self)
|
13
|
+
end
|
14
|
+
|
15
|
+
def orders
|
16
|
+
@orders ||= Order.new(self)
|
17
|
+
end
|
18
|
+
|
19
|
+
def get(path, params = {})
|
20
|
+
request(:get, path, params)
|
21
|
+
end
|
22
|
+
|
23
|
+
def post(path, body = {})
|
24
|
+
request(:post, path, body)
|
25
|
+
end
|
26
|
+
|
27
|
+
def put(path, body = {})
|
28
|
+
request(:put, path, body)
|
29
|
+
end
|
30
|
+
|
31
|
+
def delete(path)
|
32
|
+
request(:delete, path)
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def request(method, path, params = nil)
|
38
|
+
ApiRequest.new(method, path, params, @api_key).perform
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SamcartApi
|
4
|
+
class Order
|
5
|
+
RESOURCE_PATH = '/orders'
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def find(id)
|
9
|
+
new(id).find
|
10
|
+
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
|
+
|
24
|
+
attr_reader :id
|
25
|
+
|
26
|
+
def client
|
27
|
+
@client ||= SamcartApi::Client.new
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SamcartApi
|
4
|
+
class Product
|
5
|
+
RESOURCE_PATH = '/products'
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def find(id)
|
9
|
+
new(id).find
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(id)
|
14
|
+
@id = id
|
15
|
+
end
|
16
|
+
|
17
|
+
def find
|
18
|
+
product = client.get("#{RESOURCE_PATH}/#{id}")
|
19
|
+
SamcartApi::SamcartObject.new(product)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
attr_reader :id
|
25
|
+
|
26
|
+
def client
|
27
|
+
@client ||= SamcartApi::Client.new
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SamcartApi
|
4
|
+
class SamcartObject
|
5
|
+
def initialize(attributes = {})
|
6
|
+
@attributes = attributes
|
7
|
+
end
|
8
|
+
|
9
|
+
def [](key)
|
10
|
+
@attributes[key]
|
11
|
+
end
|
12
|
+
|
13
|
+
def method_missing(method_name, *args)
|
14
|
+
if @attributes.key?(method_name.to_s)
|
15
|
+
@attributes[method_name.to_s]
|
16
|
+
else
|
17
|
+
super
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def respond_to_missing?(method_name, include_private = false)
|
22
|
+
@attributes.key?(method_name.to_s) || super
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_h
|
26
|
+
@attributes
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/samcart_api.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
require 'json'
|
5
|
+
require 'active_support'
|
6
|
+
require 'active_support/core_ext'
|
7
|
+
|
8
|
+
module SamcartApi
|
9
|
+
VERSION = '0.1.0'
|
10
|
+
|
11
|
+
class Error < StandardError; end
|
12
|
+
class ConfigurationError < Error; end
|
13
|
+
class AuthenticationError < Error; end
|
14
|
+
class ApiError < Error; end
|
15
|
+
|
16
|
+
autoload :SamcartObject, 'samcart_api/samcart_object'
|
17
|
+
autoload :Product, 'samcart_api/product'
|
18
|
+
autoload :Order, 'samcart_api/order'
|
19
|
+
autoload :Client, 'samcart_api/client'
|
20
|
+
autoload :ApiRequest, 'samcart_api/api_request'
|
21
|
+
|
22
|
+
class << self
|
23
|
+
def configure
|
24
|
+
yield(configuration)
|
25
|
+
end
|
26
|
+
|
27
|
+
def configuration
|
28
|
+
@configuration ||= Configuration.new
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Configuration
|
33
|
+
attr_accessor :api_key, :api_url, :version
|
34
|
+
|
35
|
+
def initialize
|
36
|
+
@api_url = 'https://api.samcart.com'
|
37
|
+
@version = 'v1'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: samcart_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Olumuyiwa Osiname
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-03-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: faraday
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: json
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '13.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '13.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: vcr
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '6.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '6.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: webmock
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.0'
|
111
|
+
description: A Ruby gem that provides a simple interface for interacting with the
|
112
|
+
SamCart API, focusing on products and orders.
|
113
|
+
email:
|
114
|
+
- osiname@gmail.com
|
115
|
+
executables: []
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- lib/samcart_api.rb
|
120
|
+
- lib/samcart_api/api_request.rb
|
121
|
+
- lib/samcart_api/client.rb
|
122
|
+
- lib/samcart_api/order.rb
|
123
|
+
- lib/samcart_api/product.rb
|
124
|
+
- lib/samcart_api/samcart_object.rb
|
125
|
+
homepage: https://github.com/oluosiname/samcart_api
|
126
|
+
licenses:
|
127
|
+
- MIT
|
128
|
+
metadata:
|
129
|
+
homepage_uri: https://github.com/oluosiname/samcart_api
|
130
|
+
source_code_uri: https://github.com/oluosiname/samcart_api
|
131
|
+
changelog_uri: https://github.com/oluosiname/samcart_api/blob/main/CHANGELOG.md
|
132
|
+
post_install_message:
|
133
|
+
rdoc_options: []
|
134
|
+
require_paths:
|
135
|
+
- lib
|
136
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: 2.7.0
|
141
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
requirements: []
|
147
|
+
rubygems_version: 3.5.3
|
148
|
+
signing_key:
|
149
|
+
specification_version: 4
|
150
|
+
summary: Ruby gem for interacting with the SamCart API
|
151
|
+
test_files: []
|