~singpolyma/sgx-jmp

sgx-jmp/lib/alt_top_up_form.rb -rw-r--r-- 1.6 KiB
5bdeea51root21 Reserve TN with bandwith after selection and pass reservation id when ordering 3 hours ago
                                                                                
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# frozen_string_literal: true

require_relative "simple_swap"

class AltTopUpForm
	def self.for(customer)
		customer.btc_addresses.then do |addrs|
			AltTopUpForm.new(customer, addrs)
		end
	end

	def initialize(customer, btc_addresses)
		@customer = customer
		@balance = customer.balance
		@currency = customer.currency
		@btc_addresses = btc_addresses
	end

	def form
		FormTemplate.render(
			"alt_top_up",
			balance: @balance,
			currency: @currency,
			btc_addresses: @btc_addresses
		)
	end

	def parse(form)
		action =
			form.field("http://jabber.org/protocol/commands#actions")&.value.to_s
		case action
		when "BTC"
			BitcoinAddress.new(@customer)
		when /\A[A-Z]{3}\Z/
			SimpleSwapAddress.new(@customer, action, @btc_addresses.first)
		else
			NoOp.new
		end
	end

	class NoOp
		def action(*); end
	end

	class BitcoinAddress
		def initialize(customer)
			@customer = customer
		end

		def action(reply)
			@customer.add_btc_address.then do |addr|
				reply.command << FormTemplate.render(
					"alt_top_up/btc",
					btc_addresses: [addr]
				)
			end
		end
	end

	class SimpleSwapAddress
		def initialize(customer, currency, btc_address, simple_swap: SimpleSwap.new)
			@customer = customer
			@currency = currency.downcase
			@btc_address = btc_address
			@simple_swap = simple_swap
		end

		def btc_address
			@btc_address || @customer.add_btc_address
		end

		def action(reply)
			EMPromise.resolve(btc_address).then { |btc|
				@simple_swap.fetch_addr(@currency, btc)
			}.then do |addr|
				reply.command << FormTemplate.render(
					"alt_top_up/simpleswap",
					addresses: [addr]
				)
			end
		end
	end
end