~singpolyma/sgx-jmp

ref: 4178a87a901cbf00cddbf9671c2f99aa79dfd3a8 sgx-jmp/lib/catapult.rb -rw-r--r-- 797 bytes
4178a87aStephen Paul Weber Factor out Catapult connection 1 year, 2 months 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
# frozen_string_literal: true

require "value_semantics/monkey_patched"

class Catapult
	value_semantics do
		user String
		token String
		secret String
		application_id String
	end

	def import(body)
		post(
			"phoneNumbers",
			body: { applicationId: application_id }.merge(body)
		)
	end

	def post(path, body:, head: {})
		EM::HttpRequest.new(
			mkurl(path), tls: { verify_peer: true }
		).apost(
			head: catapult_headers.merge(head),
			body: body.to_json
		)
	end

	def mkurl(path)
		base = "https://api.catapult.inetwork.com/v1/users/#{@user}/"
		return path if path.start_with?(base)
		"#{base}#{path}"
	end

protected

	def catapult_headers
		{
			"Authorization" => [@token, @secret],
			"Content-Type" => "application/json"
		}
	end
end

CATAPULT = Catapult.new(**CONFIG[:catapult])