diff --git a/lib/cdr.rb b/lib/cdr.rb index efac8ab0e76a3b59fc07eaacc52352d10873cee9..b8077378605cdd995d06fff3ada51ceebfd14e77 100644 --- a/lib/cdr.rb +++ b/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 ) diff --git a/test/test_cdr.rb b/test/test_cdr.rb new file mode 100644 index 0000000000000000000000000000000000000000..3d6e8581b9907cc3175bf5204fd762f78aa30ed7 --- /dev/null +++ b/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