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,300 @@
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
+ require_relative 'serialization/address_json.rb'
9
+ require_relative 'serialization/attachment_json.rb'
10
+ require_relative 'serialization/custom_header_json.rb'
11
+ require_relative 'serialization/message_json.rb'
12
+ require_relative 'serialization/merge_data_json.rb'
13
+
14
+ module SocketLabs
15
+ module InjectionApi
16
+ module Core
17
+ class InjectionRequestFactory
18
+ include SocketLabs::InjectionApi
19
+ include SocketLabs::InjectionApi::Core
20
+ include SocketLabs::InjectionApi::Message
21
+ include SocketLabs::InjectionApi::Core::Serialization
22
+
23
+ attr_reader :server_id
24
+ attr_reader :api_key
25
+
26
+ # Creates a new instance of the InjectionRequestFactory.
27
+ # @param [String] server_id
28
+ # @return [String] api_key
29
+ def initialize(server_id, api_key)
30
+
31
+ @server_id = server_id
32
+ @api_key = api_key
33
+
34
+ end
35
+
36
+ # Generate the InjectionRequest for sending to the Injection Api.
37
+ # @param [BasicMessage, BulkMessage] message: the message object to convert
38
+ # @return [InjectionRequest] the converted InjectionRequest
39
+ def generate_request(message)
40
+
41
+ request = InjectionRequest.new
42
+
43
+ if message.instance_of? BasicMessage
44
+ request = generate_basic_message_request(message)
45
+ end
46
+
47
+ if message.instance_of? BulkMessage
48
+ request = generate_bulk_message_request(message)
49
+ end
50
+
51
+ request
52
+
53
+ end
54
+
55
+ private
56
+ # Converts MessageBase object to a MessageJson object
57
+ # @param [MessageBase] message: the message to convert
58
+ # @return [MessageJson] the convert MessageJson object
59
+ def generate_base_message(message)
60
+
61
+ message_json = MessageJson.new
62
+ message_json.subject = message.subject
63
+ message_json.plain_text_body = message.plain_text_body
64
+ message_json.html_body = message.html_body
65
+ message_json.mailing_id = message.mailing_id
66
+ message_json.message_id = message.message_id
67
+ message_json.charset = message.charset
68
+ message_json.from_email_address = email_address_to_address_json(message.from_email_address)
69
+ message_json.custom_headers = populate_custom_headers(message.custom_headers)
70
+ message_json.attachments = populate_attachments(message.attachments)
71
+
72
+ unless message.api_template.nil?
73
+ message_json.api_template = message.api_template
74
+ end
75
+ unless message.reply_to_email_address.nil?
76
+ message_json.reply_to = email_address_to_address_json(message.reply_to_email_address)
77
+ end
78
+ message_json
79
+
80
+ end
81
+
82
+ # Converts a list of Attachment objects to a List of AttachmentJson objects.
83
+ # @param [Array] custom_headers: list of CustomHeader to convert
84
+ # @return [Array] the converted list of CustomHeaderJson
85
+ def populate_custom_headers(custom_headers)
86
+
87
+ if custom_headers.nil? || custom_headers.empty?
88
+ nil
89
+ end
90
+
91
+ headers_json = Array.new
92
+
93
+ custom_headers.each do |header|
94
+ if header.instance_of? CustomHeader
95
+ headers_json.push(CustomHeaderJson.new(header.name, header.value))
96
+ elsif header.instance_of? Hash
97
+ headers_json.push(CustomHeaderJson.new(header[:name], header[:value]))
98
+ end
99
+ end
100
+
101
+ headers_json
102
+
103
+ end
104
+
105
+ # Converts a list of Attachment objects to a List of AttachmentJson objects.
106
+ # @param [Attachment] attachments: list of Attachment to convert
107
+ # @return [Array] the converted list of AttachmentJson
108
+ def populate_attachments(attachments)
109
+
110
+ if attachments.nil? || attachments.empty?
111
+ nil
112
+ end
113
+
114
+ attachments_json = Array.new
115
+
116
+ attachments.each do |item|
117
+ if item.instance_of? Attachment
118
+
119
+ at_json = AttachmentJson.new
120
+ at_json.name = item.name
121
+ at_json.name = item.name
122
+ at_json.mime_type = item.mime_type
123
+ at_json.content_id = item.content_id
124
+ at_json.content = item.content
125
+ at_json.custom_headers = populate_custom_headers(item.custom_headers)
126
+
127
+ attachments_json.push(at_json)
128
+ end
129
+ end
130
+
131
+ attachments_json
132
+
133
+ end
134
+
135
+
136
+ # Generate the InjectionRequest for sending to the Injection Api.
137
+ # @param [BasicMessage] message: the basic message object to convert
138
+ # @return [InjectionRequest] the converted InjectionRequest
139
+ def generate_basic_message_request(message)
140
+
141
+ message_json = generate_base_message(message)
142
+
143
+ unless message.to_email_address.nil? || message.to_email_address.empty?
144
+ message_json.to_email_address = populate_email_list(message.to_email_address)
145
+ end
146
+
147
+ unless message.cc_email_address.nil? || message.cc_email_address.empty?
148
+ message_json.cc_email_address = populate_email_list(message.cc_email_address)
149
+ end
150
+
151
+ unless message.bcc_email_address.nil? || message.bcc_email_address.empty?
152
+ message_json.bcc_email_address = populate_email_list(message.bcc_email_address)
153
+ end
154
+
155
+ messages_json = []
156
+ messages_json.push(message_json)
157
+
158
+ InjectionRequest.new(@server_id, @api_key, messages_json)
159
+
160
+ end
161
+
162
+ # Converts a List of EmailAddress objects to a List of AddressJson objects.
163
+ # @param [EmailAddress] addresses: list of EmailAddress to convert
164
+ # @return [Array] of AddressJson - the converted list of AddressJson
165
+ def populate_email_list(addresses)
166
+
167
+ if addresses.nil? || addresses.empty?
168
+ nil
169
+ end
170
+
171
+ addresses_json = Array.new
172
+
173
+ unless addresses.nil? || addresses.empty?
174
+ addresses.each do |email|
175
+ if email.instance_of? EmailAddress
176
+ addresses_json.push(email_address_to_address_json(email))
177
+
178
+ elsif email.instance_of? String
179
+ str_email = EmailAddress.new(email)
180
+ addresses_json.push(email_address_to_address_json(str_email))
181
+
182
+ elsif email.instance_of? Hash
183
+ hash_email = EmailAddress.new(email[:email_address], email[:friendly_name])
184
+ addresses_json.push(email_address_to_address_json(hash_email))
185
+ end
186
+ end
187
+ end
188
+
189
+ addresses_json
190
+
191
+ end
192
+
193
+ # Simple converter from EmailAddress to AddressJson
194
+ # @param [EmailAddress] address: the EmailAddress object to convert
195
+ # @return [AddressJson] the converted AddressJson object
196
+ def email_address_to_address_json(address)
197
+
198
+ unless address.friendly_name.nil? || address.friendly_name.empty?
199
+ AddressJson.new(address.email_address, address.friendly_name)
200
+ else
201
+ AddressJson.new(address.email_address)
202
+ end
203
+
204
+ end
205
+
206
+ # Generate the InjectionRequest for sending to the Injection Api.
207
+ # @param [BulkMessage] message: the bulk message object to convert
208
+ # @return [InjectionRequest] the converted InjectionRequest
209
+ def generate_bulk_message_request(message)
210
+
211
+ message_json = generate_base_message(message)
212
+ message_json.to_email_address.push(AddressJson.new("%%DeliveryAddress%%", "%%RecipientName%%"))
213
+ message_json.merge_data = populate_merge_data(message.global_merge_data, message.to_recipient)
214
+
215
+ messages_json = []
216
+ messages_json.push(message_json)
217
+
218
+ InjectionRequest.new(@server_id, @api_key, messages_json)
219
+
220
+ end
221
+
222
+ # Simple converter from BulkRecipient to AddressJson
223
+ # @param [BulkRecipient] address: the BulkRecipient object to convert
224
+ # @return [AddressJson] the converted AddressJson object
225
+ def bulk_recipient_to_address_json(address)
226
+
227
+ if address.friendly_name.nil? || address.friendly_name.empty?
228
+ AddressJson.new(address.email_address)
229
+ else
230
+ AddressJson.new(address.email_address, address.friendly_name)
231
+ end
232
+
233
+ end
234
+
235
+ # Populates global merge data and per message merge data to a MergeDataJson.
236
+ # @param [Array] global_md: array of global merge data to convert
237
+ # @param [Array] recipients: list of BulkRecipients to convert
238
+ # @return [MergeDataJson] the converted MergeDataJson object
239
+ def populate_merge_data(global_md, recipients)
240
+
241
+ per_message_mf = Array.new
242
+ global_mf = generate_merge_field_list(global_md)
243
+
244
+ unless recipients.nil? || recipients.empty?
245
+ recipients.each do |email|
246
+
247
+ if email.instance_of? BulkRecipient
248
+ recipient = email
249
+
250
+ elsif email.instance_of? String
251
+ recipient = BulkRecipient.new(email)
252
+
253
+ elsif email.instance_of? Hash
254
+ recipient = BulkRecipient.new(email[:email_address], { :friendly_name => email[:friendly_name], :merge_data => email[:merge_data] })
255
+
256
+ end
257
+
258
+ merge_field_json = generate_merge_field_list(recipient.merge_data)
259
+ merge_field_json.push(MergeFieldJson.new("DeliveryAddress", recipient.email_address))
260
+
261
+ unless recipient.friendly_name.nil? || recipient.friendly_name.empty?
262
+ merge_field_json.push(MergeFieldJson.new("RecipientName", recipient.friendly_name))
263
+ end
264
+
265
+ per_message_mf.push(merge_field_json)
266
+
267
+ end
268
+ end
269
+
270
+ MergeDataJson.new(per_message_mf, global_mf)
271
+
272
+ end
273
+
274
+ # Converts an Array of merge data into a List of MergeFieldJson objects.
275
+ # @param [Array] merge_data: Array to convert
276
+ # @return [Array] the converted list of MergeFieldJson
277
+ def generate_merge_field_list(merge_data)
278
+
279
+ merge_field_json = Array.new
280
+
281
+ merge_data.each do |item|
282
+
283
+ if item.instance_of? MergeData
284
+ json = MergeFieldJson.new(item.key, item.value)
285
+ merge_field_json.push(json)
286
+
287
+ elsif item.instance_of? Hash
288
+ json = MergeFieldJson.new(item[:key], item[:value])
289
+ merge_field_json.push(json)
290
+ end
291
+ end
292
+
293
+ merge_field_json
294
+
295
+ end
296
+
297
+ end
298
+ end
299
+ end
300
+ end
@@ -0,0 +1,130 @@
1
+
2
+ require_relative 'serialization/injection_response_dto.rb'
3
+ require_relative 'serialization/message_result_dto.rb'
4
+
5
+ module SocketLabs
6
+ module InjectionApi
7
+ module Core
8
+
9
+ class InjectionResponseParser
10
+ include SocketLabs::InjectionApi
11
+ include SocketLabs::InjectionApi::Message
12
+ include SocketLabs::InjectionApi::Core::Serialization
13
+
14
+ # Parse the response from the Injection Api into SendResponse
15
+ # @param [HttpResponse] response: the response form the Injection Api
16
+ # @return [SendResponse]
17
+ def parse(response)
18
+
19
+ injection_response = get_injection_response_dto(response)
20
+ result_enum = determine_send_result(injection_response, response)
21
+ new_response = SendResponse.new(result_enum)
22
+ new_response.transaction_receipt = injection_response.transaction_receipt
23
+
24
+ if result_enum == SendResult.enum["Warning"]
25
+ unless injection_response.message_results.nil? || injection_response.message_results.length == 0
26
+ error_code = injection_response.message_results[0].error_code
27
+ result_enum = SendResult.enum[error_code]
28
+ new_response.result = result_enum
29
+ end
30
+ end
31
+
32
+ unless injection_response.message_results.nil? || injection_response.message_results.length == 0
33
+ new_response.address_results = injection_response.message_results[0].address_results
34
+ end
35
+
36
+ new_response
37
+
38
+ end
39
+
40
+ # Get the InjectionResponseDto from the HttpResponse object
41
+ # @param [HttpResponse] response: the Http response
42
+ # @return [InjectionResponseDto] the converted injection response dto
43
+ def get_injection_response_dto(response)
44
+
45
+ hash_body = response.to_hash
46
+
47
+ resp_dto = InjectionResponseDto.new
48
+
49
+ if hash_body.key?(:ErrorCode)
50
+ resp_dto.error_code = SendResult.enum[hash_body[:ErrorCode]]
51
+ end
52
+
53
+ if hash_body.key?(:TransactionReceipt)
54
+ resp_dto.transaction_receipt = hash_body[:TransactionReceipt]
55
+ end
56
+
57
+ if hash_body.key?(:MessageResults)
58
+
59
+ resp_dto.message_results = Array.new
60
+ message_results = hash_body[:MessageResults]
61
+
62
+ unless message_results.nil?
63
+ message_results.each do |item|
64
+ message_dto = MessageResultDto.new
65
+
66
+ if item.key?(:Index)
67
+ message_dto.index = item[:Index]
68
+ end
69
+
70
+ if item.key?(:AddressResults)
71
+ address_results = item[:AddressResults]
72
+ unless address_results.nil?
73
+ address_results.each do |aitem|
74
+ message_dto.address_results.push(SocketLabs::InjectionApi::AddressResult.new(aitem[:EmailAddress], aitem[:Accepted], aitem[:ErrorCode]))
75
+ end
76
+ end
77
+ end
78
+
79
+ if item.key?(:ErrorCode)
80
+ message_dto.error_code = item[:ErrorCode]
81
+ end
82
+
83
+ resp_dto.message_results.push(message_dto)
84
+
85
+ end
86
+ end
87
+
88
+ resp_dto
89
+
90
+ end
91
+
92
+
93
+ end
94
+
95
+ # Enumerated SendResult of the payload response from the Injection Api
96
+ # @param [InjectionResponseDto] response_dto: the response dto
97
+ # @param [HttpResponse] response: the Http response
98
+ # @return [SendResult] the parsed result of the injection request
99
+ def determine_send_result(response_dto, response)
100
+
101
+ # HttpStatusCode.OK
102
+ if response.status_code == "200"
103
+ result_enum = response_dto.error_code
104
+ if result_enum.nil? || result_enum.empty?
105
+ result_enum = SendResult.enum["UnknownError"]
106
+ end
107
+
108
+ # HttpStatusCode.Unauthorized
109
+ elsif response.status_code == "500"
110
+ result_enum = SendResult.enum["InternalError"]
111
+
112
+ # HttpStatusCode.Unauthorized
113
+ elsif response.status_code == "408"
114
+ result_enum = SendResult.enum["Timeout"]
115
+
116
+ # HttpStatusCode.Unauthorized
117
+ else
118
+ result_enum = SendResult.enum["InvalidAuthentication"]
119
+
120
+ end
121
+
122
+ result_enum
123
+
124
+ end
125
+
126
+ end
127
+
128
+ end
129
+ end
130
+ end