class SuntechRails::Core::Config
Config
class is used to hold the configurations.
Examples¶ ↑
# To load configurations from file Config.load('config/suntech.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
Attributes
Public Class Methods
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" })
# File lib/suntech_rails/core/config.rb, line 152 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
Get raw configurations in Hash format.
# File lib/suntech_rails/core/config.rb, line 188 def configurations @@configurations ||= read_configurations end
Set configuration
# File lib/suntech_rails/core/config.rb, line 193 def configurations=(configs) @@config_cache = {} @@configurations = configs && Hash[configs.map{|k,v| [k.to_s, v] }] end
# File lib/suntech_rails/core/config.rb, line 134 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
# File lib/suntech_rails/core/config.rb, line 164 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
Get default environment name
# File lib/suntech_rails/core/config.rb, line 125 def default_environment @@default_environment ||= ENV['SUNTECH_ENV'] || ENV['RACK_ENV'] || ENV['RAILS_ENV'] || "development" end
Set default environment
# File lib/suntech_rails/core/config.rb, line 130 def default_environment=(env) @@default_environment = env.to_s end
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')
# File lib/suntech_rails/core/config.rb, line 117 def load(file_name, default_env = default_environment) @@config_cache = {} @@configurations = read_configurations(file_name) @@default_environment = default_env config end
Get logger
# File lib/suntech_rails/core/config.rb, line 179 def logger if @@configurations[:mode] == 'live' and Logging.logger.level == Logger::DEBUG Logging.logger.warn "DEBUG log level not allowed in sandbox mode for security of confidential information. Changing log level to INFO..." Logging.logger.level = Logger::INFO end Logging.logger end
Set logger
# File lib/suntech_rails/core/config.rb, line 174 def logger=(logger) Logging.logger = logger end
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
# File lib/suntech_rails/core/config.rb, line 80 def initialize(options) merge!(options) end
Private Class Methods
Read configurations from the given file name
Arguments¶ ↑
-
file_name
(Optional) –Configuration
file path
# File lib/suntech_rails/core/config.rb, line 202 def read_configurations(file_name = "config/suntech.yml") erb = ERB.new(File.read(file_name)) erb.filename = file_name YAML.load(erb.result) end
Public Instance Methods
Override configurations
# File lib/suntech_rails/core/config.rb, line 94 def merge!(options) options.each do |key, value| send("#{key}=", value) end self end
Validate required configuration
# File lib/suntech_rails/core/config.rb, line 102 def required!(*names) names = names.select{|name| send(name).nil? } raise MissingConfig.new("Required configuration(#{names.join(", ")})") if names.any? end
# File lib/suntech_rails/core/config.rb, line 84 def ssl_options @ssl_options ||= {}.freeze end
# File lib/suntech_rails/core/config.rb, line 88 def ssl_options=(options) options = Hash[options.map{|key, value| [key.to_sym, value] }] @ssl_options = ssl_options.merge(options).freeze end