# frozen_string_literal: true module TwilioBase class PhoneNumber COUNTRY_CODE = ENV.fetch('COUNTRY_CODE') class << self include TwilioBase::Clients def find_or_create(phone_number) phone_number ? find(phone_number) : create end def incoming_phone_number_list(**args) incoming_phone_numbers.list(args) end private def find(phone_number) incoming_phone_number_list(phone_number: phone_number).first end def create number = available_numbers.first.phone_number purchase_number(number) end def available_numbers rest_client.api.available_phone_numbers(COUNTRY_CODE).local.list( sms_enabled: true ) end def purchase_number(number) incoming_phone_numbers.create(phone_number: number) end def incoming_phone_numbers rest_client.api.incoming_phone_numbers end end end end