Class: Istox::Messaging

Inherits:
Object
  • Object
show all
Defined in:
lib/istox/helpers/messaging.rb

Class Method Summary collapse

Class Method Details

.email(emails, subject:, content:, attachments: nil) ⇒ Object

email, email can be an array of multiple emails or just string of single email address sample attachments data

[{
  fileName: "filename.pdf",
  base64: "<Base64 of the file>"
}]

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/istox/helpers/messaging.rb', line 64

def email(emails, subject:, content:, attachments: nil)
  targets = []
  if emails.is_a?(Array)
    targets = emails
  else
    targets << emails
  end

  targets = targets.compact

  raise ArgumentError, 'Target emails cannot be empty or nil' if targets.empty?
  raise ArgumentError, 'Email subject and content cannot be empty' if content.blank? || subject.blank?

  ::Istox::Publisher.manual_publish(
    exchange: 'message',
    exchange_type: 'direct',
    routing_key: 'email.message',
    message: {
      type: 'SEND_EMAIL',
      data: {
        to: targets,
        subject: subject,
        body: content,
        attachments_json: attachments&.to_json
      }
    }
  )
end

.email_template(template_name, template_data_arr, attachments: nil) ⇒ Object

sample template data, it should be an array [

email: email,
sid: xxxxx,
istoxP1: auth.first_name,
<more other sample template attributes>: <other sample template data>,

] sample attachments data

[{
  fileName: "filename.zip",
  base64: "<Base64 of the file>"
}]

Raises:

  • (ArgumentError)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/istox/helpers/messaging.rb', line 105

def email_template(template_name, template_data_arr, attachments: nil)
  raise ArgumentError, 'Template name and template data arr cannot be empty' if template_name.blank? || template_data_arr.nil?

  return log.info 'Template data is empty, skipping this request' if template_data_arr.empty?

  ::Istox::Publisher.manual_publish(
    exchange: 'message',
    exchange_type: 'direct',
    routing_key: 'email.message',
    message: {
      type: 'SEND_EMAIL_TEMPLATE',
      data: {
        template_name: template_name,
        template_data_arr_json: process_template_data_arr_for_copy_email(template_data_arr).to_json,
        attachments_json: attachments&.to_json
      }
    }
  )
end

.process_template_data_arr_for_copy_email(template_data_arr) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/istox/helpers/messaging.rb', line 125

def process_template_data_arr_for_copy_email(template_data_arr)
  copy_email_data_arr = []
  template_data_arr.each do |email_data|
    next unless email_data[:copy_emails].present?

    email_data[:copy_emails].each do |ce|
      next unless ce.present?

      # uid and sid are reserved fields that should be not cloned
      copy_email_data = email_data.clone.except(:sid).except(:uid)

      # if it is just email to cc
      if ce.is_a? String
        copy_email_data[:email] = ce
      else
        # if it is a object hash
        # {
        #     email: xxxx,
        #     sid: xxxx,
        #     params: {
        #       istoxP1: xxxx,
        #       istoxP2: xxxx
        #     }
        # }
        next unless ce[:email].present?

        copy_email_data[:email] = ce[:email]
        copy_email_data[:sid] = ce[:sid]
        copy_email_data = copy_email_data.merge(ce[:params]) if ce[:params].present?
      end

      copy_email_data.delete(:copy_emails)
      copy_email_data_arr << copy_email_data
    end

    email_data.delete(:copy_emails)
  end
  template_data_arr.concat(copy_email_data_arr)
end

.push_notification(template_name, template_data_arr) ⇒ Object

Raises:

  • (ArgumentError)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/istox/helpers/messaging.rb', line 165

def push_notification(template_name, template_data_arr)
  raise ArgumentError, 'Template name and template data arr cannot be empty' if template_name.blank? || template_data_arr.nil?

  return log.info 'Template data is empty, skipping this request' if template_data_arr.empty?

  ::Istox::Publisher.manual_publish(
    exchange: 'message',
    exchange_type: 'direct',
    routing_key: 'push_notification.message',
    message: {
      type: 'SEND_PUSH_NOTIFICATION',
      data: {
        template_name: template_name,
        template_data_arr_json: template_data_arr.to_json
      }
    }
  )
end

.sms(numbers, content, subject: nil) ⇒ Object

sms, numbers can be an array of multiple phone numbers or just string of single phone number

Raises:

  • (ArgumentError)


5
6
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
# File 'lib/istox/helpers/messaging.rb', line 5

def sms(numbers, content, subject: nil)
  targets = []
  if numbers.is_a?(Array)
    targets = numbers
  else
    targets << numbers
  end

  targets = targets.compact

  raise ArgumentError, 'Target numbers cannot be empty or nil' if targets.empty?
  raise ArgumentError, 'SMS message content cannot be empty' if content.blank?

  ::Istox::Publisher.manual_publish(
    exchange: 'message',
    exchange_type: 'direct',
    routing_key: 'sms.message',
    message: {
      type: 'SEND_SMS',
      data: {
        to: targets,
        subject: subject || 'Message from ADDX',
        body: content
      }
    }
  )
end

.sms_template(template_name, template_data_arr) ⇒ Object

sample template data, it should be an array [

mobile: mobile,
istoxP1: auth.first_name,
<more other sample template attributes>: <other sample template data>,

]

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/istox/helpers/messaging.rb', line 39

def sms_template(template_name, template_data_arr)
  raise ArgumentError, 'Template name and template data arr cannot be empty' if template_name.blank? || template_data_arr.nil?

  return log.info 'Template data is empty, skipping this request' if template_data_arr.empty?

  ::Istox::Publisher.manual_publish(
    exchange: 'message',
    exchange_type: 'direct',
    routing_key: 'sms.message',
    message: {
      type: 'SEND_SMS_TEMPLATE',
      data: {
        template_name: template_name,
        template_data_arr_json: template_data_arr.to_json
      }
    }
  )
end