Class: PayPal::SDK::Core::Config

Inherits:
Object
  • Object
show all
Includes:
Exceptions, Logging
Defined in:
lib/paypal-sdk/core/config.rb

Overview

Config class is used to hold the configurations.

Examples

# To load configurations from file
Config.load('config/paypal.yml', 'development')

# Get configuration
Config.config   # load default configuration
Config.config(:development) # load development configuration
Config.config(:development, :app_id => "XYZ") # Override configuration

# Read configuration attributes
config = Config.config
config.username
config.endpoint

Constant Summary collapse

@@config_cache =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log_event, #logger

Constructor Details

#initialize(options) ⇒ Config

Create Config object

Options(Hash)

  • username – Username

  • password – Password

  • signature (Optional if certificate present) – Signature

  • app_id – Application ID

  • cert_path (Optional if signature present) – Certificate file path



100
101
102
# File 'lib/paypal-sdk/core/config.rb', line 100

def initialize(options)
  merge!(options)
end

Instance Attribute Details

#app_idObject

Returns the value of attribute app_id.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def app_id
  @app_id
end

#cert_pathObject

Returns the value of attribute cert_path.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def cert_path
  @cert_path
end

#client_idObject

Returns the value of attribute client_id.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def client_secret
  @client_secret
end

#device_ipaddressObject

Returns the value of attribute device_ipaddress.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def device_ipaddress
  @device_ipaddress
end

#endpointObject Also known as: end_point

Returns the value of attribute endpoint.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def endpoint
  @endpoint
end

#http_proxyObject

Returns the value of attribute http_proxy.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def http_proxy
  @http_proxy
end

#http_timeoutObject

Returns the value of attribute http_timeout.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def http_timeout
  @http_timeout
end

#ipn_endpointObject Also known as: ipn_end_point

Returns the value of attribute ipn_endpoint.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def ipn_endpoint
  @ipn_endpoint
end

#merchant_endpointObject Also known as: merchant_end_point

Returns the value of attribute merchant_endpoint.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def merchant_endpoint
  @merchant_endpoint
end

#modeObject

Returns the value of attribute mode.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def mode
  @mode
end

#openid_client_idObject

Returns the value of attribute openid_client_id.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def openid_client_id
  @openid_client_id
end

#openid_client_secretObject

Returns the value of attribute openid_client_secret.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def openid_client_secret
  @openid_client_secret
end

#openid_endpointObject

Returns the value of attribute openid_endpoint.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def openid_endpoint
  @openid_endpoint
end

#openid_redirect_uriObject

Returns the value of attribute openid_redirect_uri.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def openid_redirect_uri
  @openid_redirect_uri
end

#passwordObject

Returns the value of attribute password.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def password
  @password
end

#platform_endpointObject Also known as: platform_end_point

Returns the value of attribute platform_endpoint.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def platform_endpoint
  @platform_endpoint
end

#rest_endpointObject Also known as: rest_end_point

Returns the value of attribute rest_endpoint.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def rest_endpoint
  @rest_endpoint
end

#rest_token_endpointObject Also known as: rest_token_end_point

Returns the value of attribute rest_token_endpoint.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def rest_token_endpoint
  @rest_token_endpoint
end

#sandbox_email_addressObject

Returns the value of attribute sandbox_email_address.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def sandbox_email_address
  @sandbox_email_address
end

#signatureObject

Returns the value of attribute signature.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def signature
  @signature
end

#subjectObject

Returns the value of attribute subject.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def subject
  @subject
end

#tokenObject

Returns the value of attribute token.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def token
  @token
end

#token_secretObject

Returns the value of attribute token_secret.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def token_secret
  @token_secret
end

#usernameObject

Returns the value of attribute username.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def username
  @username
end

#verbose_loggingObject

Returns the value of attribute verbose_logging.



71
72
73
# File 'lib/paypal-sdk/core/config.rb', line 71

def verbose_logging
  @verbose_logging
end

Class Method Details

.config(env = default_environment, override_configuration = {}) ⇒ Object

Create or Load Config object based on given environment and configurations.

Attributes

  • env (Optional) – Environment name

  • override_configuration (Optional) – Override the configuration given in file.

Example

Config.config
Config.config(:development)
Config.config(:development, { :app_id => "XYZ" })


195
196
197
198
199
200
201
202
203
204
205
# File 'lib/paypal-sdk/core/config.rb', line 195

def config(env = default_environment, override_configuration = {})
  if env.is_a? Hash
    override_configuration = env
    env = default_environment
  end
  if override_configuration.nil? or override_configuration.empty?
    default_config(env)
  else
    default_config(env).dup.merge!(override_configuration)
  end
end

.configurationsObject

Get raw configurations in Hash format.



227
228
229
# File 'lib/paypal-sdk/core/config.rb', line 227

def configurations
  @@configurations ||= read_configurations
end

.configurations=(configs) ⇒ Object

Set configuration



232
233
234
235
# File 'lib/paypal-sdk/core/config.rb', line 232

def configurations=(configs)
  @@config_cache   = {}
  @@configurations = configs && Hash[configs.map{|k,v| [k.to_s, v] }]
end

.configure(options = {}, &block) ⇒ Object Also known as: set_config



176
177
178
179
180
181
182
183
184
# File 'lib/paypal-sdk/core/config.rb', line 176

def configure(options = {}, &block)
  begin
    self.config.merge!(options)
  rescue Errno::ENOENT
    self.configurations = { default_environment => options }
  end
  block.call(self.config) if block
  self.config
end

.default_config(env = nil) ⇒ Object



207
208
209
210
211
212
213
214
# File 'lib/paypal-sdk/core/config.rb', line 207

def default_config(env = nil)
  env = (env || default_environment).to_s
  if configurations[env]
    @@config_cache[env] ||= new(configurations[env])
  else
    raise Exceptions::MissingConfig.new("Configuration[#{env}] NotFound")
  end
end

.default_environmentObject

Get default environment name



167
168
169
# File 'lib/paypal-sdk/core/config.rb', line 167

def default_environment
  @@default_environment ||= ENV['PAYPAL_ENV'] || ENV['RACK_ENV'] || ENV['RAILS_ENV'] || "development"
end

.default_environment=(env) ⇒ Object

Set default environment



172
173
174
# File 'lib/paypal-sdk/core/config.rb', line 172

def default_environment=(env)
  @@default_environment = env.to_s
end

.load(file_name, default_env = default_environment) ⇒ Object

Load configurations from file

Arguments

  • file_name – Configuration file path

  • default_environment (Optional) – default environment configuration to load

Example

Config.load('config/paypal.yml', 'development')


159
160
161
162
163
164
# File 'lib/paypal-sdk/core/config.rb', line 159

def load(file_name, default_env = default_environment)
  @@config_cache        = {}
  @@configurations      = read_configurations(file_name)
  @@default_environment = default_env
  config
end

.loggerObject

Get logger



222
223
224
# File 'lib/paypal-sdk/core/config.rb', line 222

def logger
  Logging.logger
end

.logger=(logger) ⇒ Object

Set logger



217
218
219
# File 'lib/paypal-sdk/core/config.rb', line 217

def logger=(logger)
  Logging.logger = logger
end

Instance Method Details

#ca_file=(ca_file) ⇒ Object



125
126
127
128
# File 'lib/paypal-sdk/core/config.rb', line 125

def ca_file=(ca_file)
  logger.warn '`ca_file=` is deprecated, Please configure `ca_file=` under `ssl_options`'
  self.ssl_options = { :ca_file => ca_file }
end

#dev_central_url=(dev_central_url) ⇒ Object



112
113
114
# File 'lib/paypal-sdk/core/config.rb', line 112

def dev_central_url=(dev_central_url)
  logger.warn '`dev_central_url=` is deprecated.'
end

#http_verify_mode=(verify_mode) ⇒ Object



130
131
132
133
# File 'lib/paypal-sdk/core/config.rb', line 130

def http_verify_mode=(verify_mode)
  logger.warn '`http_verify_mode=` is deprecated, Please configure `verify_mode=` under `ssl_options`'
  self.ssl_options = { :verify_mode => verify_mode }
end

#logfile=(filename) ⇒ Object



104
105
106
# File 'lib/paypal-sdk/core/config.rb', line 104

def logfile=(filename)
  logger.warn '`logfile=` is deprecated, Please use `PayPal::SDK::Core::Config.logger = Logger.new(STDERR)`'
end

#merge!(options) ⇒ Object

Override configurations



136
137
138
139
140
141
# File 'lib/paypal-sdk/core/config.rb', line 136

def merge!(options)
  options.each do |key, value|
    send("#{key}=", value)
  end
  self
end

#redirect_url=(redirect_url) ⇒ Object



108
109
110
# File 'lib/paypal-sdk/core/config.rb', line 108

def redirect_url=(redirect_url)
  logger.warn '`redirect_url=` is deprecated.'
end

#required!(*names) ⇒ Object

Validate required configuration

Raises:



144
145
146
147
# File 'lib/paypal-sdk/core/config.rb', line 144

def required!(*names)
  names = names.select{|name| send(name).nil? }
  raise MissingConfig.new("Required configuration(#{names.join(", ")})") if names.any?
end

#ssl_optionsObject



116
117
118
# File 'lib/paypal-sdk/core/config.rb', line 116

def ssl_options
  @ssl_options ||= {}.freeze
end

#ssl_options=(options) ⇒ Object



120
121
122
123
# File 'lib/paypal-sdk/core/config.rb', line 120

def ssl_options=(options)
  options = Hash[options.map{|key, value| [key.to_sym, value] }]
  @ssl_options = ssl_options.merge(options).freeze
end