bandwidth_tn_repo.rb

 1# frozen_string_literal: true
 2
 3require "ruby-bandwidth-iris"
 4
 5class BandwidthTnRepo
 6	def initialize
 7		@move_client =
 8			BandwidthIris::Client.new(
 9				account_id: CONFIG[:keep_area_codes_in][:account],
10				username: CONFIG[:creds][:username],
11				password: CONFIG[:creds][:password]
12			)
13	end
14
15	def find(tel)
16		BandwidthIris::Tn.new(telephone_number: tel).get_details.catch { {} }
17	end
18
19	def put_lidb_name(tel, lidb_name)
20		BandwidthIris::Lidb.create(
21			lidb_tn_groups: { lidb_tn_group: {
22				telephone_numbers: { telephone_number: tel.sub(/\A\+1/, "") },
23				subscriber_information: lidb_name,
24				use_type: "RESIDENTIAL", visibility: "PUBLIC"
25			} }
26		)
27	rescue BandwidthIris::Errors::GenericError
28		raise "Could not set CNAM, please contact support"
29	end
30
31	def disconnect(tel, order_name)
32		tn = tel.sub(/\A\+1/, "")
33		if CONFIG[:keep_area_codes].find { |area| tn.start_with?(area) }
34			BandwidthIris::Tn.new({ telephone_number: tn }, @move_client).move(
35				customer_order_id: order_name,
36				source_account_id: CONFIG[:creds][:account],
37				**CONFIG[:keep_area_codes_in].slice(:site_id, :sip_peer_id)
38			)
39		else
40			BandwidthIris::Disconnect.create(order_name, tn)
41		end
42	end
43
44	def move(tel, order_name, source_account_id)
45		tn = tel.sub(/\A\+1/, "")
46		BandwidthIris::Tn.new({ telephone_number: tn }, @move_client).move(
47			customer_order_id: order_name,
48			source_account_id: source_account_id,
49			site_id: CONFIG[:bandwidth_site],
50			sip_peer_id: CONFIG[:bandwidth_peer]
51		)
52	end
53end