Ruby

Ruby code posted
created at 15 Oct 11:44, updated at 20 Oct 08:41

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class GatewayPicker
  def self.run(context)
    if transaction.cc_type == AMERICAN_EXPRESS
      AmericanExpressGatewayPicker.execute(context)
    elseif transaction.cc_type == DISCOVER
      DiscoverGatewayPicker.execute(context)
    ...
    end
  end
end

class AmericanExpressGatewayPicker
  def self.run(context)
    context.gateway = GATEWAYS_FOR_AMEX.random { |gw| gw.can_process_amount(context.amount) }
  end
end

class DiscoverGatewayPicker
  def self.run(context)
    if context.gateway == DEPRECATED_GW_1
      DeprecatedGateway1Picker.execute(context)
    else
      context.gateway = GATEWAYS_FOR_DISCOVER.random { |gw| gw.can_process_amount(context.amount) }
    end
  end
end

(a lot more service classes)

outcome = GatewayPicker.execute(transaction)
gateway = outcome.gateway
gateway.process(transaction)
854 Bytes in 5 ms with coderay