paystack_sdk 0.0.4 → 0.0.5
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/README.md +3 -0
- data/lib/paystack_sdk/client.rb +17 -13
- data/lib/paystack_sdk/resources/base.rb +3 -27
- data/lib/paystack_sdk/utils/connection_utils.rb +48 -0
- data/lib/paystack_sdk/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5dc8af5f5a8c1305ac52bb936f3601982204a1ccab20ee7d63b2b7ad89c03b3f
|
4
|
+
data.tar.gz: 30c6c8e89a107566d079ca22f78f56cbb6d48701db2d97f60ddb6357a2f5c5b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5f448b8d57b73b3837542791f56418b1711dc697b7551b1240eac3b86d5337d7a125b70d006654ef41a84e16b40da38ad5bb5178faf50fee12ac72ca9d8c7f1
|
7
|
+
data.tar.gz: 6c3d0d9eb5c06106e1fea26e7434034a42693925cae29a7cbf9288d5655c48598a91a80cc353b238dfdd9bb73be957d4f2e511fc54004260521aa8fedaf17cf3
|
data/README.md
CHANGED
@@ -78,6 +78,9 @@ end
|
|
78
78
|
# Initialize with your Paystack secret key
|
79
79
|
paystack = PaystackSdk::Client.new(secret_key: "sk_test_xxx")
|
80
80
|
|
81
|
+
# Or set the PAYSTACK_SECRET_KEY in your environment and do this instead
|
82
|
+
paystack = PaystackSdk::Client.new # => This will dynamically fetch the secret key
|
83
|
+
|
81
84
|
# You can access the connection directly if needed
|
82
85
|
connection = paystack.connection
|
83
86
|
```
|
data/lib/paystack_sdk/client.rb
CHANGED
@@ -1,32 +1,36 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative "resources/transactions"
|
4
|
+
require_relative "utils/connection_utils"
|
4
5
|
|
5
6
|
module PaystackSdk
|
6
7
|
# The `Client` class serves as the main entry point for interacting with the Paystack API.
|
7
8
|
# It initializes a connection to the Paystack API and provides access to various resources.
|
8
9
|
class Client
|
9
|
-
#
|
10
|
-
|
10
|
+
# Include connection utilities
|
11
|
+
include Utils::ConnectionUtils
|
11
12
|
|
12
13
|
# @return [Faraday::Connection] The Faraday connection object used for API requests
|
13
14
|
attr_reader :connection
|
14
15
|
|
15
16
|
# Initializes a new `Client` instance.
|
16
17
|
#
|
17
|
-
# @param
|
18
|
+
# @param connection [Faraday::Connection, nil] The Faraday connection object used for API requests.
|
19
|
+
# If nil, a new connection will be created using the default API key.
|
20
|
+
# @param secret_key [String, nil] Optional API key to use for creating a new connection.
|
21
|
+
# Only used if connection is nil.
|
18
22
|
#
|
19
|
-
# @example
|
23
|
+
# @example With an existing connection
|
24
|
+
# connection = Faraday.new(...)
|
25
|
+
# client = PaystackSdk::Client.new(connection)
|
26
|
+
#
|
27
|
+
# @example With an API key
|
20
28
|
# client = PaystackSdk::Client.new(secret_key: "sk_test_xxx")
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
conn.headers["Content-Type"] = "application/json"
|
27
|
-
conn.headers["User-Agent"] = "paystack_sdk/#{PaystackSdk::VERSION}"
|
28
|
-
conn.adapter Faraday.default_adapter
|
29
|
-
end
|
29
|
+
#
|
30
|
+
# @example With default connection (requires PAYSTACK_SECRET_KEY environment variable)
|
31
|
+
# client = PaystackSdk::Client.new
|
32
|
+
def initialize(connection = nil, secret_key: nil)
|
33
|
+
@connection = initialize_connection(connection, secret_key: secret_key)
|
30
34
|
end
|
31
35
|
|
32
36
|
# Provides access to the `Transactions` resource.
|
@@ -3,14 +3,15 @@
|
|
3
3
|
require_relative "../response"
|
4
4
|
require_relative "../client"
|
5
5
|
require_relative "../validations"
|
6
|
+
require_relative "../utils/connection_utils"
|
6
7
|
|
7
8
|
module PaystackSdk
|
8
9
|
module Resources
|
9
10
|
# The `Base` class serves as a parent class for all resource classes in the SDK.
|
10
11
|
# It provides shared functionality, such as handling API responses.
|
11
12
|
class Base
|
12
|
-
# Include validation methods
|
13
13
|
include PaystackSdk::Validations
|
14
|
+
include PaystackSdk::Utils::ConnectionUtils
|
14
15
|
|
15
16
|
# Initializes a new `Base` instance.
|
16
17
|
#
|
@@ -29,36 +30,11 @@ module PaystackSdk
|
|
29
30
|
# @example With default connection (requires PAYSTACK_SECRET_KEY environment variable)
|
30
31
|
# resource = PaystackSdk::Resources::SomeResource.new
|
31
32
|
def initialize(connection = nil, secret_key: nil)
|
32
|
-
@connection =
|
33
|
-
connection
|
34
|
-
elsif secret_key
|
35
|
-
create_connection(secret_key:)
|
36
|
-
else
|
37
|
-
# Try to get API key from environment variable
|
38
|
-
env_secret_key = ENV["PAYSTACK_SECRET_KEY"]
|
39
|
-
raise PaystackSdk::Error, "No connection or API key provided" unless env_secret_key
|
40
|
-
|
41
|
-
create_connection(secret_key: env_secret_key)
|
42
|
-
end
|
33
|
+
@connection = initialize_connection(connection, secret_key: secret_key)
|
43
34
|
end
|
44
35
|
|
45
36
|
private
|
46
37
|
|
47
|
-
# Creates a new Faraday connection with the Paystack API.
|
48
|
-
#
|
49
|
-
# @param secret_key [String] The secret API key for authenticating with the Paystack API.
|
50
|
-
# @return [Faraday::Connection] A configured Faraday connection.
|
51
|
-
def create_connection(secret_key:)
|
52
|
-
Faraday.new(url: PaystackSdk::Client::BASE_URL) do |conn|
|
53
|
-
conn.request :json
|
54
|
-
conn.response :json, content_type: /\bjson$/
|
55
|
-
conn.headers["Authorization"] = "Bearer #{secret_key}"
|
56
|
-
conn.headers["Content-Type"] = "application/json"
|
57
|
-
conn.headers["User-Agent"] = "paystack_sdk/#{PaystackSdk::VERSION}"
|
58
|
-
conn.adapter Faraday.default_adapter
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
38
|
# Handles the API response, wrapping it in a Response object.
|
63
39
|
#
|
64
40
|
# @param response [Faraday::Response] The response object returned by the Faraday connection.
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PaystackSdk
|
4
|
+
module Utils
|
5
|
+
# The `ConnectionUtils` module provides shared functionality for creating
|
6
|
+
# and initializing API connections. This is used by both the Client class
|
7
|
+
# and resource classes.
|
8
|
+
module ConnectionUtils
|
9
|
+
# The base URL for the Paystack API.
|
10
|
+
BASE_URL = "https://api.paystack.co"
|
11
|
+
|
12
|
+
# Initializes a connection based on the provided parameters.
|
13
|
+
#
|
14
|
+
# @param connection [Faraday::Connection, nil] An existing connection object.
|
15
|
+
# @param secret_key [String, nil] Optional API key to use for creating a new connection.
|
16
|
+
# @return [Faraday::Connection] A connection object for API requests.
|
17
|
+
# @raise [PaystackSdk::Error] If no connection or API key can be found.
|
18
|
+
def initialize_connection(connection = nil, secret_key: nil)
|
19
|
+
if connection
|
20
|
+
connection
|
21
|
+
elsif secret_key
|
22
|
+
create_connection(secret_key:)
|
23
|
+
else
|
24
|
+
# Try to get API key from environment variable
|
25
|
+
env_secret_key = ENV["PAYSTACK_SECRET_KEY"]
|
26
|
+
raise PaystackSdk::Error, "No connection or API key provided" unless env_secret_key
|
27
|
+
|
28
|
+
create_connection(secret_key: env_secret_key)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Creates a new Faraday connection with the Paystack API.
|
33
|
+
#
|
34
|
+
# @param secret_key [String] The secret API key for authenticating with the Paystack API.
|
35
|
+
# @return [Faraday::Connection] A configured Faraday connection.
|
36
|
+
def create_connection(secret_key:)
|
37
|
+
Faraday.new(url: BASE_URL) do |conn|
|
38
|
+
conn.request :json
|
39
|
+
conn.response :json, content_type: /\bjson$/
|
40
|
+
conn.headers["Authorization"] = "Bearer #{secret_key}"
|
41
|
+
conn.headers["Content-Type"] = "application/json"
|
42
|
+
conn.headers["User-Agent"] = "paystack_sdk/#{PaystackSdk::VERSION}"
|
43
|
+
conn.adapter Faraday.default_adapter
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/paystack_sdk/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paystack_sdk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maxwell Nana Forson (theLazyProgrammer)
|
@@ -119,6 +119,7 @@ files:
|
|
119
119
|
- lib/paystack_sdk/resources/base.rb
|
120
120
|
- lib/paystack_sdk/resources/transactions.rb
|
121
121
|
- lib/paystack_sdk/response.rb
|
122
|
+
- lib/paystack_sdk/utils/connection_utils.rb
|
122
123
|
- lib/paystack_sdk/validations.rb
|
123
124
|
- lib/paystack_sdk/version.rb
|
124
125
|
- mise.toml
|