Skip to content

Instantly share code, notes, and snippets.

@JuPlutonic
Last active April 27, 2020 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JuPlutonic/b0e34c5ec92f82e57661c71828f12f1d to your computer and use it in GitHub Desktop.
Save JuPlutonic/b0e34c5ec92f82e57661c71828f12f1d to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
# require 'singleton'... A pattern that restricts the
# instantiation of a class to one object.
# ...when you need an object specifically to hold state
# ...to make sure that each of these services are always
# wrapped in your own interface
# #new constructor condemded
require 'singleton'
module AppName
module PaymentGatewayName
# Realization of Singleton pattern
class Configuration
include Singleton
attr_accessor :base_uri, :subscription_id,
:merchant_pin, :server_host, :account_id
end
end
end
module AppName
module PaymentGatewayName
# Our API
module API
# how the library will get configured
def self.configure
yield(configuration) if block_given?
end
# Configuration.instance - this is only one method to gain access
def self.configuration
Configuration.instance
end
# https://github.com/lostisland/faraday
# https://stackoverflow.com/a/16548003
# https://gist.github.com/mislav/938183
# https://www.mobomo.com/2012/03/faraday-one-http-client-to-rule-them-all/
# https://github.com/lostisland/faraday_middleware
#
# Definiition of the "connection" method that use our Configuration singleton
def self.connection
url = configuration.base_url
@connection ||= Faraday.new(url) do |conn|
conn.request :url_encoded
conn.response :ox
conn.adapter Faraday.default_adapter
end
end
end
end
end
AppName::PaymentGatewayName::API.configure do |config|
config.account_id = ENV.fetch('PAYMENTGATEWAY_ACCOUNT_ID')
config.base_uri = ENV.fetch('PAYMENTGATEWAY_BASE_URI')
config.merchant_pin = ENV.fetch('PAYMENTGATEWAY_MERCHANT_PIN')
config.server_host = ENV.fetch('PAYMENTGATEWAY_SERVER_HOST')
config.subscription_id = ENV.fetch('PAYMENTGATEWAY_SUBSCRIPTION_ID')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment