Class: Mock::Bandwidth::Webhooks::Messages

Inherits:
Base
  • Object
show all
Defined in:
lib/mock/bandwidth/webhooks/messages.rb

Constant Summary

Constants inherited from Base

Base::DELAY

Class Method Summary collapse

Methods inherited from Base

headers, webhook_client

Class Method Details

.trigger(id, body) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mock/bandwidth/webhooks/messages.rb', line 7

def self.trigger(id, body)
  return if Mock::Bandwidth.disable_webhooks

  # Wait simulation from twilio
  sleep DELAY.sample

  url = Mock::Bandwidth.webhook_message_status_url

  params = {
    _json: [
      {
        time: Time.current.rfc2822,
        type: "message-delivered",
        to: body["to"],
        description: "Message delivered to carrier.",
        message: {
          id: id,
          owner: body["from"],
          applicationId: "12341234",
          time: Time.current.rfc2822,
          segmentCount: 1,
          direction: "out",
          to: [
            body["to"]
          ],
          from: body["from"],
          text: body["text"],
          tag: "",
        },
      }
    ],
  }.to_json

  response = webhook_client.new(url: url, params: params, headers: headers).post

  case response.code.to_i
  when 200..204
    response
  when 400..600
    raise Webhooks::RestError, Mock::Bandwidth::ErrorHandler.new(response).raise
  end
end