diff --git a/lib/call_attempt.rb b/lib/call_attempt.rb index 3107e0fc262f9f5b73934339b891d21dad56b58a..9116caeea471876909f15ba0ddeab327ab9cc5ad 100644 --- a/lib/call_attempt.rb +++ b/lib/call_attempt.rb @@ -2,6 +2,7 @@ require "value_semantics/monkey_patched" +require_relative "tts_template" require_relative "low_balance" class CallAttempt @@ -24,14 +25,19 @@ class CallAttempt def self.for_ask_or_go(customer, otel, rate, usage, digits: nil, **kwargs) can_use = customer.minute_limit.to_d + customer.monthly_overage_limit + kwargs.merge!( + customer_id: customer.customer_id, + from: customer.registered?.phone, to: otel + ) if digits != "1" && can_use - usage < rate * 10 - AtLimit.new(**kwargs.slice(:direction, :call_id)) + AtLimit.new(**kwargs) else - new(from: customer.registered?.phone, to: otel, **kwargs) + new(**kwargs) end end value_semantics do + customer_id String from(/\A\+\d+\Z/) to(/\A\+\d+\Z/) call_id String @@ -46,16 +52,44 @@ class CallAttempt fwd.create_call(*args, &block) end + def as_json(*) + { + from: from, + to: to, + customer_id: customer_id + } + end + + def to_json(*args) + as_json.to_json(*args) + end + class Unsupported value_semantics do direction Either(:inbound, :outbound) end + def view + "#{direction}/unsupported" + end + + def tts + TTSTemplate.new(view).tts(self) + end + def to_render - ["#{direction}/unsupported"] + [view] end def create_call(*); end + + def as_json(*) + { tts: tts } + end + + def to_json(*args) + as_json.to_json(*args) + end end class NoBalance @@ -77,25 +111,65 @@ class CallAttempt direction Either(:inbound, :outbound) end + def view + "#{direction}/no_balance" + end + + def tts + TTSTemplate.new(view).tts(self) + end + def to_render - ["#{direction}/no_balance", { locals: to_h }] + [view, { locals: to_h }] end def create_call(*); end + + def as_json(*) + { tts: tts } + end + + def to_json(*args) + as_json.to_json(*args) + end end class AtLimit value_semantics do + customer_id String + from(/\A\+\d+\Z/) + to(/\A\+\d+\Z/) call_id String direction Either(:inbound, :outbound) end + def view + "#{direction}/at_limit" + end + + def tts + TTSTemplate.new(view).tts(self) + end + def to_render - ["#{direction}/at_limit", { locals: to_h }] + [view, { locals: to_h }] end def create_call(fwd, *args, &block) fwd.create_call(*args, &block) end + + def as_json(*) + { + tts: tts, + from: from, + to: to, + customer_id: customer_id + } + end + + def to_json(*args) + as_json.to_json(*args) + end end end diff --git a/lib/tts_template.rb b/lib/tts_template.rb new file mode 100644 index 0000000000000000000000000000000000000000..e392d5c5c84ff35f38e4f8b803ab3eef57331d83 --- /dev/null +++ b/lib/tts_template.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class TTSTemplate + def initialize(view) + @view = view + end + + def template_path + "#{File.dirname(__dir__)}/views/#{@view}.slim" + end + + def tts(scope) + Nokogiri::XML.parse( + Slim::Template.new(template_path).render(scope) + ).find("//SpeakSentence").map(&:content).join(" ") + end +end diff --git a/web.rb b/web.rb index 80e78ae1ba2cf3419cde23bfeae588a310bd9c55..2740b5217d0c1118fabc955d75fa15e45f2bf2fb 100644 --- a/web.rb +++ b/web.rb @@ -307,7 +307,11 @@ class Web < Roda params["to"], call_id: params["callId"], digits: params["digits"] - ).then { |ca| render(*ca.to_render) } + ).then do |ca| + r.json { ca.to_json } + + render(*ca.to_render) + end end end end