Class: Omnipay::Gateways

Inherits:
Object
  • Object
show all
Defined in:
lib/omnipay/gateways.rb

Overview

Structure responsible for storing and accessing the application’s configured gateways

Instance Method Summary collapse

Constructor Details

#initializeGateways

Returns a new instance of Gateways.



6
7
8
9
# File 'lib/omnipay/gateways.rb', line 6

def initialize
  @gateways = {}
  @dynamic_configs = [] # Collection of procs which given a uid may or may not return a gateway config hash
end

Instance Method Details

#find(uid) ⇒ Gateway

Find a static gateway or instanciate a dynamic gateway for the given uid

Parameters:

  • uid (String)

    the gateway’s uid

Returns:

  • (Gateway)

    the corresponding gateway, or nil if none



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/omnipay/gateways.rb', line 37

def find(uid)
  gateway = @gateways[uid]
  return gateway if gateway

  # Tries to find a dynamic gateway config for this uid
  config = @dynamic_configs.find do |dynamic_config|
    gateway_config = dynamic_config.call(uid)
    if gateway_config
      return Gateway.new(gateway_config.merge(:uid => uid))
    end
  end

  # Nothing found : return nil
  nil
end

#push(opts = {}, &block) ⇒ Object

Add a new gateway, static or dynamic

Can be initialized via a config hash, or a block.

If initialized with a hash, it must contains the ‘:uid` and `:adapter` keys

If initialized with a block, the block must take the uid as an argument, and return a config hash with an ‘:adapter` key

Parameters:

  • opts (Hash) (defaults to: {})

    the gateway configuration, if static.

  • block (Proc)

    the gateway configuration, if dynamic.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/omnipay/gateways.rb', line 21

def push(opts = {}, &block)
  if block_given?
    @dynamic_configs << Proc.new(&block)

  elsif opts[:uid] && opts[:adapter]
    @gateways[opts[:uid]] = Gateway.new(opts)

  else
    raise ArgumentError.new('An omnipay gateway must be given an uid and an adapter, or a dynamic block')
  end
end