customer_fwd.rb

  1# frozen_string_literal: true
  2
  3require "value_semantics/monkey_patched"
  4require "uri"
  5
  6class CustomerFwd
  7	def self.for(uri:, timeout:)
  8		timeout = Timeout.new(timeout)
  9		return None.new(uri: uri, timeout: timeout) if !uri || timeout.zero?
 10
 11		if uri =~ /\Asip:(.*)@sip.cheogram.com\Z/
 12			uri = "xmpp:#{$1.gsub(/%([0-9A-F]{2})/i) { $1.to_i(16).chr }}"
 13		end
 14		URIS.fetch(uri.split(":", 2).first.to_sym) {
 15			raise "Unknown forward URI: #{uri}"
 16		}.new(uri: uri, timeout: timeout)
 17	end
 18
 19	class Timeout
 20		def self.new(s)
 21			s.is_a?(self) ? s : super
 22		end
 23
 24		def initialize(s)
 25			@timeout = s.nil? || s.to_i.negative? ? 300 : s.to_i
 26		end
 27
 28		def zero?
 29			@timeout.zero?
 30		end
 31
 32		def to_i
 33			@timeout
 34		end
 35	end
 36
 37	value_semantics do
 38		uri Either(String, NilClass)
 39		def_attr :timeout, Timeout, coerce: Timeout.method(:new)
 40	end
 41
 42	def with(new_attrs)
 43		CustomerFwd.for(to_h.merge(new_attrs))
 44	end
 45
 46	def create_call(account)
 47		request = Bandwidth::ApiCreateCallRequest.new.tap { |cc|
 48			cc.to = to
 49			cc.call_timeout = timeout.to_i
 50			yield cc if block_given?
 51		}
 52		BANDWIDTH_VOICE.create_call(account, body: request).data.call_id
 53	end
 54
 55	def v2_safe?
 56		false
 57	end
 58
 59	class Tel < CustomerFwd
 60		def v2_safe?
 61			true
 62		end
 63
 64		def to
 65			uri.sub(/^tel:/, "")
 66		end
 67	end
 68
 69	class SIP < CustomerFwd
 70		def v2_safe?
 71			uri.end_with?(CONFIG[:sip][:realm])
 72		end
 73
 74		def to
 75			uri
 76		end
 77	end
 78
 79	class XMPP < CustomerFwd
 80		def v2_safe?
 81			true
 82		end
 83
 84		def to
 85			jid = uri.sub(/^xmpp:/, "")
 86			"sip:#{ERB::Util.url_encode(jid)}@sip.cheogram.com"
 87		end
 88	end
 89
 90	class None < CustomerFwd
 91		def create_call; end
 92
 93		def to; end
 94	end
 95
 96	URIS = {
 97		tel: Tel,
 98		sip: SIP,
 99		xmpp: XMPP
100	}.freeze
101end