# frozen_string_literal: true module TwilioBase module Protocols module Voice class Sip < Base UTU_HEADER_NAME = 'User-to-User' def endpoint "sip:#{sip_endpoint}?#{parsed_headers}" end def name :sip end def routing_method sip_refer_enabled? ? :refer : :dial end def routing_attributes sip_refer_enabled? ? refer_attribues : dial_attribues end private def parsed_headers hash = headers.each_with_object({}) do |(k, v), obj| key = k.to_s key = "X-#{key}" unless utu_header?(key) value = v.presence || '-' obj[key] = value end Addressable::URI.new(query_values: hash).query end def headers params[:headers].is_a?(Hash) ? params[:headers] : {} end def sip_endpoint sip_whip end def sip_refer_enabled? params[:sip_refer_enabled] end def refer_attribues { action: action_url } end def sip_whip @sip_whip ||= sip_endpoints.sample.to_s.strip end def sip_endpoints value = Array(to) value.flat_map { |v| v.split(',') } end def utu_header?(key) key == UTU_HEADER_NAME end end end end end