socketlabs-injectionapi 1.2.1 → 1.4.2

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: 9fce0b211f96b88503365f93ed1270eb5d943d835e21e53fcbee2806621354f0
4
- data.tar.gz: bae74a7c4e920b89fdea11aaed1efce1166ccd9ff923ef39f9e04278118eed06
3
+ metadata.gz: f557916e593c897978060429a0bdb300c61f5047fc879902a2f43ad7a8bffbab
4
+ data.tar.gz: 31a2adba06e1794c3c1b49e23bb7dbdc6de4be1bc1349e6c0d0cc5ed53cc4757
5
5
  SHA512:
6
- metadata.gz: 8004fdece9672ae94d3f3fef5838504cb65606f21ff656210a7a91741b0ec713aecc56bcd0926864e5524dc75869bca3914fc98f3f3f9be808243fb859c035e0
7
- data.tar.gz: 8a7114b59e0f8ec6c8ad416b30743d346cccd9de03c84a0b2380bf14885a25a7a0d146f7be6017a92dae2af381b8a339dfe442e128a74f67042801274a0b18b5
6
+ metadata.gz: 143016ece80ad2d42de386599a0455e1fad50172c5471ff74773fb180ec6cbd3a8d100baec4d31ffcf776a11f29bf7e2541c93633acd20bfa70fc8d397919a11
7
+ data.tar.gz: 13097dbd02d8ba26a590b42fe1da47c115049642c2eb74cad3cf1571b4e8c28ce08efbe56cabc266dd9e2e1d4a5bbc8f94fc7da25a84f4336de1971e013ea8b5
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
data/gemfile.lock CHANGED
@@ -1,17 +1,18 @@
1
- PATH
2
- remote: .
3
- specs:
4
- socketlabs-injectionapi (1.2.1)
5
-
6
- GEM
7
- remote: http://rubygems.org/
8
- specs:
9
-
10
- PLATFORMS
11
- x64-mingw32
12
-
13
- DEPENDENCIES
14
- socketlabs-injectionapi!
15
-
16
- BUNDLED WITH
17
- 2.1.4
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ socketlabs-injectionapi (1.4.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+
10
+ PLATFORMS
11
+ x64-mingw-ucrt
12
+ x64-mingw32
13
+
14
+ DEPENDENCIES
15
+ socketlabs-injectionapi!
16
+
17
+ BUNDLED WITH
18
+ 2.3.7
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+
4
+ module SocketLabs
5
+ module InjectionApi
6
+ module Core
7
+ class ApiKeyParseResult
8
+ def self.enum
9
+ {
10
+ # No result could be produced.
11
+ "None" =>
12
+ {
13
+ :name => "None",
14
+ :value =>0,
15
+ },
16
+ # The key was found to be blank or invalid.
17
+ "InvalidEmptyOrWhitespace" =>
18
+ {
19
+ :name => "InvalidEmptyOrWhitespace",
20
+ :value =>1,
21
+ },
22
+ # The public portion of the key was unable to be parsed.
23
+ "InvalidUnableToExtractPublicPart" =>
24
+ {
25
+ :name => "InvalidUnableToExtractPublicPart",
26
+ :value =>2,
27
+ },
28
+ # The secret portion of the key was unable to be parsed.
29
+ "InvalidUnableToExtractSecretPart" =>
30
+ {
31
+ :name => "InvalidUnableToExtractSecretPart",
32
+ :value =>3,
33
+ },
34
+ # Key was successfully parsed.
35
+ "Success" =>
36
+ {
37
+ :name => "Success",
38
+ :value =>4,
39
+ }
40
+ }
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+
4
+ module SocketLabs
5
+ module InjectionApi
6
+ module Core
7
+ class ApiKeyParser
8
+
9
+
10
+ # Parse the API key to determine what kind of key was provided.
11
+ # @param [String] wholeApiKey: A ApiKeyParseResult with the parsing results
12
+ # @return [ApiKeyParseResult] the SendResponse from the request
13
+ def parse(
14
+ wholeApiKey
15
+ )
16
+
17
+
18
+
19
+
20
+ if wholeApiKey.nil? || wholeApiKey.empty?
21
+ ApiKeyParseResult.enum["InvalidEmptyOrWhitespace"]
22
+ end
23
+
24
+ if wholeApiKey.length != 61
25
+ ApiKeyParseResult.enum["InvalidKeyLength"]
26
+ end
27
+
28
+ if wholeApiKey.index('.') == -1
29
+ ApiKeyParseResult.enum["InvalidKeyFormat"]
30
+ end
31
+
32
+ # extract public part
33
+ # don't check more than 50 chars
34
+ publicPartEnd = wholeApiKey[0..50].index('.')
35
+ if publicPartEnd == -1
36
+ ApiKeyParseResult.enum["InvalidUnableToExtractPublicPart"]
37
+ end
38
+
39
+ publicPart = wholeApiKey[0..publicPartEnd]
40
+ if publicPart != 20
41
+ ApiKeyParseResult.enum["InvalidPublicPartLength"]
42
+ end
43
+
44
+ # now extract the private part
45
+ if wholeApiKey.length <= publicPartEnd + 1
46
+ ApiKeyParseResult.enum["InvalidUnableToExtractSecretPart"]
47
+ end
48
+
49
+ privatePart = wholeApiKey[publicPartEnd + 1..wholeApiKey.length]
50
+
51
+ if privatePart.length != 40
52
+ ApiKeyParseResult.enum["InvalidSecretPartLength"]
53
+ end
54
+
55
+ ApiKeyParseResult.enum["Success"]
56
+
57
+ end
58
+
59
+ end
60
+ end
61
+ end
62
+ end
@@ -43,6 +43,12 @@ module SocketLabs
43
43
  @endpoint = "https://inject.socketlabs.com/api/v1/email"
44
44
  @proxy = Array.new
45
45
  @timeout = 120
46
+ @headers =
47
+ [
48
+ { :key => "User-Agent", :value => user_agent},
49
+ { :key => "Content-Type", :value => "application/json; charset=utf-8" },
50
+ { :key => "Accept", :value => "application/json"}
51
+ ]
46
52
 
47
53
  unless arguments.nil? || arguments.empty?
48
54
 
@@ -58,6 +64,10 @@ module SocketLabs
58
64
  @timeout = arguments[:timeout]
59
65
  end
60
66
 
67
+ unless arguments[:authorization].nil? || arguments[:authorization].empty?
68
+ @headers.push({ :key => "Authorization", :value => 'Bearer ' + arguments[:authorization]})
69
+ end
70
+
61
71
  end
62
72
 
63
73
  @http = nil
@@ -73,6 +83,7 @@ module SocketLabs
73
83
 
74
84
  # send request
75
85
  response = @http.request(@request)
86
+
76
87
  http_response = HttpResponse.new(response)
77
88
 
78
89
  http_response
@@ -87,25 +98,17 @@ module SocketLabs
87
98
  "SocketLabs-ruby/#{VERSION};ruby(#{RUBY_VERSION})"
88
99
  end
89
100
 
90
- # headers to add to the request
91
- def headers
92
- [
93
- { :key => "User-Agent", :value => user_agent},
94
- { :key => "Content-Type", :value => "application/json; charset=utf-8" },
95
- { :key => "Accept", :value => "application/json"}
96
- ]
97
- end
98
-
99
101
  # add request headers
100
102
  # @param [HTTP::NET] request: the request object
101
103
  # @return [HTTP::NET] the resulting request
102
104
  def add_request_headers(request)
103
105
 
104
- request.add_field('Content-Type', 'application/json')
105
- headers.each do |item|
106
+ @headers.each do |item|
106
107
  request[item[:key]] = item[:value]
107
108
  end
109
+
108
110
  request
111
+
109
112
  end
110
113
 
111
114
  # Build the API request for HTTP::NET
@@ -130,14 +133,7 @@ module SocketLabs
130
133
  @request = add_request_headers(net_http.new(uri.request_uri))
131
134
 
132
135
  end
133
-
134
136
  end
135
-
136
-
137
-
138
-
139
-
140
-
141
137
  end
142
138
  end
143
139
  end
@@ -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
@@ -27,39 +27,39 @@ module SocketLabs
27
27
 
28
28
  def send(request)
29
29
 
30
- if @retry_settings.maximum_number_of_retries == 0
31
- response = @http_client.send_request(request)
32
- response
33
- end
34
-
35
30
  attempts = 0
31
+ exception = nil
36
32
 
37
33
  loop do
38
34
  wait_interval = @retry_settings.get_next_wait_interval(attempts)
35
+ attempts += 1
39
36
 
40
37
  begin
41
38
  response = @http_client.send_request(request)
42
39
 
43
- if (@error_codes.include? response.status_code.to_i) && (attempts < @retry_settings.maximum_number_of_retries)
44
- attempts += 1
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)
45
43
  else
46
- response
44
+ return response
47
45
  end
48
46
 
49
47
  rescue Timeout::Error => exception
50
- attempts += 1
51
-
52
- if attempts > @retry_settings.maximum_number_of_retries
53
- raise exception
54
- end
48
+ exception = exception
49
+
50
+ break if attempts > @retry_settings.maximum_number_of_retries
55
51
  sleep(wait_interval)
56
52
 
57
53
  rescue Exception => exception
58
54
  raise exception
59
55
  end
60
56
 
57
+ break if attempts > @retry_settings.maximum_number_of_retries
61
58
  end
62
59
 
60
+ raise exception if exception
61
+
62
+ false
63
63
  end
64
64
 
65
65
  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
  }