Module: PaystackSdk::Utils::ConnectionUtils
- Included in:
- Client, Resources::Base
- Defined in:
- lib/paystack_sdk/utils/connection_utils.rb
Overview
The ‘ConnectionUtils` module provides shared functionality for creating and initializing API connections. This is used by both the Client class and resource classes.
Constant Summary collapse
- BASE_URL =
The base URL for the Paystack API.
"https://api.paystack.co"
Instance Method Summary collapse
-
#create_connection(secret_key:) ⇒ Faraday::Connection
Creates a new Faraday connection with the Paystack API.
-
#initialize_connection(connection = nil, secret_key: nil) ⇒ Faraday::Connection
Initializes a connection based on the provided parameters.
Instance Method Details
#create_connection(secret_key:) ⇒ Faraday::Connection
Creates a new Faraday connection with the Paystack API.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/paystack_sdk/utils/connection_utils.rb', line 36 def create_connection(secret_key:) Faraday.new(url: BASE_URL) do |conn| conn.request :json conn.response :json, content_type: /\bjson$/ conn.headers["Authorization"] = "Bearer #{secret_key}" conn.headers["Content-Type"] = "application/json" conn.headers["User-Agent"] = "paystack_sdk/#{PaystackSdk::VERSION}" conn.adapter Faraday.default_adapter end end |
#initialize_connection(connection = nil, secret_key: nil) ⇒ Faraday::Connection
Initializes a connection based on the provided parameters.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/paystack_sdk/utils/connection_utils.rb', line 18 def initialize_connection(connection = nil, secret_key: nil) if connection connection elsif secret_key create_connection(secret_key:) else # Try to get API key from environment variable env_secret_key = ENV["PAYSTACK_SECRET_KEY"] raise AuthenticationError, "No connection or API key provided" unless env_secret_key create_connection(secret_key: env_secret_key) end end |