Class: SuntechRails::Core::Config
- Inherits:
-
Object
- Object
- SuntechRails::Core::Config
- Includes:
- Exceptions, Logging
- Defined in:
- lib/suntech_rails/core/config.rb
Overview
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
Constant Summary collapse
- @@config_cache =
{}
Instance Attribute Summary collapse
-
#ali_pay_id ⇒ Object
Returns the value of attribute ali_pay_id.
-
#buy_safe_id ⇒ Object
Returns the value of attribute buy_safe_id.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#pay_code_id ⇒ Object
Returns the value of attribute pay_code_id.
-
#transaction_code ⇒ Object
Returns the value of attribute transaction_code.
-
#twentyfour_id ⇒ Object
Returns the value of attribute twentyfour_id.
-
#web_atm_id ⇒ Object
Returns the value of attribute web_atm_id.
Class Method Summary collapse
-
.config(env = default_environment, override_configuration = {}) ⇒ Object
Create or Load Config object based on given environment and configurations.
-
.configurations ⇒ Object
Get raw configurations in Hash format.
-
.configurations=(configs) ⇒ Object
Set configuration.
- .configure(options = {}, &block) ⇒ Object
- .default_config(env = nil) ⇒ Object
-
.default_environment ⇒ Object
Get default environment name.
-
.default_environment=(env) ⇒ Object
Set default environment.
-
.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’). -
.logger ⇒ Object
Get logger.
-
.logger=(logger) ⇒ Object
Set logger.
Instance Method Summary collapse
-
#initialize(options) ⇒ Config
constructor
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. -
#merge!(options) ⇒ Object
Override configurations.
-
#required!(*names) ⇒ Object
Validate required configuration.
- #ssl_options ⇒ Object
- #ssl_options=(options) ⇒ Object
Methods included from Logging
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
80 81 82 |
# File 'lib/suntech_rails/core/config.rb', line 80 def initialize() merge!() end |
Instance Attribute Details
#ali_pay_id ⇒ Object
Returns the value of attribute ali_pay_id.
70 71 72 |
# File 'lib/suntech_rails/core/config.rb', line 70 def ali_pay_id @ali_pay_id end |
#buy_safe_id ⇒ Object
Returns the value of attribute buy_safe_id.
70 71 72 |
# File 'lib/suntech_rails/core/config.rb', line 70 def buy_safe_id @buy_safe_id end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
70 71 72 |
# File 'lib/suntech_rails/core/config.rb', line 70 def endpoint @endpoint end |
#mode ⇒ Object
Returns the value of attribute mode.
70 71 72 |
# File 'lib/suntech_rails/core/config.rb', line 70 def mode @mode end |
#pay_code_id ⇒ Object
Returns the value of attribute pay_code_id.
70 71 72 |
# File 'lib/suntech_rails/core/config.rb', line 70 def pay_code_id @pay_code_id end |
#transaction_code ⇒ Object
Returns the value of attribute transaction_code.
70 71 72 |
# File 'lib/suntech_rails/core/config.rb', line 70 def transaction_code @transaction_code end |
#twentyfour_id ⇒ Object
Returns the value of attribute twentyfour_id.
70 71 72 |
# File 'lib/suntech_rails/core/config.rb', line 70 def twentyfour_id @twentyfour_id end |
#web_atm_id ⇒ Object
Returns the value of attribute web_atm_id.
70 71 72 |
# File 'lib/suntech_rails/core/config.rb', line 70 def web_atm_id @web_atm_id end |
Class Method Details
.config(env = default_environment, override_configuration = {}) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 |
# 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 |
.configurations ⇒ Object
Get raw configurations in Hash format.
188 189 190 |
# File 'lib/suntech_rails/core/config.rb', line 188 def configurations @@configurations ||= read_configurations end |
.configurations=(configs) ⇒ Object
Set configuration
193 194 195 196 |
# 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 |
.configure(options = {}, &block) ⇒ Object
134 135 136 137 138 139 140 141 142 |
# File 'lib/suntech_rails/core/config.rb', line 134 def configure( = {}, &block) begin self.config.merge!() rescue Errno::ENOENT self.configurations = { default_environment => } end block.call(self.config) if block self.config end |
.default_config(env = nil) ⇒ Object
164 165 166 167 168 169 170 171 |
# 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 |
.default_environment ⇒ Object
Get default environment name
125 126 127 |
# 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 |
.default_environment=(env) ⇒ Object
Set default environment
130 131 132 |
# File 'lib/suntech_rails/core/config.rb', line 130 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')
117 118 119 120 121 122 |
# 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 |
.logger ⇒ Object
Get logger
179 180 181 182 183 184 185 |
# 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 |
Instance Method Details
#merge!(options) ⇒ Object
Override configurations
94 95 96 97 98 99 |
# File 'lib/suntech_rails/core/config.rb', line 94 def merge!() .each do |key, value| send("#{key}=", value) end self end |
#required!(*names) ⇒ Object
Validate required configuration
102 103 104 105 |
# 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 |
#ssl_options ⇒ Object
84 85 86 |
# File 'lib/suntech_rails/core/config.rb', line 84 def @ssl_options ||= {}.freeze end |
#ssl_options=(options) ⇒ Object
88 89 90 91 |
# File 'lib/suntech_rails/core/config.rb', line 88 def () = Hash[.map{|key, value| [key.to_sym, value] }] @ssl_options = .merge().freeze end |