async_braintree.rb

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