Class: Mock::Bandwidth::Decorators::MessagingV2::MessageCreate

Inherits:
Object
  • Object
show all
Defined in:
lib/mock/bandwidth/decorators/messaging_v2/message_create.rb

Class Method Summary collapse

Class Method Details

.decorate(body, request) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mock/bandwidth/decorators/messaging_v2/message_create.rb', line 9

def decorate(body, request)
  data = JSON.parse request.request_body

  body["to"] = data["to"] if body["to"]
  body["from"] = data["from"] if body["from"]
  body["owner"] = data["from"] if body["owner"]
  body["text"] = data["text"] if body["text"]
  body["applicationId"] = data["applicationId"] if body["applicationId"]

  body["id"] = message_id(body) if body["id"]

  body.to_json
end

.message_id(body) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mock/bandwidth/decorators/messaging_v2/message_create.rb', line 23

def message_id(body)
  timestamp = (Time.now.to_f * 1000).to_i.to_s[0..12]
  random_part = SecureRandom.alphanumeric(15).downcase
  id = "#{timestamp}#{random_part}"
  scheduler = Rufus::Scheduler.new
  scheduler.in '2s' do
    begin
      Mock::Bandwidth::Webhooks::Messages.trigger(id, body)
    rescue  => e
      puts e
    end
  end
  id
end