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
17 rescue StandardError
18 {}
19 end
20
21 def put_lidb_name(tel, lidb_name)
22 BandwidthIris::Lidb.create(
23 lidb_tn_groups: { lidb_tn_group: {
24 telephone_numbers: { telephone_number: tel.sub(/\A\+1/, "") },
25 subscriber_information: lidb_name,
26 use_type: "RESIDENTIAL", visibility: "PUBLIC"
27 } }
28 )
29 rescue BandwidthIris::Errors::GenericError
30 raise "Could not set CNAM, please contact support"
31 end
32
33 def disconnect(tel, order_name)
34 tn = tel.sub(/\A\+1/, "")
35 if CONFIG[:keep_area_codes].find { |area| tn.start_with?(area) }
36 BandwidthIris::Tn.new({ telephone_number: tn }, @move_client).move(
37 customer_order_id: order_name,
38 source_account_id: CONFIG[:creds][:account],
39 **CONFIG[:keep_area_codes_in].slice(:site_id, :sip_peer_id)
40 )
41 else
42 BandwidthIris::Disconnect.create(order_name, tn)
43 end
44 end
45
46 def move(tel, order_name, source_account_id)
47 tn = tel.sub(/\A\+1/, "")
48 BandwidthIris::Tn.new({ telephone_number: tn }, @move_client).move(
49 customer_order_id: order_name,
50 source_account_id: source_account_id,
51 site_id: CONFIG[:bandwidth_site],
52 sip_peer_id: CONFIG[:bandwidth_peer]
53 )
54 end
55end