module MangoPay

Holds top-level configuration.

Constants

LOG
MAIN_API_URL
SANDBOX_API_URL
VERSION

Public Class Methods

configuration() click to toggle source

Provides MangoPay configuration object for current environment.

noinspection RubyResolve

# File lib/mangopay.rb, line 100
def configuration
  env = environment
  env.configuration ||
    raise("MangoPay.configure() was not called for environment #{env.id}")
end
configure() { |config| ... } click to toggle source

Allows configuration of the current MangoPay environment. Yields configuration object to a provided block.

noinspection RubyResolve

# File lib/mangopay.rb, line 110
def configure
  env = environment
  LOG.info 'Configuring environment :{}', env.id
  config = env.configuration || MangoPay::Configuration.new
  yield config
  validate config
  LOG.info 'Successfully configured environment :{} for client {}',
           env.id, config.client_id
  env.configuration = config
end
environment() click to toggle source

Returns the environment currently being used.

noinspection RubyResolve

# File lib/mangopay.rb, line 52
def environment
  @environments[thread_local_env_id]
end
use_default() click to toggle source

Sets MangoPay to run under the default environment.

# File lib/mangopay.rb, line 45
def use_default
  use_environment :default
end
use_environment(env_id) click to toggle source

Allows to specify environment in which to run MangoPay operations. All subsequent threads will automatically run in that environment.

@param env_id ID of the required environment

noinspection RubyResolve

# File lib/mangopay.rb, line 33
def use_environment(env_id)
  @global_env_id = env_id || :default
  unless @environments[@global_env_id]
    LOG.debug 'Creating new environment :{}', env_id
    env = MangoPay::Environment.new(@global_env_id)
    @environments[@global_env_id] = env
  end
  LOG.info 'Using environment :{}', env_id
  put_thread_local_environment
end

Private Class Methods

initiate_mapping_removal(thread) click to toggle source

Starts another thread which will delete current thread's environment hash entry as soon as current thread terminates.

noinspection RubyResolve

# File lib/mangopay.rb, line 83
def initiate_mapping_removal(thread)
  thread_id = thread.object_id
  LOG.debug 'Initiating removal task for thread-to-environment mapping '\
              '{ {} => :{} }', thread_id, @global_env_id
  Thread.new do
    thread.join
    deleted = @env_id.delete thread_id
    LOG.debug 'Removed thread-to-environment mapping '\
                '{ {} => :{} }', thread_id, deleted
  end
end
put_thread_local_environment() click to toggle source

Saves currently-set global environment as the one to be used for current thread.

noinspection RubyResolve

# File lib/mangopay.rb, line 70
def put_thread_local_environment
  thread = Thread.current
  thread_id = thread.object_id
  LOG.debug 'Mapping thread {} to environment :{}',
            thread_id, @global_env_id
  initiate_mapping_removal(thread) unless @env_id[thread_id]
  @env_id[thread_id] = @global_env_id
end
thread_local_env_id() click to toggle source

Returns the ID of the environment to be used for current thread.

noinspection RubyResolve

# File lib/mangopay.rb, line 61
def thread_local_env_id
  thread_id = Thread.current.object_id
  @env_id[thread_id] || put_thread_local_environment
end
validate(config) click to toggle source

Validates a provided MangoPay configuration.

# File lib/mangopay.rb, line 124
def validate(config)
  cause_message = 'You must specify your client ID'
  raise cause_message unless config.client_id
  cause_message = 'You must specify your client APIKey'
  raise cause_message unless config.client_apiKey
  config.root_url = config.preproduction? ? SANDBOX_API_URL : MAIN_API_URL\
                      unless config.root_url
end