# frozen_string_literal: true require "digest" require "securerandom" require "value_semantics/monkey_patched" require_relative "bandwidth_iris_patch" require_relative "mn_words" class SipAccount def self.find(customer_id) new(BandwidthIris::SipCredential.get("c#{customer_id}")) rescue BandwidthIris::APIError # 404 New.new(BandwidthIris::SipCredential.new( user_name: "c#{customer_id}", realm: CONFIG[:sip][:realm], http_voice_v2_app_id: CONFIG[:sip][:app] )) end def initialize(api_object, password: nil) @api_object = api_object @password = password end def with(password:) self.class.new(@api_object.class.new(@api_object.to_data.merge( hash1: Digest::MD5.hexdigest("#{username}:#{server}:#{password}"), hash1b: Digest::MD5.hexdigest( "#{username}:#{server}:#{server}:#{password}" ) )), password: password) end def with_random_password with(password: MN_WORDS.sample(3).join(" ")) end PORT = { var: "port", type: :fixed, value: "5008", label: "Port", description: "usually optional" }.freeze def form form = Blather::Stanza::X.new(:result) form.title = "Sip Account Reset!" form.instructions = "These are your new SIP credentials" form.fields = [ { var: "username", value: username, label: "Username", type: :fixed }, { var: "password", value: @password, label: "Password", type: :fixed }, { var: "server", value: server, label: "Server", type: :fixed }, PORT ] form end def put @api_object.update( hash1: @api_object.hash1, hash1b: @api_object.hash1b, realm: server, http_voice_v2_app_id: @api_object.http_voice_v2_app_id ) self end def delete @api_object.delete end def username @api_object.user_name.to_s end def server @api_object.realm end def uri "sip:#{username}@#{server}" end class New < SipAccount def put BandwidthIris::SipCredential.create( user_name: username, hash1: @api_object.hash1, hash1b: @api_object.hash1b, realm: server, http_voice_v2_app_id: @api_object.http_voice_v2_app_id ) self end end end