api.rb

 1# frozen_string_literal: true
 2
 3class API
 4	def self.for(customer)
 5		EMPromise.all([
 6			sgx_jmp?(customer),
 7			api_version(customer)
 8		]).then do |is_jmp, api|
 9			is_jmp ? JMP.new : api
10		end
11	end
12
13	def self.sgx_jmp?(customer)
14		key = "catapult_cred-customer_#{customer.customer_id}@jmp.chat"
15		REDIS.exists(key).then { |is_sgx| is_sgx == 1 }
16	end
17
18	def self.api_version(customer)
19		REDIS.lindex("catapult_cred-#{customer.jid}", 0).then do |api|
20			case api
21			when CONFIG.dig(:catapult, :user)
22				V1.new
23			when CONFIG.dig(:creds, :account)
24				V2.new
25			else
26				new
27			end
28		end
29	end
30
31	class V1 < API
32		def to_s
33			"v1"
34		end
35	end
36
37	class V2 < API
38		def to_s
39			"v2"
40		end
41	end
42
43	class JMP < V2
44		def to_s
45			"sgx-jmp"
46		end
47	end
48
49	def to_s
50		"not JMP"
51	end
52end