Class: ConfigManager
- Inherits:
-
Object
- Object
- ConfigManager
- Defined in:
- lib/config_manager.rb
Instance Attribute Summary collapse
-
#current ⇒ Object
readonly
Returns the value of attribute current.
-
#current_account ⇒ Object
readonly
Returns the value of attribute current_account.
-
#current_merchant_account ⇒ Object
Returns the value of attribute current_merchant_account.
-
#masked ⇒ Object
Returns the value of attribute masked.
Instance Method Summary collapse
- #activate!(name) ⇒ Object
- #activate_first! ⇒ Object
- #add(name, braintree_account_args) ⇒ Object
- #as_json ⇒ Object
- #has_config?(name) ⇒ Boolean
-
#initialize ⇒ ConfigManager
constructor
A new instance of ConfigManager.
- #test_environment!(name) ⇒ Object
- #valid? ⇒ Boolean
- #validate_environment! ⇒ Object
Constructor Details
#initialize ⇒ ConfigManager
Returns a new instance of ConfigManager.
11 12 13 14 15 16 17 |
# File 'lib/config_manager.rb', line 11 def initialize @masked = false @configs = {}.with_indifferent_access @current = nil @current_account = nil @current_merchant_account = nil end |
Instance Attribute Details
#current ⇒ Object (readonly)
Returns the value of attribute current.
8 9 10 |
# File 'lib/config_manager.rb', line 8 def current @current end |
#current_account ⇒ Object (readonly)
Returns the value of attribute current_account.
8 9 10 |
# File 'lib/config_manager.rb', line 8 def current_account @current_account end |
#current_merchant_account ⇒ Object
Returns the value of attribute current_merchant_account.
8 9 10 |
# File 'lib/config_manager.rb', line 8 def current_merchant_account @current_merchant_account end |
#masked ⇒ Object
Returns the value of attribute masked.
9 10 11 |
# File 'lib/config_manager.rb', line 9 def masked @masked end |
Instance Method Details
#activate!(name) ⇒ Object
45 46 47 48 49 |
# File 'lib/config_manager.rb', line 45 def activate!(name) @configs.fetch(name).activate! @current = name @current_account = @configs.fetch(name) end |
#activate_first! ⇒ Object
39 40 41 42 43 |
# File 'lib/config_manager.rb', line 39 def activate_first! raise "No accounts" unless valid? name, braintree_account = @configs.first activate!(name) end |
#add(name, braintree_account_args) ⇒ Object
30 31 32 33 |
# File 'lib/config_manager.rb', line 30 def add(name, braintree_account_args) @configs[name] = BraintreeAccount.new(braintree_account_args.with_indifferent_access) @configs[name].masked = @masked end |
#as_json ⇒ Object
19 20 21 22 23 24 |
# File 'lib/config_manager.rb', line 19 def as_json @configs.inject({}) do |json, (name, config)| json[name] = config.as_json json end end |
#has_config?(name) ⇒ Boolean
26 27 28 |
# File 'lib/config_manager.rb', line 26 def has_config?(name) @configs.has_key?(name) end |
#test_environment!(name) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/config_manager.rb', line 51 def test_environment!(name) original_config = current activate!(name) rescue Exception => e @configs.delete(name) raise e ensure activate!(original_config) end |
#valid? ⇒ Boolean
35 36 37 |
# File 'lib/config_manager.rb', line 35 def valid? @configs.any? end |
#validate_environment! ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/config_manager.rb', line 61 def validate_environment! old = ::OpenSSL::SSL::VERIFY_PEER silence_warnings{ ::OpenSSL::SSL.const_set :VERIFY_PEER, OpenSSL::SSL::VERIFY_NONE } puts "--- [#{current}] Getting client token for validation of environment" begin client_token = JSON.parse(Braintree::ClientToken.generate) rescue Errno::ECONNRESET => e return "The gateway is down for URL #{Braintree::Configuration.instantiate.base_merchant_url}" rescue Braintree::AuthenticationError => e return "Unable to authenticate to Braintree while getting client token. Your keys or merchant ID may be wrong or the gateway is down." end if client_token["paypal"].nil? == false puts "--- [#{current}] Trying to connect to paypal" begin Timeout::timeout(5) { open("#{client_token["paypal"]["baseUrl"]}/paypal") } rescue OpenURI::HTTPError => e if e. != "401 Unauthorized" return "Error opening #{"#{client_token["paypal"]["baseUrl"]}/paypal"}: #{e.}" end rescue Errno::ECONNREFUSED => e return "Can't connect to paypal base url #{client_token["paypal"]["baseUrl"]}" end end "Valid" rescue Timeout::Error => e "Timed out connecting to paypal at #{"#{client_token["paypal"]["baseUrl"]}/paypal"}" ensure silence_warnings{ ::OpenSSL::SSL.const_set :VERIFY_PEER, old } end |