Class: Mock::Twilio::Decorators::Api2010::Messages

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

Constant Summary collapse

@@scheduler =
Rufus::Scheduler.new

Class Method Summary collapse

Class Method Details

.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/messages.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"]

  message_sid(body, request) if body["sid"]
  body["messaging_service_sid"] = request.data["MessagingServiceSid"] if request.data["MessagingServiceSid"]
  pagination(body) if body["available_phone_numbers"]

  body["to"] = request.data["To"] if request.data["To"]
  body["from"] = request.data["From"] if request.data["From"]

  body
end

.message_sid(body, request) ⇒ Object



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/messages.rb', line 36

def message_sid(body, request)
  prefix = request.data["MediaUrl"] ? "MM" : "SM"
  sid = prefix + SecureRandom.hex(16)
  callback_url = request.data["StatusCallback"] if request.data["StatusCallback"]
  @@scheduler.in '2s' do
    begin
      response = Mock::Twilio::Webhooks::Messages.trigger(sid, callback_url)

      if response.success? && request.data["Body"].downcase.include?("inbound")
        inbound_sid = prefix + SecureRandom.hex(16)
        Mock::Twilio::Webhooks::InboundMessages.trigger(inbound_sid, request.data)
      end
    rescue  => e
      puts e
    end
  end
  body["sid"] = sid
end

.pagination(body) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/mock/twilio/decorators/api_2010/messages.rb', line 28

def pagination(body)
  # Params returned in mock_server but not on real twilio request for the moment.
  # Not needed for us now.
  PAGES_KEYS.each do |key|
    body.delete(key) if body[key]
  end
end