Class: Mock::Twilio::Decorators::Api2010::Calls

Inherits:
Object
  • Object
show all
Defined in:
lib/mock/twilio/decorators/api_2010/calls.rb

Constant Summary collapse

@@scheduler =
Rufus::Scheduler.new

Class Method Summary collapse

Class Method Details

.call_sid(body, request) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mock/twilio/decorators/api_2010/calls.rb', line 28

def call_sid(body, request)
  prefix = "CA"
  sid = prefix + SecureRandom.hex(16)
  @@scheduler.in '2s' do
    conference_uuid = request.data["Url"].split("conference_uuid=").last
    begin
      response = Mock::Twilio::Webhooks::CallStatusUpdates.trigger(sid, conference_uuid, 'unknown', 'ringing')

      conference_response = if response.success?
                              twiMl_xml = Nokogiri::XML response.body
                              friendly_name = twiMl_xml.at_xpath('//Dial').at_xpath('//Conference').children.text
                              Mock::Twilio::Webhooks::Conferences.trigger(friendly_name)
                            end

      # Participant
      participant_ringing   = Mock::Twilio::Webhooks::Calls.trigger(sid, 'ringing') if conference_response.success?
      participant_completed = Mock::Twilio::Webhooks::Calls.trigger(sid, 'completed') if participant_ringing.success?

      call_completed = Mock::Twilio::Webhooks::CallStatusUpdates.trigger(sid, conference_uuid, 'human', 'completed') if participant_completed.success?
      Mock::Twilio::Webhooks::Voicemail.trigger(sid) if call_completed.success? && [true,false].sample
    rescue  => e
      puts e
    end
  end
  body["sid"] = sid
end

.decorate(body, request) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mock/twilio/decorators/api_2010/calls.rb', line 11

def decorate(body, request)
  body["date_updated"] = Time.current.rfc2822 if body["date_updated"]
  body["date_sent"] = Time.current.rfc2822 if body["date_sent"]
  body["date_created"] = Time.current.rfc2822 if body["date_created"]
  body["start_time"] = Time.current.rfc2822 if body["start_time"]
  body["end_time"] = Time.current.rfc2822 if body["end_time"]

  call_sid(body, request) if body["sid"]
  phone_number_sid(body, request) if body["phone_number_sid"]
  body["direction"] = "outbound-api" if body["direction"]
  body["api_version"] = "2010-04-01" if body["api_version"]
  body["to"] = request.data['To'] if body["to"]
  body["from"] = request.data['From'] if body["from"]

  body
end

.phone_number_sid(body, request) ⇒ Object



55
56
57
58
59
# File 'lib/mock/twilio/decorators/api_2010/calls.rb', line 55

def phone_number_sid(body, request)
  prefix = "PN"
  sid = prefix + SecureRandom.hex(16)
  body["phone_number_sid"] = sid
end