socketlabs-injectionapi 0.0.1.pre.Dev

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +50 -0
  3. data/.idea/inspectionProfiles/Project_Default.xml +7 -0
  4. data/.idea/misc.xml +7 -0
  5. data/.idea/modules.xml +8 -0
  6. data/.idea/socketlabs-ruby.iml +12 -0
  7. data/.idea/vcs.xml +7 -0
  8. data/.idea/workspace.xml +452 -0
  9. data/LICENSE.MD +21 -0
  10. data/README.MD +212 -0
  11. data/gemfile +3 -0
  12. data/gemfile.lock +17 -0
  13. data/lib/socketlabs/injectionapi/address_result.rb +41 -0
  14. data/lib/socketlabs/injectionapi/core/http_request.rb +135 -0
  15. data/lib/socketlabs/injectionapi/core/http_response.rb +27 -0
  16. data/lib/socketlabs/injectionapi/core/injection_request_factory.rb +300 -0
  17. data/lib/socketlabs/injectionapi/core/injection_response_parser.rb +130 -0
  18. data/lib/socketlabs/injectionapi/core/send_validator.rb +387 -0
  19. data/lib/socketlabs/injectionapi/core/serialization/address_json.rb +48 -0
  20. data/lib/socketlabs/injectionapi/core/serialization/attachment_json.rb +60 -0
  21. data/lib/socketlabs/injectionapi/core/serialization/custom_header_json.rb +40 -0
  22. data/lib/socketlabs/injectionapi/core/serialization/injection_request.rb +56 -0
  23. data/lib/socketlabs/injectionapi/core/serialization/injection_response_dto.rb +77 -0
  24. data/lib/socketlabs/injectionapi/core/serialization/merge_data_json.rb +103 -0
  25. data/lib/socketlabs/injectionapi/core/serialization/merge_field_json.rb +40 -0
  26. data/lib/socketlabs/injectionapi/core/serialization/message_json.rb +250 -0
  27. data/lib/socketlabs/injectionapi/core/serialization/message_result_dto.rb +66 -0
  28. data/lib/socketlabs/injectionapi/core/string_extension.rb +74 -0
  29. data/lib/socketlabs/injectionapi/message/attachment.rb +146 -0
  30. data/lib/socketlabs/injectionapi/message/basic_message.rb +136 -0
  31. data/lib/socketlabs/injectionapi/message/bulk_message.rb +103 -0
  32. data/lib/socketlabs/injectionapi/message/bulk_recipient.rb +87 -0
  33. data/lib/socketlabs/injectionapi/message/custom_header.rb +53 -0
  34. data/lib/socketlabs/injectionapi/message/email_address.rb +52 -0
  35. data/lib/socketlabs/injectionapi/message/merge_data.rb +50 -0
  36. data/lib/socketlabs/injectionapi/message/message_base.rb +167 -0
  37. data/lib/socketlabs/injectionapi/proxy.rb +29 -0
  38. data/lib/socketlabs/injectionapi/send_response.rb +63 -0
  39. data/lib/socketlabs/injectionapi/send_result.rb +318 -0
  40. data/lib/socketlabs/injectionapi/socketlabsclient.rb +123 -0
  41. data/lib/socketlabs/version.rb +5 -0
  42. data/lib/socketlabs-injectionapi.rb +27 -0
  43. data/socketlabs-injectionapi.gemspec +25 -0
  44. metadata +85 -0
@@ -0,0 +1,387 @@
1
+ require_relative '../message/message_base.rb'
2
+ require_relative '../message/basic_message.rb'
3
+ require_relative '../message/email_address.rb'
4
+ require_relative '../message/bulk_message.rb'
5
+ require_relative '../message/bulk_recipient.rb'
6
+ require_relative '../message/custom_header.rb'
7
+
8
+ module SocketLabs
9
+ module InjectionApi
10
+ module Core
11
+
12
+ # Used by the SocketLabsClient to conduct basic validation on the message before sending to the Injection API.
13
+ class SendValidator
14
+ include SocketLabs::InjectionApi
15
+ include SocketLabs::InjectionApi::Message
16
+
17
+ public
18
+ # Validate a basic email message before sending to the Injection API.
19
+ # @param [BasicMessage] message
20
+ # @return [SendResponse]
21
+ def validate_message(message)
22
+
23
+ result = SendResponse.new
24
+
25
+ if message.instance_of? BasicMessage
26
+ result = validate_basic_message(message)
27
+ end
28
+
29
+ if message.instance_of? BulkMessage
30
+ result = validate_bulk_message(message)
31
+ end
32
+
33
+ result
34
+
35
+ end
36
+
37
+ # Validate the ServerId and Api Key pair prior before sending to the Injection API.
38
+ # @param [Integer] server_id
39
+ # @param [String] api_key
40
+ # @return [SendResponse]
41
+ def validate_credentials(server_id, api_key)
42
+
43
+ if api_key.nil? || api_key.empty?
44
+ SendResponse.new(result=SendResult.enum["AuthenticationValidationFailed"])
45
+ end
46
+
47
+ if server_id.nil? || server_id.empty?
48
+ SendResponse.new(result=SendResult.enum["AuthenticationValidationFailed"])
49
+ end
50
+
51
+ SendResponse.new(result=SendResult.enum["Success"])
52
+ end
53
+
54
+ private
55
+ # Maximum recipient threshold
56
+ def maximum_recipients_per_message
57
+ 50
58
+ end
59
+
60
+ # Validate the required fields of a BasicMessage.
61
+ # Fields validated are Subject, From Address, Reply To (if set),
62
+ # Message Body, and Custom Headers (if set)
63
+ # @param [MessageBase] message
64
+ # @return [SendResult]
65
+ def validate_base_message(message)
66
+
67
+ unless has_subject(message)
68
+ SendResult.enum["MessageValidationEmptySubject"]
69
+ end
70
+ unless has_from_email_address(message)
71
+ SendResult.enum["EmailAddressValidationMissingFrom"]
72
+ end
73
+ unless message.from_email_address.is_valid
74
+ SendResult.enum["EmailAddressValidationInvalidFrom"]
75
+ end
76
+ unless has_valid_reply_to(message)
77
+ SendResult.enum["RecipientValidationInvalidReplyTo"]
78
+ end
79
+ unless has_message_body(message)
80
+ SendResult.enum["MessageValidationEmptyMessage"]
81
+ end
82
+ unless has_valid_custom_headers(message.custom_headers)
83
+ SendResult.enum["MessageValidationInvalidCustomHeaders"]
84
+ end
85
+
86
+ SendResult.enum["Success"]
87
+
88
+ end
89
+
90
+ # Check if the message has a subject
91
+ # @param [MessageBase] message
92
+ # @return [Boolean]
93
+ def has_subject(message)
94
+ !(message.subject.nil? || message.subject.empty?)
95
+ end
96
+
97
+ # Check if the message has a valid From Email Address
98
+ # @param [MessageBase] message
99
+ # @return [Boolean]
100
+ def has_message_body(message)
101
+
102
+ if has_api_template(message)
103
+ true
104
+ end
105
+
106
+ has_html_body = !(message.html_body.nil? || message.html_body.empty?)
107
+ has_plain_text_body = !(message.plain_text_body.nil? || message.plain_text_body.empty?)
108
+
109
+ has_html_body || has_plain_text_body
110
+
111
+ end
112
+
113
+ # Check if an ApiTemplate was specified and is valid
114
+ # @param [MessageBase] message
115
+ # @return [Boolean]
116
+ def has_api_template(message)
117
+ !(message.api_template.nil?)
118
+ end
119
+
120
+ # Check if the message has a valid From Email Address
121
+ # @param [MessageBase] message
122
+ # @return [Boolean]
123
+ def has_from_email_address(message)
124
+
125
+ if message.from_email_address.nil?
126
+ false
127
+ end
128
+
129
+ !(message.from_email_address.email_address.nil? || message.from_email_address.email_address.empty?)
130
+
131
+ end
132
+
133
+ # Check if reply_to_email_address is a valid to email address
134
+ # @param [MessageBase] message
135
+ # @return [Boolean]
136
+ def has_valid_reply_to(message)
137
+
138
+ unless message.reply_to_email_address.nil?
139
+ message.reply_to_email_address.is_valid
140
+ end
141
+
142
+ false
143
+
144
+ end
145
+
146
+ # Validate email recipients for a basic message
147
+ # Checks the To, Cc, and the Bcc EmailAddress lists for the following:
148
+ # > At least 1 recipient is in the list.
149
+ # > Cumulative count of recipients in all 3 lists do not exceed the MaximumRecipientsPerMessage
150
+ # > Recipients in lists are valid.
151
+ # If errors are found, the SendResponse will contain the invalid email addresses
152
+ # @param [BasicMessage] message
153
+ # @return [Boolean]
154
+ def validate_email_addresses(message)
155
+
156
+ rec_count = get_full_recipient_count(message)
157
+ if rec_count <= 0
158
+ SendResponse.new(result=SendResult.enum["RecipientValidationNoneInMessage"])
159
+ end
160
+ if rec_count > maximum_recipients_per_message
161
+ SendResponse.new(result=SendResult.enum["RecipientValidationMaxExceeded"])
162
+ end
163
+ invalid_rec = has_invalid_email_addresses(message)
164
+ unless invalid_rec.nil? || invalid_rec.empty?
165
+ SendResponse.new(result=SendResult.enum["RecipientValidationInvalidRecipients"], address_results=invalid_rec)
166
+ end
167
+
168
+ SendResponse.new(result=SendResult.enum["Success"])
169
+
170
+ end
171
+
172
+ # Validate email recipients for a bulk message
173
+ # Checks the To recipient lists for the following:
174
+ # > At least 1 recipient is in the list.
175
+ # > Cumulative count of recipients in all 3 lists do not exceed the MaximumRecipientsPerMessage
176
+ # > Recipients in lists are valid.
177
+ # If errors are found, the SendResponse will contain the invalid email addresses
178
+ # @param [BulkMessage] message
179
+ # @return [Boolean]
180
+ def validate_recipients(message)
181
+
182
+ if message.to_recipient.nil? || message.to_recipient.empty?
183
+ SendResponse.new(result=SendResult.enum["RecipientValidationMissingTo"])
184
+ end
185
+ if message.to_recipient.length > maximum_recipients_per_message
186
+ SendResponse.new(result=SendResult.enum["RecipientValidationMaxExceeded"])
187
+ end
188
+ invalid_rec = has_invalid_recipients(message)
189
+ unless invalid_rec.nil? || invalid_rec.empty?
190
+ SendResponse.new(result=SendResult.enum["RecipientValidationInvalidRecipients"], address_results=invalid_rec)
191
+ end
192
+
193
+ SendResponse.new(result=SendResult.enum["Success"])
194
+ end
195
+
196
+ # Check all 3 EmailAddress lists To, Cc, and Bcc for valid email addresses
197
+ # @param [BasicMessage] message
198
+ # @return [Boolean]
199
+ def has_invalid_email_addresses(message)
200
+
201
+ invalid = Array.new
202
+
203
+ invalid_to = find_invalid_email_addresses(message.to_email_address)
204
+ unless invalid_to.nil? || invalid_to.empty?
205
+ invalid.push(*invalid_to)
206
+ end
207
+
208
+ invalid_cc = find_invalid_email_addresses(message.cc_email_address)
209
+ unless invalid_cc.nil? || invalid_cc.empty?
210
+ invalid.push(*invalid_cc)
211
+ end
212
+
213
+ invalid_bcc = find_invalid_email_addresses(message.bcc_email_address)
214
+ unless invalid_bcc.nil? || invalid_bcc.empty?
215
+ invalid.push(*invalid_bcc)
216
+ end
217
+
218
+ if invalid.length > 0
219
+ invalid
220
+ end
221
+
222
+ nil
223
+
224
+ end
225
+
226
+ # Check the To recipient list for valid email addresses
227
+ # @param [BasicMessage] message
228
+ # @return [Boolean]
229
+ def has_invalid_recipients(message)
230
+
231
+ invalid = Array.new
232
+
233
+ invalid_to = find_invalid_recipients(message.to_recipient)
234
+ unless invalid_to.nil? || invalid_to.empty?
235
+ invalid.push(*invalid_to)
236
+ end
237
+
238
+ if invalid.length > 0
239
+ invalid
240
+ end
241
+
242
+ nil
243
+
244
+ end
245
+
246
+ # Check the list of EmailAddress for valid email addresses
247
+ # @param [Array] email_addresses
248
+ # @return [Array]
249
+ def find_invalid_email_addresses(email_addresses)
250
+
251
+ invalid = Array.new
252
+
253
+ unless email_addresses.nil? || email_addresses.empty?
254
+ email_addresses.each do |email|
255
+ if email.instance_of? EmailAddress
256
+ unless email.is_valid
257
+ invalid.push(AddressResult.new(email.email_address, false, "InvalidAddress"))
258
+ end
259
+
260
+ elsif email.instance_of? String
261
+ str_email = EmailAddress.new(email)
262
+ unless str_email.is_valid
263
+ invalid.push(AddressResult.new(str_email.email_address, false, "InvalidAddress"))
264
+ end
265
+
266
+ elsif email.instance_of? Hash
267
+ hash_email = EmailAddress.new(email[:email_address], email[:friendly_name])
268
+ unless hash_email.is_valid
269
+ invalid.push(AddressResult.new(hash_email.email_address, false, "InvalidAddress"))
270
+ end
271
+ end
272
+ end
273
+ end
274
+
275
+ invalid
276
+
277
+ end
278
+
279
+ # Check the list of BulkRecipient for valid email addresses
280
+ # @param [Array] recipients
281
+ # @return [Array]
282
+ def find_invalid_recipients(recipients)
283
+
284
+ invalid = Array.new
285
+
286
+ unless recipients.nil? || recipients.empty?
287
+ recipients.each do |email|
288
+
289
+ if email.instance_of? BulkRecipient
290
+ unless email.is_valid
291
+ invalid.push(AddressResult.new(email.email_address, false, "InvalidAddress"))
292
+ end
293
+
294
+ elsif email.instance_of? String
295
+ str_email = BulkRecipient.new(email)
296
+ unless str_email.is_valid
297
+ invalid.push(AddressResult.new(str_email.email_address, false, "InvalidAddress"))
298
+ end
299
+
300
+ elsif email.instance_of? Hash
301
+ hash_email = BulkRecipient.new(email[:email_address], { :friendly_name => email[:friendly_name], :merge_data => email[:merge_data] })
302
+ unless hash_email.is_valid
303
+ invalid.push(AddressResult.new(hash_email.email_address, false, "InvalidAddress"))
304
+ end
305
+ end
306
+
307
+ end
308
+ end
309
+
310
+ invalid
311
+
312
+ end
313
+
314
+ # Cumulative count of email addresses in all 3 EmailAddress lists To, Cc, and Bcc
315
+ # @param [BasicMessage] message
316
+ # @return [Integer]
317
+ def get_full_recipient_count(message)
318
+
319
+ recipient_count = 0
320
+ unless message.to_email_address.nil? || message.to_email_address.empty?
321
+ recipient_count += message.to_email_address.length
322
+ end
323
+
324
+ unless message.cc_email_address.nil? || message.cc_email_address.empty?
325
+ recipient_count += message.cc_email_address.length
326
+ end
327
+
328
+ unless message.bcc_email_address.nil? || message.bcc_email_address.empty?
329
+ recipient_count += message.bcc_email_address.length
330
+ end
331
+ recipient_count
332
+
333
+ end
334
+
335
+ # Check if the list of custom header is valid
336
+ # @param [Array] custom_headers
337
+ # @return [Array]
338
+ def has_valid_custom_headers(custom_headers)
339
+
340
+ unless custom_headers.nil? || custom_headers.empty?
341
+ custom_headers.each do |item|
342
+ if item.instance_of? CustomHeader
343
+ unless item.is_valid
344
+ false
345
+ end
346
+ end
347
+ end
348
+ end
349
+
350
+ true
351
+
352
+ end
353
+
354
+ # @param [BasicMessage] message
355
+ # @return [SendResponse]
356
+ def validate_basic_message(message)
357
+
358
+ valid_base = validate_base_message(message)
359
+ if valid_base == SendResult.enum["Success"]
360
+ result = validate_email_addresses(message)
361
+ else
362
+ result = SendResponse.new(result=valid_base)
363
+ end
364
+
365
+ result
366
+
367
+ end
368
+
369
+ # @param [BasicMessage] message
370
+ # @return [SendResponse]
371
+ def validate_bulk_message(message)
372
+
373
+ valid_base = validate_base_message(message)
374
+ if valid_base == SendResult.enum["Success"]
375
+ result = validate_recipients(message)
376
+ else
377
+ result = SendResponse.new(result=valid_base)
378
+ end
379
+
380
+ result
381
+
382
+ end
383
+
384
+ end
385
+ end
386
+ end
387
+ end
@@ -0,0 +1,48 @@
1
+ require_relative '../../core/string_extension.rb'
2
+
3
+ module SocketLabs
4
+ module InjectionApi
5
+ module Core
6
+ module Serialization
7
+
8
+ # Represents an individual email address for a message.
9
+ # To be serialized into JSON string before sending to the Injection Api.
10
+ class AddressJson
11
+
12
+ # the email address
13
+ attr_accessor :email_address
14
+ #the friendly or display name
15
+ attr_accessor :friendly_name
16
+
17
+ # Initializes a new instance of the AddressJson class
18
+ # @param [String] email_address
19
+ # @param [String] friendly_name
20
+ def initialize(
21
+ email_address = nil,
22
+ friendly_name = nil
23
+ )
24
+ @email_address = email_address
25
+ @friendly_name = friendly_name
26
+ end
27
+
28
+ # build json hash for AddressJson
29
+ # @return [hash]
30
+ def to_hash
31
+ if @friendly_name.nil? || @friendly_name.empty?
32
+ {
33
+ :emailAddress => @email_address
34
+ }
35
+ else
36
+ {
37
+ :emailAddress => @email_address,
38
+ :friendlyName => @friendly_name
39
+ }
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,60 @@
1
+ require_relative '../../core/string_extension.rb'
2
+
3
+ module SocketLabs
4
+ module InjectionApi
5
+ module Core
6
+ module Serialization
7
+
8
+ # Represents a message attachment in the form of a byte array.
9
+ # To be serialized into JSON string before sending to the Injection Api.
10
+
11
+ class AttachmentJson
12
+
13
+ # the name of attachment
14
+ attr_accessor :name
15
+ # the MIME type of the attachment.
16
+ attr_accessor :mime_type
17
+ # ContentId for an Attachment.
18
+ attr_accessor :content_id
19
+ # Content of an Attachment. The BASE64 encoded str.
20
+ attr_accessor :content
21
+ # the list of custom headers added to the attachment.
22
+ attr_accessor :custom_headers
23
+
24
+ # Initializes a new instance of the AttachmentJson class
25
+ def initialize
26
+ @name = nil
27
+ @mime_type = nil
28
+ @content_id = nil
29
+ @content = nil
30
+ @custom_headers = Array.new
31
+ end
32
+
33
+ # build json hash for AttachmentJson
34
+ # @return [hash]
35
+ def to_hash
36
+ json =
37
+ {
38
+ :name=> @name,
39
+ :content=> @content,
40
+ :contentType=> @mime_type
41
+ }
42
+ unless @content_id.nil? || @content_id.empty?
43
+ json[:contentId] = @content_id
44
+ end
45
+ unless @custom_headers.nil? || @custom_headers.length == 0
46
+ e = Array.new
47
+ @custom_headers.each do |value|
48
+ e.push(value.to_hash)
49
+ end
50
+ json[:customHeaders] = e
51
+ end
52
+ json
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,40 @@
1
+ module SocketLabs
2
+ module InjectionApi
3
+ module Core
4
+ module Serialization
5
+
6
+ # Represents a custom header as a name and value pair.
7
+ # To be serialized into JSON string before sending to the Injection Api.
8
+ class CustomHeaderJson
9
+
10
+ # name of the custom header.
11
+ attr_accessor :name
12
+ # value of the custom header.
13
+ attr_accessor :value
14
+
15
+ # Initializes a new instance of the CustomHeaderJson class
16
+ # @param [String] name
17
+ # @param [String] value
18
+ def initialize(
19
+ name = nil,
20
+ value = nil
21
+ )
22
+ @name = name
23
+ @value = value
24
+ end
25
+
26
+ # build json hash for CustomHeaderJson
27
+ # @return [hash]
28
+ def to_hash
29
+ {
30
+ :name => @name,
31
+ :value => @value
32
+ }
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,56 @@
1
+ module SocketLabs
2
+ module InjectionApi
3
+ module Core
4
+ module Serialization
5
+
6
+ # Represents a injection request for sending to the Injection Api.
7
+ # To be serialized into JSON string before sending to the Injection Api.
8
+ class InjectionRequest
9
+
10
+ # the SocketLabs Injection API key for the Injection Request.
11
+ attr_accessor :api_key
12
+ # the server id for the injection Request.
13
+ attr_accessor :server_id
14
+ # the list of messages (MessageJson) to send. This library is limited to one
15
+ attr_accessor :messages
16
+
17
+ # Initializes a new instance of the InjectionRequest class
18
+ # @param [String] api_key
19
+ # @param [String] server_id
20
+ # @param [String] messages
21
+ def initialize(
22
+ server_id = nil,
23
+ api_key = nil,
24
+ messages = nil
25
+ )
26
+ @api_key = api_key
27
+ @server_id = server_id
28
+ @messages = messages
29
+ end
30
+
31
+ # build json hash for InjectionRequest
32
+ # @return [hash]
33
+ def to_hash
34
+
35
+ json = {
36
+ :serverId => @server_id,
37
+ :apiKey => @api_key
38
+ }
39
+
40
+ if @messages.length > 0
41
+ e = Array.new
42
+ @messages.each do |value|
43
+ e.push(value.to_hash)
44
+ end
45
+ json[:messages] = e
46
+ end
47
+
48
+ json
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,77 @@
1
+ module SocketLabs
2
+ module InjectionApi
3
+ module Core
4
+ module Serialization
5
+
6
+ # Represents an individual email address for a message.
7
+ # To be serialized into JSON string before sending to the Injection Api.
8
+ class InjectionResponseDto
9
+
10
+ # the response ErrorCode of the Injection Api send request
11
+ attr_accessor :error_code
12
+ # the transaction receipt of the Injection Api send request
13
+ attr_accessor :transaction_receipt
14
+
15
+ # Initializes a new instance of the AddressJson class
16
+ # @param [String] error_code
17
+ # @param [String] transaction_receipt
18
+ # @param [Array] message_results
19
+ def initialize(
20
+ error_code = nil,
21
+ transaction_receipt = nil,
22
+ message_results = nil
23
+ )
24
+
25
+ @error_code = error_code
26
+ @transaction_receipt = transaction_receipt
27
+ @message_results = message_results
28
+
29
+ end
30
+
31
+ # Get the array of MessageResultDto objects that contain the status of each message sent.
32
+ # @return [Array]
33
+ def message_results
34
+ @message_results
35
+ end
36
+
37
+ # Set the array of MessageResultDto objects that contain the status of each message sent.
38
+ # @param [Array] value
39
+ def message_results=(value)
40
+ @message_results = Array.new
41
+
42
+ unless value.nil? || value.empty?
43
+ value.each do |v1|
44
+ if v1.instance_of? MessageResultDto
45
+ @message_results.push(v1)
46
+ end
47
+ end
48
+
49
+ end
50
+ end
51
+
52
+ # build json hash for InjectionResponseDto
53
+ # @return [hash]
54
+ def to_hash
55
+
56
+ json = {
57
+ :errorCode => @server_id,
58
+ :transactionReceipt => @api_key
59
+ }
60
+
61
+ if @message_results.length > 0
62
+ e = Array.new
63
+ @message_results.each do |value|
64
+ e.push(value.to_hash)
65
+ end
66
+ json[:messageResult] = e
67
+ end
68
+
69
+ json
70
+ end
71
+
72
+ end
73
+
74
+ end
75
+ end
76
+ end
77
+ end