1# frozen_string_literal: true
 2
 3require "braintree"
 4
 5# Braintree is not async, so wrap in EM.defer for now
 6class AsyncBraintree
 7	def initialize(environment:, merchant_id:, public_key:, private_key:, **)
 8		@gateway = Braintree::Gateway.new(
 9			environment: environment,
10			merchant_id: merchant_id,
11			public_key: public_key,
12			private_key: private_key
13		)
14		@gateway.config.logger = LOG
15	end
16
17	def respond_to_missing?(m, *)
18		@gateway.respond_to?(m) || super
19	end
20
21	def method_missing(m, *args)
22		return super unless respond_to_missing?(m, *args)
23
24		EM.promise_defer(klass: PromiseChain) do
25			@gateway.public_send(m, *args)
26		end
27	end
28
29	class PromiseChain < EMPromise
30		def respond_to_missing?(*)
31			false && super # We don't actually know what we respond to...
32		end
33
34		def method_missing(m, *args)
35			return super if respond_to_missing?(m, *args)
36
37			self.then { |o| o.public_send(m, *args) }
38		end
39	end
40end