Allow constructing CDR for an inbound or outbound event

Stephen Paul Weber created

Change summary

lib/cdr.rb       | 23 +++++++++++++++++++----
test/test_cdr.rb | 42 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 61 insertions(+), 4 deletions(-)

Detailed changes

lib/cdr.rb 🔗

@@ -13,15 +13,30 @@ class CDR
 		direction Either(:inbound, :outbound)
 	end
 
-	def self.for_disconnect(event)
+	def self.for(event, **kwargs)
 		start = Time.parse(event["startTime"])
 
-		new(
+		new({
 			cdr_id: "sgx-jmp/#{event['callId']}",
-			customer_id: event["from"].sub(/^\+/, ""),
 			start: start,
 			billsec: (Time.parse(event["endTime"]) - start).ceil,
-			disposition: Disposition.for(event["cause"]),
+			disposition: Disposition.for(event["cause"])
+		}.merge(kwargs))
+	end
+
+	def self.for_inbound(customer_id, event)
+		self.for(
+			event,
+			customer_id: customer_id,
+			tel: event["from"],
+			direction: :inbound
+		)
+	end
+
+	def self.for_outbound(event)
+		self.for(
+			event,
+			customer_id: event["from"].sub(/^\+/, ""),
 			tel: event["to"],
 			direction: :outbound
 		)

test/test_cdr.rb 🔗

@@ -0,0 +1,42 @@
+# frozen_string_literal: true
+
+require "test_helper"
+require "cdr"
+
+class CDRTest < Minitest::Test
+	def test_for_inbound
+		cdr = CDR.for_inbound(
+			"test",
+			"from" => "+15551234567",
+			"startTime" => "2020-01-01T00:00:00Z",
+			"endTime" => "2020-01-01T01:00:00Z",
+			"callId" => "a_call",
+			"cause" => "hangup"
+		)
+		assert_equal cdr.cdr_id, "sgx-jmp/a_call"
+		assert_equal cdr.customer_id, "test"
+		assert_equal cdr.start, Time.parse("2020-01-01T00:00:00Z")
+		assert_equal cdr.billsec, 60 * 60
+		assert_equal cdr.disposition, "ANSWERED"
+		assert_equal cdr.tel, "+15551234567"
+		assert_equal cdr.direction, :inbound
+	end
+
+	def test_for_outbound
+		cdr = CDR.for_outbound(
+			"to" => "+15551234567",
+			"from" => "+test",
+			"startTime" => "2020-01-01T00:00:00Z",
+			"endTime" => "2020-01-01T01:00:00Z",
+			"callId" => "a_call",
+			"cause" => "hangup"
+		)
+		assert_equal cdr.cdr_id, "sgx-jmp/a_call"
+		assert_equal cdr.customer_id, "test"
+		assert_equal cdr.start, Time.parse("2020-01-01T00:00:00Z")
+		assert_equal cdr.billsec, 60 * 60
+		assert_equal cdr.disposition, "ANSWERED"
+		assert_equal cdr.tel, "+15551234567"
+		assert_equal cdr.direction, :outbound
+	end
+end