socketlabs-injectionapi 1.1.0 → 1.4.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6836ca1a20d067ff9b472ed43b5cc27355216e03f67ceba0afbfe658581b6aca
4
- data.tar.gz: 370b18591dc5cc3bc1ee4c30f42871d7ec7c53423ec68c15136e36a3a353ebfc
3
+ metadata.gz: 166ba13c298ad93c8c2ea44c9a2af22399a3a00c5bde303e7daabfd080cd0563
4
+ data.tar.gz: b1fcab9c9778c858306c8dfa5bc3f117a043df81383207878c2c4cb566196ec6
5
5
  SHA512:
6
- metadata.gz: 485ce4f186821ae6faf881e8931e5f7583d6e6131c754c4aabb8a4c57e938110a860bbe31a0bb21fe00363a60fe1041ea72f59dc86e4e7c65f37f1466869626b
7
- data.tar.gz: 63b65f20755c0f31d1c3208c6007ddf57164b811e0b7d515abcc9ca2944781370f9bf294fd5455237105184a2f1293f22f5558de1057f4725c0fdbbc2baab66c
6
+ metadata.gz: 9b793d816754372596a2c8681bb6d5ec226d4a06a4b6985d0ebe801883adbbdeab1c2973a666500c95b642639084c24dd03e6e524cc0b45e4424eb15c548053d
7
+ data.tar.gz: 3700a0da0d4a12a79446dd45fcf63043909cc454efc6963341164a5cfdf55e6b33f2c5337540cb1b9035dbcaa4ba15f3e79e93b2352e07d16285e9b5eaffef04
data/.gitignore CHANGED
@@ -49,3 +49,5 @@ build-iPhoneSimulator/
49
49
 
50
50
  # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
51
51
  .rvmrc
52
+
53
+ .idea
data/README.MD CHANGED
@@ -14,7 +14,7 @@ The SocketLabs Email Delivery Ruby library allows you to easily send email messa
14
14
  <a name="prerequisites-and-installation" id="prerequisites-and-installation"></a>
15
15
  # Prerequisites and Installation
16
16
  ## Prerequisites
17
- * A supported Ruby version (2.4 and above)
17
+ * A supported Ruby version (3.1 and above)
18
18
  * A SocketLabs account. If you don't have one yet, you can [sign up for a free account](https://signup.socketlabs.com/step-1?plan=free) to get started.
19
19
 
20
20
  ## Installation
@@ -175,6 +175,9 @@ This example demonstrates how to embed an image in your message.
175
175
  ### [Basic send with a web proxy](https://github.com/socketlabs/socketlabs-ruby/blob/master/examples/basic/basic_send_with_proxy.rb)
176
176
  This example demonstrates how to use a proxy with your HTTP client.
177
177
 
178
+ ### [Basic send with retry enabled](https://github.com/socketlabs/socketlabs-ruby/blob/master/examples/basic/basic_send_with_retry.rb)
179
+ This example demonstrates how to use the retry logic with your HTTP client.
180
+
178
181
  ### [Basic send with Amp ](https://github.com/socketlabs/socketlabs-ruby/blob/master/examples/basic/basic_send_with_amp_body.rb)
179
182
  This example demonstrates how to send a basic message with an AMP Html body.
180
183
  For more information about AMP please see [AMP Project](https://amp.dev/documentation/)
@@ -213,6 +216,8 @@ For more information about AMP please see [AMP Project](https://amp.dev/document
213
216
 
214
217
  <a name="version"></a>
215
218
  # Version
219
+ * 1.2.1 - Adding optional retry logic for Http requests. If configured, the request will retry when certain 500 errors occur (500, 502, 503, 504)
220
+ * 1.1.1 - Adding request timeout value on the client for Http requests
216
221
  * 1.1.0 - Adds Amp Html Support
217
222
  * 1.0.0 - Initial Release
218
223
 
data/gemfile.lock CHANGED
@@ -1,17 +1,18 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- socketlabs-injectionapi (1.1.0)
4
+ socketlabs-injectionapi (1.4.1)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
8
8
  specs:
9
9
 
10
10
  PLATFORMS
11
+ x64-mingw-ucrt
11
12
  x64-mingw32
12
13
 
13
14
  DEPENDENCIES
14
15
  socketlabs-injectionapi!
15
16
 
16
17
  BUNDLED WITH
17
- 2.1.4
18
+ 2.3.7
@@ -28,6 +28,8 @@ module SocketLabs
28
28
  attr_reader :proxy
29
29
  # The Net::HTTP used when making the HTTP request
30
30
  attr_reader :http
31
+ # The Timeout to use when making the HTTP request
32
+ attr_reader :timeout
31
33
 
32
34
  # @param [Hash] http_request_method
33
35
  # @param [Hash] arguments:
@@ -40,6 +42,7 @@ module SocketLabs
40
42
  @request_method = http_request_method
41
43
  @endpoint = "https://inject.socketlabs.com/api/v1/email"
42
44
  @proxy = Array.new
45
+ @timeout = 120
43
46
 
44
47
  unless arguments.nil? || arguments.empty?
45
48
 
@@ -51,6 +54,10 @@ module SocketLabs
51
54
  @proxy = arguments[:proxy]
52
55
  end
53
56
 
57
+ unless arguments[:timeout].nil?
58
+ @timeout = arguments[:timeout]
59
+ end
60
+
54
61
  end
55
62
 
56
63
  @http = nil
@@ -68,8 +75,7 @@ module SocketLabs
68
75
  response = @http.request(@request)
69
76
  http_response = HttpResponse.new(response)
70
77
 
71
- parser = InjectionResponseParser.new
72
- parser.parse(http_response)
78
+ http_response
73
79
 
74
80
  end
75
81
 
@@ -112,6 +118,8 @@ module SocketLabs
112
118
  params += @proxy.values_at(:host, :port, :user, :pass) unless @proxy.empty?
113
119
 
114
120
  @http = Net::HTTP.new(*params)
121
+ # add timeout
122
+ @http.read_timeout = @timeout
115
123
  # add ssl
116
124
  if @endpoint.start_with?('https')
117
125
  @http.use_ssl = true
@@ -4,10 +4,12 @@ require_relative '../message/email_address.rb'
4
4
  require_relative '../message/bulk_message.rb'
5
5
  require_relative '../message/bulk_recipient.rb'
6
6
  require_relative '../message/custom_header.rb'
7
+ require_relative '../message/metadata.rb'
7
8
 
8
9
  require_relative 'serialization/address_json.rb'
9
10
  require_relative 'serialization/attachment_json.rb'
10
11
  require_relative 'serialization/custom_header_json.rb'
12
+ require_relative 'serialization/metadata_json.rb'
11
13
  require_relative 'serialization/message_json.rb'
12
14
  require_relative 'serialization/merge_data_json.rb'
13
15
 
@@ -68,6 +70,8 @@ module SocketLabs
68
70
  message_json.charset = message.charset
69
71
  message_json.from_email_address = email_address_to_address_json(message.from_email_address)
70
72
  message_json.custom_headers = populate_custom_headers(message.custom_headers)
73
+ message_json.metadata = populate_metadata(message.metadata)
74
+ message_json.tags = message.tags
71
75
  message_json.attachments = populate_attachments(message.attachments)
72
76
 
73
77
  unless message.api_template.nil?
@@ -80,7 +84,7 @@ module SocketLabs
80
84
 
81
85
  end
82
86
 
83
- # Converts a list of Attachment objects to a List of AttachmentJson objects.
87
+ # Converts a list of CustomHeader objects to a List of CustomHeaderJson objects.
84
88
  # @param [Array] custom_headers: list of CustomHeader to convert
85
89
  # @return [Array] the converted list of CustomHeaderJson
86
90
  def populate_custom_headers(custom_headers)
@@ -103,6 +107,29 @@ module SocketLabs
103
107
 
104
108
  end
105
109
 
110
+ # Converts a list of Metadata objects to a List of MetadataJson objects.
111
+ # @param [Array] metadata: list of Metadata to convert
112
+ # @return [Array] the converted list of MetadataJson
113
+ def populate_metadata(metadata)
114
+
115
+ if metadata.nil? || metadata.empty?
116
+ nil
117
+ end
118
+
119
+ metadata_json = Array.new
120
+
121
+ metadata.each do |header|
122
+ if header.instance_of? Metadata
123
+ metadata_json.push(MetadataJson.new(header.key, header.value))
124
+ elsif header.instance_of? Hash
125
+ metadata_json.push(MetadataJson.new(header[:key], header[:value]))
126
+ end
127
+ end
128
+
129
+ metadata_json
130
+
131
+ end
132
+
106
133
  # Converts a list of Attachment objects to a List of AttachmentJson objects.
107
134
  # @param [Attachment] attachments: list of Attachment to convert
108
135
  # @return [Array] the converted list of AttachmentJson
@@ -0,0 +1,68 @@
1
+ require_relative '../../version.rb'
2
+ require_relative '../retrysettings.rb'
3
+ require_relative 'injection_response_parser'
4
+
5
+ module SocketLabs
6
+ module InjectionApi
7
+ module Core
8
+ class RetryHandler
9
+ # include SocketLabsClient::InjectionApi
10
+
11
+ private
12
+
13
+ attr_accessor :http_client
14
+ attr_accessor :endpoint_url
15
+ attr_accessor :retry_settings
16
+ attr_accessor :error_codes
17
+
18
+ def initialize(client, endpoint, settings)
19
+ @http_client = client
20
+ @endpoint_url = endpoint
21
+ @retry_settings = settings
22
+ @error_codes = [500, 502, 503, 504]
23
+
24
+ end
25
+
26
+ public
27
+
28
+ def send(request)
29
+
30
+ attempts = 0
31
+ exception = nil
32
+
33
+ loop do
34
+ wait_interval = @retry_settings.get_next_wait_interval(attempts)
35
+ attempts += 1
36
+
37
+ begin
38
+ response = @http_client.send_request(request)
39
+
40
+ if @error_codes.include? response.status_code.to_i
41
+ exception = SocketLabs::InjectionApi::Exceptions::ServerException.new("Failed to send email. Received #{response.status_code} from server.")
42
+ sleep(wait_interval)
43
+ else
44
+ return response
45
+ end
46
+
47
+ rescue Timeout::Error => exception
48
+ exception = exception
49
+
50
+ break if attempts > @retry_settings.maximum_number_of_retries
51
+ sleep(wait_interval)
52
+
53
+ rescue Exception => exception
54
+ raise exception
55
+ end
56
+
57
+ break if attempts > @retry_settings.maximum_number_of_retries
58
+ end
59
+
60
+ raise exception if exception
61
+
62
+ false
63
+ end
64
+
65
+ end
66
+ end
67
+ end
68
+ end
@@ -4,6 +4,7 @@ require_relative '../message/email_address.rb'
4
4
  require_relative '../message/bulk_message.rb'
5
5
  require_relative '../message/bulk_recipient.rb'
6
6
  require_relative '../message/custom_header.rb'
7
+ require_relative '../message/metadata.rb'
7
8
 
8
9
  module SocketLabs
9
10
  module InjectionApi
@@ -44,7 +45,7 @@ module SocketLabs
44
45
  SendResponse.new(result=SendResult.enum["AuthenticationValidationFailed"])
45
46
  end
46
47
 
47
- if server_id.nil? || server_id.empty?
48
+ if server_id.nil? || (!server_id.is_a?(Integer) && server_id.empty?)
48
49
  SendResponse.new(result=SendResult.enum["AuthenticationValidationFailed"])
49
50
  end
50
51
 
@@ -82,6 +83,9 @@ module SocketLabs
82
83
  unless has_valid_custom_headers(message.custom_headers)
83
84
  SendResult.enum["MessageValidationInvalidCustomHeaders"]
84
85
  end
86
+ unless has_valid_metadata(message.metadata)
87
+ SendResult.enum["MessageValidationInvalidMetadata"]
88
+ end
85
89
 
86
90
  SendResult.enum["Success"]
87
91
 
@@ -351,6 +355,25 @@ module SocketLabs
351
355
 
352
356
  end
353
357
 
358
+ # Check if the list of metadata is valid
359
+ # @param [Array] metadata
360
+ # @return [Array]
361
+ def has_valid_metadata(metadata)
362
+
363
+ unless metadata.nil? || metadata.empty?
364
+ metadata.each do |item|
365
+ if item.instance_of? Metadata
366
+ unless item.is_valid
367
+ false
368
+ end
369
+ end
370
+ end
371
+ end
372
+
373
+ true
374
+
375
+ end
376
+
354
377
  # @param [BasicMessage] message
355
378
  # @return [SendResponse]
356
379
  def validate_basic_message(message)
@@ -44,6 +44,8 @@ module SocketLabs
44
44
  @merge_data = MergeDataJson.new
45
45
  @attachments = Array.new
46
46
  @custom_headers = Array.new
47
+ @metadata = Array.new
48
+ @tags = Array.new
47
49
  @to_email_address = Array.new
48
50
  @cc_email_address = Array.new
49
51
  @bcc_email_address = Array.new
@@ -82,6 +84,7 @@ module SocketLabs
82
84
  def custom_headers
83
85
  @custom_headers
84
86
  end
87
+
85
88
  # Set the list of CustomHeaderJson.
86
89
  # @param [Array] value
87
90
  def custom_headers=(value)
@@ -103,6 +106,63 @@ module SocketLabs
103
106
  end
104
107
  end
105
108
 
109
+ #metadata
110
+ # Get the list of MetadataJson.
111
+ # @return [Array]
112
+ def metadata
113
+ @metadata
114
+ end
115
+
116
+ # Set the list of MetadataJson.
117
+ # @param [Array] value
118
+ def metadata=(value)
119
+ @metadata = Array.new
120
+ unless value.nil? || value.empty?
121
+ value.each do |v1|
122
+ if v1.instance_of? MetadataJson
123
+ @metadata.push(v1)
124
+ end
125
+ end
126
+ end
127
+ end
128
+
129
+ # Add a MetadataJson to the metadata list
130
+ # @param [MetadataJson] value
131
+ def add_metadata(value)
132
+ if value.instance_of? MetadataJson
133
+ @metadata.push(value)
134
+ end
135
+ end
136
+
137
+ # Get the list of tags added to the message.
138
+ def tags
139
+ @tags
140
+ end
141
+
142
+ # Set the list of tags added to the message.
143
+ def tags=(value)
144
+ @tags = Array.new
145
+ unless value.nil? || value.empty?
146
+ value.each do |v1|
147
+ if v1.kind_of? String
148
+ @tags.push(v1)
149
+ else
150
+ raise StandardError("Invalid type for tag, type of 'String' was expected")
151
+ end
152
+ end
153
+ end
154
+ end
155
+
156
+ # Add a Tag to the message.
157
+ # @param [String] value
158
+ def add_metadata(value = nil)
159
+
160
+ if value.kind_of? String
161
+ @tags.push(value)
162
+ end
163
+
164
+ end
165
+
106
166
  # Get the To email address list
107
167
  # @return [Array]
108
168
  def to_email_address
@@ -233,6 +293,18 @@ module SocketLabs
233
293
  json[:customHeaders] = e
234
294
  end
235
295
 
296
+ unless @metadata.nil? || @metadata.length == 0
297
+ e = Array.new
298
+ @metadata.each do |value|
299
+ e.push(value.to_hash)
300
+ end
301
+ json[:metadata] = e
302
+ end
303
+
304
+ unless @tags.nil? || @tags.length == 0
305
+ json[:tags] = @tags
306
+ end
307
+
236
308
  unless @attachments.nil? || @attachments.length == 0
237
309
  e = Array.new
238
310
  @attachments.each do |value|
@@ -0,0 +1,40 @@
1
+ module SocketLabs
2
+ module InjectionApi
3
+ module Core
4
+ module Serialization
5
+
6
+ # Represents metadata as a key and value pair.
7
+ # To be serialized into JSON string before sending to the Injection Api.
8
+ class MetadataJson
9
+
10
+ # key of the metadata.
11
+ attr_accessor :key
12
+ # value of the metadata.
13
+ attr_accessor :value
14
+
15
+ # Initializes a new instance of the MetadataJson class
16
+ # @param [String] key
17
+ # @param [String] value
18
+ def initialize(
19
+ key = nil,
20
+ value = nil
21
+ )
22
+ @key = key
23
+ @value = value
24
+ end
25
+
26
+ # build json hash for MetadataJson
27
+ # @return [hash]
28
+ def to_hash
29
+ {
30
+ :key => @key,
31
+ :value => @value
32
+ }
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,7 @@
1
+ module SocketLabs
2
+ module InjectionApi
3
+ module Exceptions
4
+ class ServerException < Exception;end
5
+ end
6
+ end
7
+ end
@@ -12,6 +12,7 @@ module SocketLabs
12
12
  @to_email_address = Array.new
13
13
  @cc_email_address = Array.new
14
14
  @bcc_email_address = Array.new
15
+ @tags = Array.new
15
16
 
16
17
  end
17
18
 
@@ -87,6 +88,8 @@ module SocketLabs
87
88
  replyTo: @reply_to_email_address,
88
89
  attachments: @attachments,
89
90
  customHeaders: @custom_headers,
91
+ metadata: @metadata,
92
+ tags: @tags,
90
93
  to: @to_email_address,
91
94
  cc: @cc_email_address,
92
95
  bcc: @bcc_email_address
@@ -55,6 +55,8 @@ module SocketLabs
55
55
  replyTo: @reply_to_email_address,
56
56
  attachments: @attachments,
57
57
  customHeaders: @custom_headers,
58
+ metadata: @metadata,
59
+ tags: @tags,
58
60
  to: @to_recipient,
59
61
  global_merge_data: @global_merge_data
60
62
  }
@@ -20,11 +20,11 @@ module SocketLabs
20
20
  # (Optional) Either TextBody or HtmlBody must be used with the AmpBody or use a ApiTemplate
21
21
  attr_accessor :api_template
22
22
  # the custom MailingId for the message.
23
- # See https://www.injectionapi.com/blog/best-practices-for-using-custom-mailingids-and-messageids/
23
+ # See https://www.socketlabs.com/blog/best-practices-for-using-custom-mailingids-and-messageids/
24
24
  # for more information.
25
25
  attr_accessor :mailing_id
26
26
  # the custom MessageId for the message.
27
- # See https://www.injectionapi.com/blog/best-practices-for-using-custom-mailingids-and-messageids/
27
+ # See https://www.socketlabs.com/blog/best-practices-for-using-custom-mailingids-and-messageids/
28
28
  # for more information.
29
29
  attr_accessor :message_id
30
30
  # the optional character set. Default is UTF-8
@@ -77,6 +77,8 @@ module SocketLabs
77
77
 
78
78
  @attachments = Array.new
79
79
  @custom_headers = Array.new
80
+ @metadata = Array.new
81
+ @tags = Array.new
80
82
 
81
83
  end
82
84
 
@@ -140,6 +142,7 @@ module SocketLabs
140
142
  def custom_headers
141
143
  @custom_headers
142
144
  end
145
+
143
146
  # Set the list of custom message headers added to the message.
144
147
  def custom_headers=(value)
145
148
  @custom_headers = Array.new
@@ -153,6 +156,7 @@ module SocketLabs
153
156
  end
154
157
  end
155
158
  end
159
+
156
160
  # Add a CustomHeader to the message.
157
161
  # @param [String/CustomHeader] name
158
162
  # @param [String] value
@@ -168,6 +172,68 @@ module SocketLabs
168
172
 
169
173
  end
170
174
 
175
+ # Get the list of metadata added to the message.
176
+ def metadata
177
+ @metadata
178
+ end
179
+
180
+ # Set the list of metadata added to the message.
181
+ def metadata=(value)
182
+ @metadata = Array.new
183
+ unless value.nil? || value.empty?
184
+ value.each do |v1|
185
+ if v1.instance_of? Metadata
186
+ @metadata.push(v1)
187
+ else
188
+ raise StandardError("Invalid type for metadata, type of 'Metadata' was expected")
189
+ end
190
+ end
191
+ end
192
+ end
193
+
194
+ # Add a Metadata to the message.
195
+ # @param [String/Metadata] key
196
+ # @param [String] value
197
+ def add_metadata(key, value = nil)
198
+
199
+ if key.kind_of? Metadata
200
+ @metadata.push(key)
201
+
202
+ elsif key.kind_of? String
203
+ @metadata.push(Metadata.new(key, value))
204
+
205
+ end
206
+
207
+ end
208
+
209
+ # Get the list of tags added to the message.
210
+ def tags
211
+ @tags
212
+ end
213
+
214
+ # Set the list of tags added to the message.
215
+ def tags=(value)
216
+ @tags = Array.new
217
+ unless value.nil? || value.empty?
218
+ value.each do |v1|
219
+ if v1.kind_of? String
220
+ @tags.push(v1)
221
+ else
222
+ raise StandardError("Invalid type for tag, type of 'String' was expected")
223
+ end
224
+ end
225
+ end
226
+ end
227
+
228
+ # Add a Tag to the message.
229
+ # @param [String] value
230
+ def add_tag(value = nil)
231
+
232
+ if value.kind_of? String
233
+ @tags.push(value)
234
+ end
235
+
236
+ end
171
237
 
172
238
 
173
239
  end
@@ -0,0 +1,52 @@
1
+ require_relative '../core/string_extension.rb'
2
+
3
+ module SocketLabs
4
+ module InjectionApi
5
+ module Message
6
+
7
+ # Represents metadata as a key-value pair.
8
+ # Example:
9
+ #
10
+ # metadata1 = Metadata.new
11
+ # metadata1.key = "key1"
12
+ # metadata1.value = "value1"
13
+ #
14
+ # metadata2 = Metadata.new("key2", "value2")
15
+
16
+ class Metadata
17
+
18
+ # the key of the metadata item
19
+ attr_accessor :key
20
+ # the value of the metadata item
21
+ attr_accessor :value
22
+
23
+ # Initializes a new instance of the Metadata class
24
+ # @param [String] key
25
+ # @param [String] value
26
+ def initialize(
27
+ key = nil,
28
+ value = nil
29
+ )
30
+ @key = key
31
+ @value = value
32
+ end
33
+
34
+ # Determines if the Metadata is valid.
35
+ # @return [Boolean]
36
+ def is_valid
37
+ valid_key = !(@key.nil? || @key.empty?)
38
+ valid_value = !(@value.nil? || @value.empty?)
39
+
40
+ valid_key && valid_value
41
+ end
42
+
43
+ # Represents the Metadata key-value pair as a String
44
+ # @return [String]
45
+ def to_s
46
+ "#{@key}, #{@value}"
47
+ end
48
+
49
+ end
50
+ end
51
+ end
52
+ end