Method: Ax25::ImmutableHop.from_raw

Defined in:
lib/ax25/frame/immutable_hop.rb

.from_raw(raw_hop, strict_callsign: true, strict_ssid: true) ⇒ Object

Raises:

  • (ArgumentError)

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ax25/frame/immutable_hop.rb', line 21

def self.from_raw(raw_hop, strict_callsign: true, strict_ssid: true)
  raise ArgumentError.new("raw_hop can not be nil") if raw_hop.nil?

  callsign = nil
  ssid = nil
  seen = nil

  hop = raw_hop.dup
  if hop[-1].eql? "*"
    seen = true
    hop = hop[0..-2]
  else
    seen = false
  end

  raise ArgumentError.new("Hops can only contain letters, numbers and dashes. Hop: #{hop.to_s}") if hop.strip.match?(/[^a-zA-Z0-9\-]/) && strict_callsign

  if not hop.include? "-"
    ssid = nil
    callsign = hop.strip
  else
    split_hop = hop.strip.split("-")
    raise ArgumentError.new("More than one hypen seen in a hop, invalid format") if split_hop.length > 2
    raise ArgumentError.new("Hop format was not valid, hyphen placed at end or beginning of string") if split_hop.length <= 1
    callsign = split_hop[0]
    ssid = split_hop[1].to_i
  end

  return Ax25::ImmutableHop.new(callsign, ssid, seen, strict_callsign: strict_callsign, strict_ssid: strict_ssid)
end