From 64d9fc92325095337d14982e741306b25bfee0fd Mon Sep 17 00:00:00 2001
From: Jeffrey Phillips Freeman <the@jeffreyfreeman.me>
Date: Thu, 17 Aug 2023 17:07:38 -0400
Subject: [PATCH] Made IGATE reads less strict on parsing

---
 lib/apex/encoder/igate_tcp.rb      |  6 +++---
 lib/apex/frame/immutable_entity.rb | 12 ++++++------
 lib/apex/frame/immutable_hop.rb    | 14 +++++++-------
 lib/apex/frame/immutable_path.rb   |  4 ++--
 spec/apex/frame/path_spec.rb       |  7 ++++---
 5 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/lib/apex/encoder/igate_tcp.rb b/lib/apex/encoder/igate_tcp.rb
index 4011030..edd73fe 100644
--- a/lib/apex/encoder/igate_tcp.rb
+++ b/lib/apex/encoder/igate_tcp.rb
@@ -56,9 +56,9 @@ module Apex
 
             path = path.split(',')
 
-            decoded_source = Apex::ImmutableEntity.from_raw(decoded_source)
-            decoded_destination = Apex::ImmutableEntity.from_raw(path.shift)
-            decoded_path = Apex::ImmutablePath.from_raw(path)
+            decoded_source = Apex::ImmutableEntity.from_raw(decoded_source, strict_callsign: false, strict_ssid: false)
+            decoded_destination = Apex::ImmutableEntity.from_raw(path.shift, strict_callsign: false, strict_ssid: false)
+            decoded_path = Apex::ImmutablePath.from_raw(path, strict_callsign: false, strict_ssid: false)
             decoded_payload = frame_so_far
 
             return Apex::ImmutableFrame.new(decoded_source, decoded_destination, decoded_path, decoded_payload)
diff --git a/lib/apex/frame/immutable_entity.rb b/lib/apex/frame/immutable_entity.rb
index 458dcd7..bd92164 100644
--- a/lib/apex/frame/immutable_entity.rb
+++ b/lib/apex/frame/immutable_entity.rb
@@ -8,15 +8,15 @@ module Apex
     attr_reader :callsign, :ssid
 
     public
-    def initialize(callsign, ssid)
+    def initialize(callsign, ssid, strict_callsign: true, strict_ssid: true)
       raise ArgumentError.new("callsign can not be nil") if callsign.nil?
 
       raise ArgumentError.new("callsign must be a String") if not callsign.kind_of? String
       raise ArgumentError.new("ssid must be an Integer.") if (not ssid.nil?) && (not ssid.kind_of? Integer)
 
-      raise ArgumentError.new("ssid must be a value between 0 (inclusive) and 15 (inclusive): #{callsign}-#{ssid}") if (not ssid.nil?) && (ssid < 0 || ssid > 15)
+      raise ArgumentError.new("ssid must be a value between 0 (inclusive) and 15 (inclusive): #{callsign}-#{ssid}") if (not ssid.nil?) && (ssid < 0 || ssid > 15) && (strict_ssid)
       raise ArgumentError.new("Callsign can not be an empty string") if callsign.empty?
-      raise ArgumentError.new("Callsign must only contain numebers and letters") if callsign.strip.match?(/[^a-zA-Z0-9]/)
+      raise ArgumentError.new("Callsign must only contain numebers and letters") if callsign.strip.match?(/[^a-zA-Z0-9]/) && (strict_callsign)
 
       @callsign = callsign.strip.upcase.freeze
       if (ssid.nil?) || (ssid.eql? 0)
@@ -27,7 +27,7 @@ module Apex
     end
 
     public
-    def self.from_raw(raw_hop)
+    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
@@ -48,14 +48,14 @@ module Apex
         ssid = split_hop[1].to_i
       end
 
-      return Apex::ImmutableEntity.new(callsign, ssid)
+      return Apex::ImmutableEntity.new(callsign, ssid, strict_callsign: strict_callsign, strict_ssid: strict_ssid)
     end
 
     public
     def decrement_ssid
       raise RangeError.new("SSID can not be decremented when it is currently 0 or nil") if (self.ssid.nil?) or (self.ssid <= 0)
 
-      return Apex::ImmutableEntity.new(self.callsign, self.ssid - 1)
+      return Apex::ImmutableEntity.new(self.callsign, self.ssid - 1, strict_callsign: false, strict_ssid: false)
     end
 
     public
diff --git a/lib/apex/frame/immutable_hop.rb b/lib/apex/frame/immutable_hop.rb
index 4645b81..3613765 100644
--- a/lib/apex/frame/immutable_hop.rb
+++ b/lib/apex/frame/immutable_hop.rb
@@ -7,18 +7,18 @@ module Apex
     include Hop
 
     public
-    def initialize(callsign, ssid, seen)
+    def initialize(callsign, ssid, seen, strict_callsign: true, strict_ssid: true)
 
       raise ArgumentError.new("seen can not be nil") if seen.nil?
       raise ArgumentError.new("seen must be a Boolean") if not (!!seen == seen)
 
-      super(callsign, ssid)
+      super(callsign, ssid, strict_callsign: strict_callsign, strict_ssid: strict_ssid)
 
       @seen = seen
     end
 
     public
-    def self.from_raw(raw_hop)
+    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
@@ -33,7 +33,7 @@ module Apex
         seen = false
       end
 
-      raise ArgumentError.new("Hops can only contain letters, numbers and dashes. Hop: " + hop) if hop.strip.match?(/[^a-zA-Z0-9\-]/)
+      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
@@ -46,7 +46,7 @@ module Apex
         ssid = split_hop[1].to_i
       end
 
-      return Apex::ImmutableHop.new(callsign, ssid, seen)
+      return Apex::ImmutableHop.new(callsign, ssid, seen, strict_callsign: strict_callsign, strict_ssid: strict_ssid)
     end
 
     public
@@ -56,14 +56,14 @@ module Apex
 
     public
     def toggle_seen
-      return Apex::ImmutableHop.new(self.callsign, self.ssid, !self.seen?)
+      return Apex::ImmutableHop.new(self.callsign, self.ssid, !self.seen?, strict_callsign: false, strict_ssid: false)
     end
 
     public
     def decrement_ssid
       raise RangeError.new("SSID can not be decremented when it is currently 0 or nil") if (self.ssid.nil?) or (self.ssid <= 0)
 
-      return Apex::ImmutableHop.new(self.callsign, self.ssid - 1, self.seen?)
+      return Apex::ImmutableHop.new(self.callsign, self.ssid - 1, self.seen?, strict_callsign: false, strict_ssid: false)
     end
 
     public
diff --git a/lib/apex/frame/immutable_path.rb b/lib/apex/frame/immutable_path.rb
index cce3335..bfd90c3 100644
--- a/lib/apex/frame/immutable_path.rb
+++ b/lib/apex/frame/immutable_path.rb
@@ -23,7 +23,7 @@ module Apex
     end
 
     public
-    def self.from_raw(raw_path)
+    def self.from_raw(raw_path, strict_callsign: true, strict_ssid: true)
       raise ArgumentError.new("raw_path can not be nil") if raw_path.nil?
 
       if raw_path.kind_of? String
@@ -43,7 +43,7 @@ module Apex
         if hop.kind_of? Apex::Hop
           new_hop = hop
         else
-          new_hop = Apex::ImmutableHop.from_raw(hop)
+          new_hop = Apex::ImmutableHop.from_raw(hop, strict_callsign: strict_callsign, strict_ssid: strict_ssid)
         end
         new_path_array << new_hop
       end
diff --git a/spec/apex/frame/path_spec.rb b/spec/apex/frame/path_spec.rb
index 9ab4efb..003bf0a 100644
--- a/spec/apex/frame/path_spec.rb
+++ b/spec/apex/frame/path_spec.rb
@@ -70,10 +70,11 @@ describe Apex::Path do
     context "Given two hops where the first is unseen and second is seen" do
       hop1 = Apex::ImmutableHop.new("WI2ARD", 4, false)
       hop2 = Apex::ImmutableHop.new("K3TXD", 0, true)
-      it "throws an ArgumentError" do
-        expect {
+      it "instantiates a path" do
           path = Apex::ImmutablePath.new(hop1, hop2)
-        }.to raise_error(ArgumentError)
+          expect(path).to_not be_nil
+          expect(path).to be_kind_of(Apex::ImmutablePath)
+          expect(path).to be_kind_of(Apex::Path)
       end
     end
   end
-- 
GitLab