Class: Cpaas::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/cpaas-sdk/resources/notification.rb

Overview

CPaaS notification helper methods

Class Method Summary collapse

Class Method Details

.parse(notification) ⇒ Object

Parse inbound sms notification received in webhook. It parses the notification and returns simplified version of the response.

Parameters:

  • notification (JSON)

    JSON received in the subscription webhook.



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
49
# File 'lib/cpaas-sdk/resources/notification.rb', line 14

def self.parse(notification)
  parsed_notification = convert_hash_keys(notification)
  top_level_key = parsed_notification.keys.first
  notification_obj = parsed_notification[top_level_key]

  case top_level_key
  when :outbound_sms_message_notification, :inbound_sms_message_notification
    message = notification_obj.dig(:outbound_sms_message).nil? ? notification_obj.dig(:inbound_sms_message) : notification_obj.dig(:outbound_sms_message)

    {
      notification_id: notification_obj.dig(:id),
      notification_date_time: notification_obj.dig(:date_time),
      type: types[top_level_key]
    }.merge(message)
  when :sms_subscription_cancellation_notification
    {
      subscription_id: id_from(notification_obj.dig(:link, 0, :href)),
      notification_id: notification_obj.dig(:id),
      notification_date_time: notification_obj.dig(:date_time),
      type: types[top_level_key]
    }
  when :sms_event_notification
    {
      notification_id: notification_obj.dig(:id),
      notification_date_time: notification_obj.dig(:date_time),
      message_id: id_from(notification_obj.dig(:link, 0, :href)),
      type: types[top_level_key],
      event_details: {
        description: notification_obj.dig(:event_description),
        type: notification_obj.dig(:event_type)
      }
    }
  else
    notification_obj
  end
end