Owner more often etc

Stephen Paul Weber created

Change summary

lib/message_event.rb   | 38 ++++++++++++++------------------------
sgx-bwmsgsv2.rb        |  2 ++
test/test_component.rb |  1 +
3 files changed, 17 insertions(+), 24 deletions(-)

Detailed changes

lib/message_event.rb 🔗

@@ -24,7 +24,7 @@ module MessageEvent
 		def initialize(event:, timestamp: nil)
 			raise ArgumentError, "event must be a String" unless event.is_a?(String)
 
-			Time.iso8601(timestamp) if timestamp.is_a?(String)
+			timestamp = Time.iso8601(timestamp) if timestamp.is_a?(String)
 			@event = event
 			@timestamp = timestamp
 		end
@@ -48,15 +48,21 @@ module MessageEvent
 	end
 
 	class Message < Base
-		def initialize(to:, from:, body:, media_urls: [], **kwargs)
+		def initialize(to:, from:, body:, owner:, bandwidth_id: nil, media_urls: [], **kwargs)
+			raise ArgumentError, "owner must be a valid US telephone number" unless ValidTel === owner
 			raise ArgumentError, "to must be an array" unless to.is_a?(Array)
 			raise ArgumentError, "body must be a String" unless body.is_a?(String)
 			raise ArgumentError, "media_urls must be an Array" unless media_urls.is_a?(Array)
+			if bandwidth_id && !bandwidth_id.is_a?(String)
+				raise ArgumentError, "bandwidth_id must be a String"
+			end
 
 			@from = from
 			@to = to
 			@body = body
 			@media_urls = media_urls
+			@owner = owner
+			@bandwidth_id = bandwidth_id if bandwidth_id
 			super(**kwargs)
 		end
 
@@ -64,53 +70,37 @@ module MessageEvent
 
 		def to_redis_fields
 			fields = super.merge(
+				"owner" => @owner,
 				"from" => @from,
 				"to" => JSON.dump(@to)
 			)
-			fields["body"] = @body unless @body.nil? || @body.empty?
+			fields["body"] = @body unless @body.nil?
+			fields["bandwidth_id"] = @bandwidth_id
 			fields["media_urls"] = JSON.dump(@media_urls) unless @media_urls.empty?
 			fields
 		end
 	end
 
 	class In < Message
-		def initialize(owner:, bandwidth_id:, **kwargs)
-			raise ArgumentError, "owner must be a valid US telephone number" unless ValidTel === owner
-			raise ArgumentError, "bandwidth_id must be a String" unless bandwidth_id.is_a?(String)
-
-			@owner = owner
-			@bandwidth_id = bandwidth_id
+		def initialize(**kwargs)
 			super(event: "in", **kwargs)
 		end
 
 		attr_reader :owner, :bandwidth_id
-
-		def to_redis_fields
-			super.merge(
-				"owner" => @owner,
-				"bandwidth_id" => @bandwidth_id
-			)
-		end
 	end
 
 	class Out < Message
-		def initialize(stanza_id:, bandwidth_id: nil, **kwargs)
+		def initialize(stanza_id:, **kwargs)
 			raise ArgumentError, "stanza_id must be a String" unless stanza_id.is_a?(String)
-			if bandwidth_id && !bandwidth_id.is_a?(String)
-				raise ArgumentError, "bandwidth_id must be a String"
-			end
 
 			@stanza_id = stanza_id
-			@bandwidth_id = bandwidth_id
 			super(event: "out", **kwargs)
 		end
 
 		attr_reader :stanza_id, :bandwidth_id
 
 		def to_redis_fields
-			fields = super.merge("stanza_id" => @stanza_id)
-			fields["bandwidth_id"] = @bandwidth_id if @bandwidth_id
-			fields
+			super.merge("stanza_id" => @stanza_id)
 		end
 	end
 

sgx-bwmsgsv2.rb 🔗

@@ -233,6 +233,7 @@ module SGXbwmsgsv2
 		# ID provides the emit time.
 		oob_url = m.at("oob|x > oob|url", oob: "jabber:x:oob")&.text
 		MessageEvent::Thru.new(
+			owner: users_num,
 			from: users_num,
 			to: [dest_num],
 			stanza_id: m.id.to_s,
@@ -376,6 +377,7 @@ module SGXbwmsgsv2
 			parsed = JSON.parse(response) rescue {}
 			MessageEvent::Out.new(
 				timestamp: parsed["time"] || Time.now,
+				owner: usern,
 				from: usern,
 				to: Array(num_dest),
 				stanza_id: s.id.to_s,

test/test_component.rb 🔗

@@ -549,6 +549,7 @@ class ComponentTest < Minitest::Test
 		event = entries.first[:fields]
 		assert_equal "out", event["event"]
 		assert_equal "+15550000000", event["from"]
+		assert_equal "+15550000000", event["owner"]
 		assert_equal JSON.dump(["+15551234567"]), event["to"]
 		assert_equal "stanza-123", event["stanza_id"]
 		assert_equal "bw-msg-123", event["bandwidth_id"]