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
data/README.MD ADDED
@@ -0,0 +1,212 @@
1
+ [![SocketLabs](https://www.socketlabs.com/assets/socketlabs-logo1.png)](https://www.socketlabs.com)
2
+ # [![Twitter Follow](https://img.shields.io/twitter/follow/socketlabs.svg?style=social&label=Follow)](https://twitter.com/socketlabs) [![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/socketlabs/socketlabs-csharp/blob/master/CONTRIBUTING.md)
3
+ <!--
4
+ [![GitHub contributors](https://img.shields.io/github/contributors/socketlabs/socketlabs-python.svg)](https://github.com/socketlabs/socketlabs-python/graphs/contributors)
5
+ -->
6
+
7
+ The SocketLabs Email Delivery Ruby library allows you to easily send email messages via the [SocketLabs Injection API](https://www.socketlabs.com/api-reference/injection-api/). The library makes it easy to build and send any type of message supported by the API, from a simple message to a single recipient all the way to a complex bulk message sent to a group of recipients with unique merge data per recipient.
8
+
9
+ # Table of Contents
10
+ * [Prerequisites and Installation](#prerequisites-and-installation)
11
+ * [Getting Started](#getting-started)
12
+ * [Managing API Keys](#managing-api-keys)
13
+ * [Examples and Use Cases](#examples-and-use-cases)
14
+ * [License](#license)
15
+
16
+
17
+ <a name="prerequisites-and-installation" id="prerequisites-and-installation"></a>
18
+ # Prerequisites and Installation
19
+ ## Prerequisites
20
+ * A supported Ruby version (2.4 and above)
21
+ * 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.
22
+
23
+ ## Installation
24
+
25
+ Add this line to your application's Gemfile:
26
+ ```ruby
27
+ gem 'socketlabs-injectionapi'
28
+ ```
29
+ And then execute:
30
+
31
+ $ bundle install
32
+
33
+ Install it yourself as:
34
+
35
+ $ gem install socketlabs-injectionapi
36
+
37
+ Or From git
38
+ ```
39
+ gem "socketlabs-injectionapi", :git => "git://github.com:socketlabs/socketlabs-ruby.git"
40
+ ```
41
+
42
+ For more information please see the [Installing Gems](https://guides.rubygems.org/rubygems-basics/) tutorial
43
+
44
+ <a name="getting-started" id="getting-started"></a>
45
+ # Getting Started
46
+ ## Obtaining your API Key and SocketLabs ServerId number
47
+ In order to get started, you'll need to enable the Injection API feature in the [SocketLabs Control Panel](https://cp.socketlabs.com).
48
+ Once logged in, navigate to your SocketLabs server's dashboard (if you only have one server on your account you'll be taken here immediately after logging in).
49
+ Make note of your 4 or 5 digit ServerId number, as you'll need this along with
50
+ your API key in order to use the Injection API.
51
+
52
+ To enable the Injection API, click on the "For Developers" dropdown on the top-level navigation, then choose the "Configure HTTP Injection API" option.
53
+ Once here, you can enable the feature by choosing the "Enabled" option in the
54
+ dropdown. Enabling the feature will also generate your API key, which you'll
55
+ need (along with your ServerId) to start using the API. Be sure to click the
56
+ "Update" button to save your changes once you are finished.
57
+
58
+
59
+ ## Basic Message
60
+ A basic message is an email message like you'd send from a personal email client such as Outlook.
61
+ A basic message can have many recipients, including multiple To addresses, CC addresses, and even BCC addresses.
62
+ You can also send a file attachment in a basic message.
63
+
64
+ ```ruby
65
+ require "socketlabs-injectionapi.rb"
66
+
67
+ include SocketLabs::InjectionApi
68
+ include SocketLabs::InjectionApi::Message
69
+
70
+ message = BasicMessage.new
71
+
72
+ message.subject = "Sending A Basic Message"
73
+ message.html_body = "<html>This is the Html Body of my message.</html>"
74
+ message.plain_text_body = "This is the Plain Text Body of my message."
75
+
76
+ message.from_email_address = EmailAddress.new("from@example.com")
77
+
78
+ # A basic message supports up to 50 recipients
79
+ # and supports several different ways to add recipients
80
+
81
+ # Add a To address by passing the email address
82
+ message.to_email_address.push("recipient1@example.com")
83
+ message.to_email_address.push(EmailAddress.new("recipient2@example.com", "Recipient #2"))
84
+
85
+ # // Adding CC Recipients
86
+ message.add_cc_email_address("recipient3@example.com")
87
+ message.add_cc_email_address("recipient4@example.com", "Recipient #4")
88
+
89
+ # Adding Bcc Recipients
90
+ message.add_bcc_email_address(EmailAddress.new("recipient5@example.com"))
91
+ message.add_bcc_email_address(EmailAddress.new("recipient6@example.com", "Recipient #6"))
92
+
93
+ # Your SocketLabs ServerId and Injection API key
94
+ client = SocketLabsClient.new(10000, "YOUR-API-KEY")
95
+
96
+ response = client.send(message)
97
+ ```
98
+
99
+ ## Bulk Message
100
+ A bulk message usually contains a single recipient per message
101
+ and is generally used to send the same content to many recipients,
102
+ optionally customizing the message via the use of MergeData.
103
+ For more information about using Merge data, please see the [Injection API documentation](https://www.socketlabs.com/api-reference/injection-api/#merging).
104
+ ```ruby
105
+ require "socketlabs-injectionapi.rb"
106
+
107
+ include SocketLabs::InjectionApi
108
+ include SocketLabs::InjectionApi::Message
109
+
110
+ # Your SocketLabs ServerId and Injection API key
111
+ client = SocketLabsClient.new(10000, "YOUR-API-KEY")
112
+
113
+ message = BulkMessage.new
114
+
115
+ message.subject = "Sending A Bulk Message"
116
+ message.plain_text_body = "This is the body of my message sent to %%Name%%"
117
+ message.html_body = "<html>This is the HtmlBody of my message sent to %%Name%%</html>"
118
+
119
+ message.from_email_address = EmailAddress.new("from@example.com")
120
+
121
+ message.add_global_merge_data("HairColor", "{ not set }")
122
+
123
+ recipient1 = BulkRecipient.new("recipient1@example.com")
124
+ recipient1.add_merge_data("Name", "Recipient1")
125
+ message.add_to_recipient(recipient1)
126
+
127
+ recipient2 = BulkRecipient.new("recipient2@example.com", { :friendly_name => "Recipient #2" })
128
+ recipient2.add_merge_data("Name", "Recipient2")
129
+ message.add_to_recipient(recipient2)
130
+
131
+ response = client.send(message)
132
+ ```
133
+
134
+ <a name="managing-api-keys" id="managing-api-keys"></a>
135
+ ## Managing API Keys
136
+ For ease of demonstration, many of our examples include the ServerId (SOCKETLABS_SERVER_ID) and API key
137
+ (SOCKETLABS_INJECTION_API_KEY) directly in our code sample. Generally it is not considered a good practice to store
138
+ sensitive information like this directly in your code. Depending on your project type, we recommend either storing your
139
+ credentials using Environment Variables. For more information please see:
140
+ [Using Environment Variables](https://docs.microsoft.com/en-us/dotnet/api/system.environment.getenvironmentvariable)
141
+
142
+
143
+ <a name="examples-and-use-cases" id="examples-and-use-cases"></a>
144
+ # Examples and Use Cases
145
+ In order to demonstrate the many possible use cases for the SDK, we've provided
146
+ an assortment of code examples. These examples demonstrate many different
147
+ features available to the Injection API and SDK, including using templates
148
+ created in the [SocketLabs Email Designer](https://www.socketlabs.com/blog/introducing-new-email-designer/), custom email headers, sending
149
+ attachments, sending content that is stored in an HTML file, advanced bulk
150
+ merging, and even pulling recipients from a datasource.
151
+
152
+ ### [Basic send example](https://github.com/socketlabs/socketlabs-ruby/blob/master/examples/basic/basic_send.rb)
153
+ This example demonstrates a Basic Send.
154
+
155
+ ### [Basic send complex example](https://github.com/socketlabs/socketlabs-ruby/blob/master/examples/basic/basic_send_complex.rb)
156
+ This example demonstrates many features of the Basic Send, including adding multiple recipients, adding message and mailing id's, and adding an embedded image.
157
+
158
+ ### [Basic send from HTML file](https://github.com/socketlabs/socketlabs-ruby/blob/master/examples/basic/basic_send_from_html_file.rb)
159
+ This example demonstrates how to read in your HTML content from an HTML file
160
+ rather than passing in a string directly.
161
+
162
+ ### [Basic send from SocketLabs Template](https://github.com/socketlabs/socketlabs-ruby/blob/master/examples/basic/basic_send_with_api_template.rb)
163
+ This example demonstrates the sending of a piece of content that was created in the
164
+ SocketLabs Email Designer. This is also known as the [API Templates](https://www.socketlabs.com/blog/introducing-api-templates/) feature.
165
+
166
+ ### [Basic send with specified character set](https://github.com/socketlabs/socketlabs-ruby/blob/master/examples/basic/basic_send_with_ascii_charset.rb)
167
+ This example demonstrates sending with a specific character set.
168
+
169
+ ### [Basic send with file attachment](https://github.com/socketlabs/socketlabs-ruby/blob/master/examples/basic/basic_send_with_attachment.rb)
170
+ This example demonstrates how to add a file attachment to your message.
171
+
172
+ ### [Basic send with custom email headers](https://github.com/socketlabs/socketlabs-ruby/blob/master/examples/basic/basic_send_with_custom_headers.rb)
173
+ This example demonstrates how to add custom headers to your email message.
174
+
175
+ ### [Basic send with embedded image](https://github.com/socketlabs/socketlabs-ruby/blob/master/examples/basic/basic_send_with_embedded_image.rb)
176
+ This example demonstrates how to embed an image in your message.
177
+
178
+ ### [Basic send with a web proxy](https://github.com/socketlabs/socketlabs-ruby/blob/master/examples/basic/basic_send_with_proxy.rb)
179
+ This example demonstrates how to use a proxy with your HTTP client.
180
+
181
+ ### [Basic send with invalid file attachment](https://github.com/socketlabs/socketlabs-ruby/blob/master/examples/basic/invalid/basic_send_with_invalid_attachment.rb)
182
+ This example demonstrates the results of attempting to do a send with an invalid attachment.
183
+
184
+ ### [Basic send with invalid from address](https://github.com/socketlabs/socketlabs-ruby/blob/master/examples/basic/invalid/basic_send_with_invalid_from.rb)
185
+ This example demonstrates the results of attempting to do a send with an invalid from address.
186
+
187
+ ### [Basic send with invalid recipients](https://github.com/socketlabs/socketlabs-ruby/blob/master/examples/basic/invalid/basic_send_with_invalid_recipients.rb)
188
+ This example demonstrates the results of attempting to do a send with invalid recipients.
189
+
190
+ ### [Bulk send with multiple recipients](https://github.com/socketlabs/socketlabs-ruby/blob/master/examples/bulk/bulk_send.rb)
191
+ This example demonstrates how to send a bulk message to multiple recipients.
192
+
193
+ ### [Bulk send with merge data](https://github.com/socketlabs/socketlabs-ruby/blob/master/examples/bulk/bulk_send_with_merge_data.rb)
194
+ This example demonstrates how to send a bulk message to multiple recipients with
195
+ unique merge data per recipient.
196
+
197
+ ### [Bulk send with complex merge including attachments](https://github.com/socketlabs/socketlabs-ruby/blob/master/examples/bulk/bulk_send_complex.rb)
198
+ This example demonstrates many features of the `BulkMessage()`, including
199
+ adding multiple recipients, merge data, and adding an attachment.
200
+
201
+ ### [Bulk send with recipients pulled from a datasource](https://github.com/socketlabs/socketlabs-ruby/blob/master/examples/bulk/bulk_send_from_data_source_with_merge_data.rb)
202
+ This example uses a mock repository class to demonstrate how you would pull
203
+ your recipients from a database and create a bulk mailing with merge data.
204
+
205
+ ### [Bulk send with Ascii charset and special characters](https://github.com/socketlabs/socketlabs-ruby/blob/master/examples/bulk/bulk_send_with_ascii_charset_merge_data.rb)
206
+ This example demonstrates how to send a bulk message with a specified character
207
+ set and special characters.
208
+
209
+
210
+ <a name="license" id="license"></a>
211
+ # License
212
+ The SocketLabs.EmailDelivery library and all associated code, including any code samples, are [MIT Licensed](https://github.com/socketlabs/socketlabs-python/blob/master/LICENSE.MD).
data/gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
data/gemfile.lock ADDED
@@ -0,0 +1,17 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ socketlabs-injectionapi (0.0.1.pre.Dev)
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
@@ -0,0 +1,41 @@
1
+ module SocketLabs
2
+ module InjectionApi
3
+ #The result of a single recipient in the Injection request.
4
+ class AddressResult
5
+
6
+ # The recipient's email address.
7
+ attr_accessor :email_address
8
+ # Whether the recipient was accepted for delivery.
9
+ attr_accessor :accepted
10
+ # An error code detailing why the recipient was not accepted.
11
+ attr_accessor :error_code
12
+
13
+ def initialize (
14
+ email_address= nil,
15
+ accepted= nil,
16
+ error_code= nil
17
+ )
18
+ @email_address = email_address
19
+ @accepted = accepted
20
+ @error_code = error_code
21
+ end
22
+
23
+ # Represents the AddressResult as a str. Useful for debugging.
24
+ # @return [String]
25
+ def to_s
26
+ "#{@error_code}: #{@email_address}"
27
+ end
28
+
29
+ # build json hash for AddressResult
30
+ def to_json(*)
31
+ {
32
+ :errorCode => @error_code,
33
+ :accepted => @accepted,
34
+ :emailAddress => @email_address
35
+ }.delete_if { |_, value| value.to_s.strip == '' || value == [] || value == {}}
36
+ end
37
+
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,135 @@
1
+ require 'net/https'
2
+ require 'net/http'
3
+
4
+ require_relative '../../version.rb'
5
+ require_relative 'injection_response_parser'
6
+
7
+ module SocketLabs
8
+ module InjectionApi
9
+ module Core
10
+ class HttpRequest
11
+
12
+ public
13
+ # Hash enumeration of HTTP Request Methods
14
+ def self.http_request_method
15
+ {
16
+ :Get => { :method => "GET" },
17
+ :Post => { :method => "POST" },
18
+ :Put => { :method => "PUT" },
19
+ :Delete => { :method => "DELETE" }
20
+ }
21
+ end
22
+
23
+ # The HTTP Request Method to use
24
+ attr_reader :request_method
25
+ # The SocketLabs Injection API endpoint
26
+ attr_reader :endpoint
27
+ # The Proxy to use when making the HTTP request
28
+ attr_reader :proxy
29
+ # The Net::HTTP used when making the HTTP request
30
+ attr_reader :http
31
+
32
+ # @param [Hash] http_request_method
33
+ # @param [Hash] arguments:
34
+ # http_endpoint = The SocketLabs Injection API endpoint
35
+ # proxy = hash of proxy settings. ex: { host: '127.0.0.1', port: 8080 }
36
+ def initialize(
37
+ http_request_method,
38
+ arguments = nil
39
+ )
40
+ @request_method = http_request_method
41
+ @endpoint = "https://inject.socketlabs.com/api/v1/email"
42
+ @proxy = Array.new
43
+
44
+ unless arguments.nil? || arguments.empty?
45
+
46
+ unless arguments[:http_endpoint].nil? || arguments[:http_endpoint].empty?
47
+ @endpoint = arguments[:http_endpoint]
48
+ end
49
+
50
+ unless arguments[:proxy].nil? || arguments[:proxy].empty?
51
+ @proxy = arguments[:proxy]
52
+ end
53
+
54
+ end
55
+
56
+ @http = nil
57
+ @request = build_request
58
+ end
59
+
60
+ # Send the HTTP Request
61
+ # @param [InjectionRequest]
62
+ def send_request(request)
63
+
64
+ factory_hash = request.to_hash
65
+ @request.body = factory_hash.to_json
66
+
67
+ # send request
68
+ response = @http.request(@request)
69
+ http_response = HttpResponse.new(response)
70
+
71
+ parser = InjectionResponseParser.new
72
+ parser.parse(http_response)
73
+
74
+ end
75
+
76
+ private
77
+ # The User-Agent request header added to the Injection API Http request.
78
+ # Used to identify the source of the request.
79
+ # @return [String] the SocketLabs User-Agent
80
+ def user_agent
81
+ "SocketLabs-ruby/#{VERSION};ruby(#{RUBY_VERSION})"
82
+ end
83
+
84
+ # headers to add to the request
85
+ def headers
86
+ [
87
+ { :key => "User-Agent", :value => user_agent},
88
+ { :key => "Content-Type", :value => "application/json; charset=utf-8" },
89
+ { :key => "Accept", :value => "application/json"}
90
+ ]
91
+ end
92
+
93
+ # add request headers
94
+ # @param [HTTP::NET] request: the request object
95
+ # @return [HTTP::NET] the resulting request
96
+ def add_request_headers(request)
97
+
98
+ request.add_field('Content-Type', 'application/json')
99
+ headers.each do |item|
100
+ request[item[:key]] = item[:value]
101
+ end
102
+ request
103
+ end
104
+
105
+ # Build the API request for HTTP::NET
106
+ def build_request
107
+
108
+ uri = URI.parse(@endpoint)
109
+ # add uri
110
+ params = [uri.host, uri.port]
111
+ # add proxy
112
+ params += @proxy.values_at(:host, :port, :user, :pass) unless @proxy.empty?
113
+
114
+ @http = Net::HTTP.new(*params)
115
+ # add ssl
116
+ if @endpoint.start_with?('https')
117
+ @http.use_ssl = true
118
+ @http.verify_mode = OpenSSL::SSL::VERIFY_PEER
119
+ end
120
+
121
+ net_http = Kernel.const_get('Net::HTTP::' + @request_method[:method].capitalize)
122
+ @request = add_request_headers(net_http.new(uri.request_uri))
123
+
124
+ end
125
+
126
+ end
127
+
128
+
129
+
130
+
131
+
132
+
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,27 @@
1
+
2
+ module SocketLabs
3
+ module InjectionApi
4
+ module Core
5
+
6
+ class HttpResponse
7
+
8
+ attr_reader :status_code
9
+ attr_reader :body
10
+ attr_reader :headers
11
+
12
+ def initialize(response)
13
+ @status_code = response.code
14
+ @body = response.body
15
+ @headers = response.to_hash
16
+ end
17
+
18
+ # Returns the body as a hash
19
+ def to_hash
20
+ @parsed_body ||= JSON.parse(@body, symbolize_names: true)
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+ end
27
+ end