Module: Switest::FromParser

Defined in:
lib/switest/from_parser.rb

Overview

Parses a from string into FreeSWITCH channel variables, replicating mod_rayo's parse_dial_from() behavior.

Algorithm (matching mod_rayo):

  1. Split on last space — before = display name, after = URI
  2. No space — entire string is URI, no display name
  3. Strip "" from display name (if quoted)
  4. Strip <> from URI (if angle-bracketed)
  5. Detect scheme: sip:/sips: with @ → SIP, tel: → TEL, plain → TEL, empty → UNKNOWN
  6. Map to FreeSWITCH variables accordingly

Constant Summary collapse

SIP_URI_PATTERN =
/\Asips?:.+@/

Class Method Summary collapse

Class Method Details

.build_sip_vars(display_name, uri) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/switest/from_parser.rb', line 80

def build_sip_vars(display_name, uri)
  vars = {
    sip_from_uri: uri,
    origination_caller_id_number: uri
  }

  if display_name
    vars[:sip_from_display] = display_name
    vars[:origination_caller_id_name] = display_name
  end

  vars
end

.build_tel_vars(display_name, number) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/switest/from_parser.rb', line 94

def build_tel_vars(display_name, number)
  vars = {
    origination_caller_id_number: number,
    origination_caller_id_name: display_name || number
  }

  vars
end

.parse(from) ⇒ Hash

Parse a from string into a hash of FreeSWITCH channel variable symbol keys.

Parameters:

  • from (String, nil)

    The from string to parse

Returns:

  • (Hash)

    Symbol keys matching channel variable names, only keys with values



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/switest/from_parser.rb', line 23

def parse(from)
  return {} if from.nil? || from.strip.empty?

  display_name, uri = split_display_and_uri(from)
  display_name = strip_quotes(display_name)
  uri = strip_angle_brackets(uri)

  return {} if uri.empty?

  if uri.match?(SIP_URI_PATTERN)
    build_sip_vars(display_name, uri)
  elsif uri.start_with?("tel:")
    number = uri.delete_prefix("tel:")
    build_tel_vars(display_name, number)
  else
    build_tel_vars(display_name, uri)
  end
end

.split_display_and_uri(from) ⇒ Object

Split on last space: everything before = display name, after = URI. If no space, entire string is URI with no display name.



44
45
46
47
48
49
50
51
52
53
# File 'lib/switest/from_parser.rb', line 44

def split_display_and_uri(from)
  from = from.strip
  last_space = from.rindex(" ")

  if last_space
    [from[0...last_space], from[(last_space + 1)..]]
  else
    [nil, from]
  end
end

.strip_angle_brackets(uri) ⇒ Object

Strip surrounding angle brackets from URI.



69
70
71
72
73
74
75
76
77
78
# File 'lib/switest/from_parser.rb', line 69

def strip_angle_brackets(uri)
  return "" if uri.nil?
  uri = uri.strip

  if uri.start_with?("<") && uri.end_with?(">")
    uri = uri[1...-1]
  end

  uri
end

.strip_quotes(name) ⇒ Object

Strip surrounding double-quotes from display name.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/switest/from_parser.rb', line 56

def strip_quotes(name)
  return nil if name.nil?
  name = name.strip
  return nil if name.empty?

  if name.start_with?('"') && name.end_with?('"')
    name = name[1...-1]
  end

  name.empty? ? nil : name
end