unione-ruby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c8462ee8f317ff0d316298a45e0f6f948897776346bcbe7f70997190a681e02a
4
+ data.tar.gz: 51f9b320c81f7bd65559b10eda50bdaf20a0bd1148c2d7017590a1d344319118
5
+ SHA512:
6
+ metadata.gz: 46993c14f31096f8f22eefb96f050c26436088335ef44572d981fd599f25187b333d69b7c19790e0fe04f3efee05b164f226181d5633f0c06c30737a25e62bd4
7
+ data.tar.gz: 33b4d179be489d1edb42b0a24f5866a672d640ae90e30dd8db1376a63ecb695624c5034b48f52b30584153c1298cf7fd780be610204ae7e02e43715d3e37e82f
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 UniOne
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,42 @@
1
+ # unione-ruby
2
+ UniOne (unione.io) integration gem for Ruby
3
+
4
+ [Examples of usage](https://github.com/unione-repo/unione-ruby/tree/master/examples)
5
+
6
+ ## Working with API responses
7
+ This library using [faraday](https://github.com/lostisland/faraday) gem for making HTTP queries along with [mashify middleware](https://github.com/hashie/hashie#mash) for post-processing response body. So, you can use extended syntax for accessing response fields.
8
+
9
+ Example of work with responses:
10
+
11
+ ```ruby
12
+ require 'unione-ruby'
13
+ unione = UniOne::Client.new(data_center: 'eu1', lang: 'en', api_key: ENV['UNIONE_API_KEY'])
14
+ response = unione.get_dns_records("example.com")
15
+ puts response.status
16
+ puts response.body.to_h
17
+ puts response.headers
18
+ # Access fields as hash or as methods
19
+ puts response.body['status']
20
+ puts response.body.status
21
+ ```
22
+
23
+ ## Handling API errors
24
+ Library using [`raise-error' middleware](https://lostisland.github.io/faraday/middleware/raise-error) for handling error responses.
25
+
26
+ Example of work with errors:
27
+
28
+ ```ruby
29
+ require 'unione-ruby'
30
+ begin
31
+ unione = UniOne::Client.new(data_center: 'eu1', lang: 'en', api_key: ENV['UNIONE_API_KEY'])
32
+ response = unione.get_dns_records("example.com")
33
+ puts response.status
34
+ puts response.body.to_h
35
+ puts response.headers
36
+ rescue Faraday::Error => e
37
+ # Note that exception response has internal format which is a little bit different and body field is not post-processed
38
+ puts e.response[:status]
39
+ puts e.response[:body]
40
+ puts e.response[:headers]
41
+ end
42
+ ```
@@ -0,0 +1,25 @@
1
+ require 'unione-ruby'
2
+ require 'json'
3
+
4
+ unione = UniOne::Client.new(data_center: 'eu1', lang: 'en', api_key: ENV['UNIONE_API_KEY'])
5
+
6
+ # Validate domain verification record
7
+ unione.validate_verification_record("example.com")
8
+
9
+ # Create template
10
+ template = UniOne::Template.new
11
+ template.name = 'Template Name'
12
+ template.subject = 'Email Subject'
13
+ template.template_engine = 'simple'
14
+ template.global_substitutions = {"someVar" => "someVal"}
15
+
16
+ template.from = {from_email: 'test@example.com', from_name: 'userName'}
17
+ template.headers = {"X-ReplyTo" => "reply@example.com"}
18
+ template.body = {html: "<b>Hello {{substitutionName}}</b>"}
19
+
20
+ template.attachments << {"type" => "text/plain", "name" => "myfile.txt", "content" => "ZXhhbXBsZSBmaWxl"}
21
+ template.inline_attachments << {"type" => "image/png", "name" => "IMAGECID", "content" => "iVBORw0KGgo"}
22
+ template.options = {"unsubscribe_url" => "someurl"}
23
+
24
+ response = unione.set_template(template.to_json)
25
+ template_id = response.body.template.id
@@ -0,0 +1,23 @@
1
+ require 'unione-ruby'
2
+ require 'json'
3
+
4
+ unione = UniOne::Client.new(data_center: 'eu1', lang: 'en', api_key: ENV['UNIONE_API_KEY'])
5
+
6
+ mail = UniOne::Mail.new
7
+ mail.template = {"template_engine" => "simple", "template_id" => template_id}
8
+
9
+ substitutions = {"substitutionName" => "substitutionVal", "to_name" => "Name Surname 1"}
10
+ recipient = {email: 'test1@example.com', substitutions: substitutions, metadata: {"key1" => "val1"}}
11
+ mail.recipients << recipient
12
+
13
+ substitutions = {"substitutionName" => "substitutionVal", "to_name" => "Name Surname 2"}
14
+ recipient = {email: 'test2@example.com', substitutions: substitutions, metadata: {"key1" => "val1"}}
15
+ mail.recipients << recipient
16
+
17
+ mail.metadata = {"key1" => "val1"}
18
+ mail.headers = {"X-ReplyTo" => "reply@example.com"}
19
+ mail.attachments << {"type" => "text/plain", "name" => "myfile.txt", "content" => "ZXhhbXBsZSBmaWxl"}
20
+ mail.inline_attachments << {"type" => "image/png", "name" => "IMAGECID", "content" => "iVBORw0KGgo"}
21
+ mail.options = {"unsubscribe_url" => "someurl"}
22
+
23
+ unione.send_email(mail.to_json)
@@ -0,0 +1,18 @@
1
+ require 'unione-ruby'
2
+
3
+ unione = UniOne::Client.new(data_center: 'eu1', lang: 'en', api_key: ENV['UNIONE_API_KEY'])
4
+
5
+ # Generate/get DNS records for domain verification and DKIM signing
6
+ response = unione.get_dns_records("example.com")
7
+ puts response.status
8
+ puts response.body.to_h
9
+ puts response.headers
10
+
11
+ # Validate domain verification record before use
12
+ response = unione.validate_verification_record("example.com")
13
+
14
+ # Validate DKIM signature before sending
15
+ response = unione.validate_dkim("example.com")
16
+
17
+ # Get a list of your domains and their verification/DKIM statuses
18
+ response = unione.list_domains
@@ -0,0 +1,36 @@
1
+ require 'unione-ruby'
2
+ require 'json'
3
+
4
+ # Send email
5
+ mail = UniOne::Mail.new
6
+ mail.body = {html: "<b>Hello {{substitutionName}}</b>"}
7
+ mail.from = {from_email: 'test@example.com', from_name: 'userName'}
8
+ mail.subject = 'Email Subject'
9
+ mail.template = {"template_engine" => "simple", "template_id" => "template_id"}
10
+
11
+ substitutions = {"substitutionName" => "substitutionVal", "to_name" => "Name Surname 1"}
12
+ recipient1 = {email: 'test1@example.com', substitutions: substitutions, metadata: {"key1" => "val1"}}
13
+ mail.recipients << recipient1
14
+
15
+ substitutions = {"substitutionName" => "substitutionVal", "to_name" => "Name Surname 2"}
16
+ recipient2 = {email: 'test2@example.com', substitutions: substitutions, metadata: {"key1" => "val1"}}
17
+ mail.recipients << recipient2
18
+
19
+ mail.metadata = {"key1" => "val1"}
20
+ mail.headers = {"X-ReplyTo" => "reply@example.com"}
21
+ mail.attachments << {"type" => "text/plain", "name" => "myfile.txt", "content" => "ZXhhbXBsZSBmaWxl"}
22
+ mail.inline_attachments << {"type" => "image/png", "name" => "IMAGECID", "content" => "iVBORw0KGgo"}
23
+ mail.options = {"unsubscribe_url" => "someurl"}
24
+
25
+ puts mail.to_json
26
+
27
+ unione = UniOne::Client.new(data_center: 'eu1', lang: 'en', api_key: ENV['UNIONE_API_KEY'])
28
+ response = unione.send_email(mail.to_json)
29
+ puts response.status
30
+ puts response.body
31
+ puts response.headers
32
+
33
+ # Subscribe email
34
+ unione = UniOne::Client.new(data_center: 'eu1', lang: 'en', api_key: ENV['UNIONE_API_KEY'])
35
+ response = unione.subscribe_email(email_address_from: "emailFrom",
36
+ email_address_to: "blackhole@example.com", name_from: "Example Sender")
@@ -0,0 +1,20 @@
1
+ require 'unione-ruby'
2
+
3
+ # Create project
4
+ unione = UniOne::Client.new(data_center: 'eu1', lang: 'en', api_key: ENV['UNIONE_API_KEY'])
5
+ response = unione.create_project(
6
+ {"name" => "Projectname 2", "send_enabled" => true, "custom_unsubscribe_url_enabled" => true})
7
+ puts response.status
8
+ puts response.body.to_h
9
+ puts response.headers
10
+
11
+ # Update project
12
+ response = unione.update_project("5qugsquc85i1e87xfb4spgxh8bnmi1pjfrtx1w1c",
13
+ {"name" => "Projectname 2", "send_enabled" => true, "custom_unsubscribe_url_enabled" => true})
14
+
15
+ # Delete project
16
+ response = unione.delete_project("5qegsquc85i1e87xfb4spgxh8bnmi1pjfrtx1w1c")
17
+
18
+ # List projects
19
+ response = unione.list_projects
20
+ response = unione.list_projects("5qegsquc85i1e87xfb4spgxh8bnmi1pjfrtx1w1c")
@@ -0,0 +1,36 @@
1
+ require 'unione-ruby'
2
+ require 'json'
3
+
4
+ # Set template
5
+ template = UniOne::Template.new
6
+ template.name = 'Template Name'
7
+ template.subject = 'Email Subject'
8
+ template.template_engine = 'simple'
9
+ template.global_substitutions = {"someVar" => "someVal"}
10
+
11
+ template.from = {from_email: 'test@example.com', from_name: 'userName'}
12
+ template.headers = {"X-ReplyTo" => "reply@example.com"}
13
+ template.body = {html: "<b>Hello {{substitutionName}}</b>"}
14
+
15
+ template.attachments << {"type" => "text/plain", "name" => "myfile.txt", "content" => "ZXhhbXBsZSBmaWxl"}
16
+ template.inline_attachments << {"type" => "image/png", "name" => "IMAGECID", "content" => "iVBORw0KGgo"}
17
+ template.options = {"unsubscribe_url" => "someurl"}
18
+
19
+ puts template.to_json
20
+
21
+ unione = UniOne::Client.new(data_center: 'eu1', lang: 'en', api_key: ENV['UNIONE_API_KEY'])
22
+ response = unione.set_template(template.to_json)
23
+ puts response.status
24
+ puts response.body.to_h
25
+ puts response.body.template.id # Get Id of created template for later referencing
26
+ puts response.headers
27
+
28
+ # Get template
29
+ unione = UniOne::Client.new(data_center: 'eu1', lang: 'en', api_key: ENV['UNIONE_API_KEY'])
30
+ response = unione.get_template("cef89054-40a8-4b9b-a379-22030d525c49")
31
+
32
+ # List templates
33
+ response = unione.list_templates(50, 0)
34
+
35
+ # Delete template
36
+ response = unione.delete_template("cef89054-40a8-4b9b-a379-22030d525c49")
@@ -0,0 +1,14 @@
1
+ require 'unione-ruby'
2
+
3
+ # Unsubscribe subscriber
4
+ unione = UniOne::Client.new(data_center: 'eu1', lang: 'en', api_key: ENV['UNIONE_API_KEY'])
5
+ response = unione.unsubscribe("email@example.com")
6
+ puts response.status
7
+ puts response.body.to_h
8
+ puts response.headers
9
+
10
+ # Check unsubscribed
11
+ response = unione.check_unsubscribed("email@example.com")
12
+
13
+ # List unsubscribed
14
+ response = unione.list_unsubscribed("2019-01-01")
@@ -0,0 +1,32 @@
1
+ require 'unione-ruby'
2
+ require 'json'
3
+
4
+ # Set webhook
5
+ webhook = UniOne::Webhook.new
6
+ webhook.url = "http://example.com"
7
+ webhook.settings = {event_format: "json_post", delivery_info: 1, single_event: 0, max_parallel: 10}
8
+ webhook.events = {email_status: [
9
+ "sent",
10
+ "delivered",
11
+ "opened",
12
+ "hard_bounced",
13
+ "soft_bounced",
14
+ "spam",
15
+ "clicked",
16
+ "unsubscribed"
17
+ ], spam_block: ["*"]}
18
+
19
+ puts webhook.to_json
20
+
21
+ unione = UniOne::Client.new(data_center: 'eu1', lang: 'en', api_key: ENV['UNIONE_API_KEY'])
22
+ response = unione.set_webhook(webhook.to_json)
23
+ puts response.status
24
+ puts response.body.to_h
25
+ puts response.headers
26
+
27
+ # Get webhook
28
+ unione = UniOne::Client.new(data_center: 'eu1', lang: 'en', api_key: ENV['UNIONE_API_KEY'])
29
+ response = unione.get_webhook("http://example.com")
30
+
31
+ # Delete webhook
32
+ response = unione.delete_webhook("http://example.com")
@@ -0,0 +1,5 @@
1
+ require_relative 'unione/client'
2
+ require_relative 'unione/version'
3
+ require_relative 'unione/helpers/mail/mail'
4
+ require_relative 'unione/helpers/template/template'
5
+ require_relative 'unione/helpers/webhook/webhook'
@@ -0,0 +1,44 @@
1
+ require_relative 'connection'
2
+ require_relative 'validation'
3
+ require_relative 'client/domain'
4
+ require_relative 'client/email'
5
+ require_relative 'client/template'
6
+ require_relative 'client/webhook'
7
+ require_relative 'client/unsubscribed'
8
+ require_relative 'client/project'
9
+
10
+ module UniOne
11
+
12
+ class Client
13
+
14
+ include UniOne::Connection
15
+ include UniOne::Validation
16
+ include UniOne::Client::Domain
17
+ include UniOne::Client::Email
18
+ include UniOne::Client::Template
19
+ include UniOne::Client::Webhook
20
+ include UniOne::Client::Unsubscribed
21
+ include UniOne::Client::Project
22
+
23
+ API_ENDPOINT =
24
+ 'https://%{data_center}.unione.io/%{lang}/transactional/api/v1/'
25
+
26
+ # * *Args* :
27
+ # - +data_center+ -> data center: ['eu1', 'us1']
28
+ # - +lang+ -> Two-letter ISO 639-1 language abbreviation, e.g. 'en'
29
+ # - +api_key+ -> your UniOne API key
30
+ #
31
+ def initialize(data_center:, lang:, api_key:)
32
+ @data_center = data_center
33
+ @lang = lang
34
+ @api_key = api_key
35
+ end
36
+
37
+ private
38
+
39
+ def api_endpoint
40
+ @api_endpoint ||=
41
+ API_ENDPOINT % { data_center: @data_center, lang: @lang }
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,61 @@
1
+ module UniOne
2
+ class Client
3
+
4
+ module Domain
5
+
6
+ def get_dns_records(domain)
7
+ params = { domain: domain }
8
+ post 'domain/get-dns-records.json', params
9
+ validate_response({
10
+ 'type' => 'object', 'required' => ['status', 'domain', 'verification-record', 'dkim'], 'properties' => {
11
+ 'status' => {'type' => 'string'},
12
+ 'domain' => {'type' => 'string'},
13
+ 'verification-record' => {'type' => 'string'},
14
+ 'dkim' => {'type' => 'string'}}
15
+ })
16
+ end
17
+
18
+ def validate_verification_record(domain)
19
+ params = { domain: domain }
20
+ post 'domain/validate-verification-record.json', params
21
+ validate_response({
22
+ 'type' => 'object', 'required' => ['status', 'message'], 'properties' => {
23
+ 'status' => {'type' => 'string'},
24
+ 'message' => {'type' => 'string'}}
25
+ })
26
+ end
27
+
28
+ def validate_dkim(domain)
29
+ params = { domain: domain }
30
+ post 'domain/validate-dkim.json', params
31
+ validate_response({
32
+ 'type' => 'object', 'required' => ['status', 'message'], 'properties' => {
33
+ 'status' => {'type' => 'string'},
34
+ 'message' => {'type' => 'string'}}
35
+ })
36
+ end
37
+
38
+ def list_domains
39
+ post 'domain/list.json', {}
40
+ validate_response({
41
+ 'type' => 'object', 'required' => ['status', 'domains'], 'properties' => {
42
+ 'status' => {'type' => 'string'},
43
+ 'domains' =>
44
+ {'items' =>
45
+ {'type' => 'object', 'required' => ['domain', 'verification-record', 'dkim'], 'properties' => [
46
+ 'domain' => {'type' => 'string'},
47
+ 'verification-record' =>
48
+ {'type' => 'object', 'required' => ['value', 'status'], 'properties' => [
49
+ 'value' => {'type' => 'string'},
50
+ 'status' => {'type' => 'string'}]},
51
+ 'dkim' =>
52
+ {'type' => 'object', 'required' => ['key', 'status'], 'properties' => [
53
+ 'key' => {'type' => 'string'},
54
+ 'status' => {'type' => 'string'}]}
55
+ ]}}}
56
+ })
57
+ end
58
+
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,32 @@
1
+ module UniOne
2
+ class Client
3
+
4
+ module Email
5
+
6
+ def send_email(message)
7
+ post 'email/send.json', message
8
+ validate_response({
9
+ 'type' => 'object', 'required' => ['status', 'job_id', 'emails'], 'properties' => {
10
+ 'status' => {'type' => 'string'},
11
+ 'job_id' => {'type' => 'string'},
12
+ 'emails' => {'items' => {'type' => 'string'}},
13
+ 'failed_emails' => {'type' => 'object'}}
14
+ })
15
+ end
16
+
17
+ def subscribe_email(email_address_from:, email_address_to:, name_from:)
18
+ params = {
19
+ email_address_from: email_address_from,
20
+ email_address_to: email_address_to,
21
+ name_from: name_from
22
+ }
23
+ post 'email/subscribe.json', params
24
+ validate_response({
25
+ 'type' => 'object', 'required' => ['status'], 'properties' => {
26
+ 'status' => {'type' => 'string'}}
27
+ })
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,58 @@
1
+ module UniOne
2
+ class Client
3
+
4
+ module Project
5
+
6
+ def create_project(project)
7
+ params = { project: project }
8
+ post 'project/create.json', params
9
+ validate_response({
10
+ 'type' => 'object', 'required' => ['status', 'project_api_key'], 'properties' => {
11
+ 'status' => {'type' => 'string'},
12
+ 'project_api_key' => {'type' => 'string'}}
13
+ })
14
+ end
15
+
16
+ def update_project(project_api_key, project)
17
+ params = { project_api_key: project_api_key, project: project }
18
+ post 'project/update.json', params
19
+ validate_response({
20
+ 'type' => 'object', 'required' => ['status', 'project_api_key'], 'properties' => {
21
+ 'status' => {'type' => 'string'},
22
+ 'project_api_key' => {'type' => 'string'}}
23
+ })
24
+ end
25
+
26
+ def delete_project(project_api_key)
27
+ params = { project_api_key: project_api_key }
28
+ post 'project/delete.json', params
29
+ validate_response({
30
+ 'type' => 'object', 'required' => ['status'], 'properties' => {
31
+ 'status' => {'type' => 'string'}}
32
+ })
33
+ end
34
+
35
+ def list_projects(project_api_key = nil)
36
+ params = { project_api_key: project_api_key }
37
+ post 'project/list.json', params
38
+ validate_response({
39
+ 'type' => 'object', 'required' => ['status'], 'properties' => {
40
+ 'status' => {'type' => 'string'},
41
+ 'projects' => {'items' => project_schema}}
42
+ })
43
+ end
44
+
45
+ private
46
+
47
+ def project_schema
48
+ {'type' => 'object', 'required' => ['api_key', 'name', 'reg_time', 'send_enabled', 'custom_unsubscribe_url_enabled'], 'properties' => {
49
+ 'api_key' => {'type' => 'string'},
50
+ 'name' => {'type' => 'string'},
51
+ 'reg_time' => {'type' => 'string'},
52
+ 'send_enabled' => {'type' => 'boolean'},
53
+ 'custom_unsubscribe_url_enabled' => {'type' => 'boolean'}
54
+ }}
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,80 @@
1
+ module UniOne
2
+ class Client
3
+
4
+ module Template
5
+
6
+ def set_template(template)
7
+ post 'template/set.json', template
8
+ validate_response({
9
+ 'type' => 'object', 'required' => ['status', 'template'], 'properties' => {
10
+ 'status' => {'type' => 'string'},
11
+ 'template' => template_schema}
12
+ })
13
+ end
14
+
15
+ def get_template(id)
16
+ params = { id: id }
17
+ post 'template/get.json', params
18
+ validate_response({
19
+ 'type' => 'object', 'required' => ['status', 'template'], 'properties' => {
20
+ 'status' => {'type' => 'string'},
21
+ 'template' => template_schema}
22
+ })
23
+ end
24
+
25
+ def list_templates(limit, offset)
26
+ params = { limit: limit, offset: offset }
27
+ post 'template/list.json', params
28
+ list_template_schema = remove_fields_from_schema(template_schema, ['from_name', 'headers'])
29
+ validate_response({
30
+ 'type' => 'object', 'required' => ['status', 'templates'], 'properties' => {
31
+ 'status' => {'type' => 'string'},
32
+ 'templates' => {'items' => list_template_schema}}
33
+ })
34
+ end
35
+
36
+ def delete_template(id)
37
+ params = { id: id }
38
+ post 'template/delete.json', params
39
+ validate_response({
40
+ 'type' => 'object', 'required' => ['status'], 'properties' => {
41
+ 'status' => {'type' => 'string'}}
42
+ })
43
+ end
44
+
45
+ private
46
+
47
+ def template_schema
48
+ {'type' => 'object', 'required' => ['id', 'name', 'subject', 'from_name', 'body', 'headers', 'attachments',
49
+ 'inline_attachments', 'options', 'created', 'user_id'], 'properties' => {
50
+ 'id' => {'type' => 'string'},
51
+ 'name' => {'type' => 'string'},
52
+ 'subject' => {'type' => 'string'},
53
+ 'from_name' => {'type' => 'string'},
54
+ 'body' => template_body_schema,
55
+ 'headers' => {'type' => 'object'},
56
+ 'attachments' => {'items' => {'type' => 'object'}},
57
+ 'inline_attachments' => {'items' => {'type' => 'object'}},
58
+ 'options' => template_options_schema,
59
+ 'created' => {'type' => 'string'},
60
+ 'user_id' => {'type' => 'integer'},
61
+ }}
62
+ end
63
+
64
+ def template_body_schema
65
+ {'type' => 'object', 'required' => ['html', 'plaintext', 'amp'], 'properties' => {
66
+ 'html' => {'type' => 'string, null'},
67
+ 'plaintext' => {'type' => 'string, null'},
68
+ 'amp' => {'type' => 'string, null'}
69
+ }}
70
+ end
71
+
72
+ def template_options_schema
73
+ {'type' => 'object', 'required' => ['unsubscribe_url'], 'properties' => {
74
+ 'unsubscribe_url' => {'type' => 'string'}
75
+ }}
76
+ end
77
+
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,44 @@
1
+ module UniOne
2
+ class Client
3
+
4
+ module Unsubscribed
5
+
6
+ def unsubscribe(address)
7
+ params = { address: address }
8
+ post 'unsubscribed/set.json', params
9
+ validate_response({
10
+ 'type' => 'object', 'required' => ['status', 'address', 'message'], 'properties' => {
11
+ 'status' => {'type' => 'string'},
12
+ 'address' => {'type' => 'string'},
13
+ 'message' => {'type' => 'string'}}
14
+ })
15
+ end
16
+
17
+ def check_unsubscribed(address)
18
+ params = { address: address }
19
+ post 'unsubscribed/check.json', params
20
+ validate_response({
21
+ 'type' => 'object', 'required' => ['status', 'address', 'is_unsubscribed'], 'properties' => {
22
+ 'status' => {'type' => 'string'},
23
+ 'address' => {'type' => 'string'},
24
+ 'is_unsubscribed' => {'type' => 'boolean'}}
25
+ })
26
+ end
27
+
28
+ def list_unsubscribed(date_from)
29
+ params = { date_from: date_from }
30
+ post 'unsubscribed/list.json', params
31
+ validate_response({
32
+ 'type' => 'object', 'required' => ['status', 'unsubscribed'], 'properties' => {
33
+ 'status' => {'type' => 'string'},
34
+ 'unsubscribed' => {
35
+ 'items' => {'type' => 'object'}, 'required' => ['address', 'unsubscribed_on'], 'properties' => {
36
+ 'address' => {'type' => 'string'},
37
+ 'unsubscribed_on' => {'type' => 'string'}
38
+ }}}
39
+ })
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,53 @@
1
+ module UniOne
2
+ class Client
3
+
4
+ module Webhook
5
+
6
+ def set_webhook(webhook)
7
+ post 'webhook/set.json', webhook
8
+ validate_response({'type' => 'object', 'required' => ['status', 'object'], 'properties' => {
9
+ 'status' => {'type' => 'string'},
10
+ 'object' => webhook_schema
11
+ }})
12
+ end
13
+
14
+ def get_webhook(url)
15
+ params = { url: url }
16
+ post 'webhook/get.json', params
17
+ get_webhook_schema = remove_fields_from_schema(webhook_schema, ['delivery_info', 'single_event'])
18
+ validate_response({'type' => 'object', 'required' => ['status', 'object'], 'properties' => {
19
+ 'status' => {'type' => 'string'},
20
+ 'object' => get_webhook_schema
21
+ }})
22
+ end
23
+
24
+ def delete_webhook(url)
25
+ params = { url: url }
26
+ post 'webhook/delete.json', params
27
+ validate_response({'type' => 'object', 'required' => ['status'], 'properties' => {
28
+ 'status' => {'type' => 'string'}
29
+ }})
30
+ end
31
+
32
+ private
33
+
34
+ def webhook_schema
35
+ {'type' => 'object', 'required' => ['url', 'events', 'event_format', 'delivery_info', 'single_event', 'max_parallel'], 'properties' => {
36
+ 'url' => {'type' => 'string'},
37
+ 'events' => webhook_events_schema,
38
+ 'event_format' => {'type' => 'string'},
39
+ 'delivery_info' => {'type' => 'integer'},
40
+ 'single_event' => {'type' => 'integer'},
41
+ 'max_parallel' => {'type' => 'integer'}
42
+ }}
43
+ end
44
+
45
+ def webhook_events_schema
46
+ {'type' => 'object', 'required' => ['email_status', 'spam_block'], 'properties' => {
47
+ 'email_status' => {'items' => {'type' => 'string'}},
48
+ 'spam_block' => {'items' => {'type' => 'string'}}
49
+ }}
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,54 @@
1
+ require 'json'
2
+ require 'faraday'
3
+ require 'faraday_middleware'
4
+ require 'hashie'
5
+
6
+ module UniOne
7
+ module Connection
8
+
9
+ def get(url, params)
10
+ params.merge!(default_params)
11
+ # Assume HTTP library receives params as Hash
12
+ request :get, url, params
13
+ end
14
+
15
+ def post(url, params)
16
+ params.merge!(default_params)
17
+ # Assume HTTP library receives payload body as String
18
+ request :post, url, JSON.dump(params)
19
+ end
20
+
21
+ private
22
+
23
+ def request(method, path, data)
24
+ path = add_version(path)
25
+ begin
26
+ @last_response = conn.send(method, path, data)
27
+ rescue Faraday::Error => e
28
+ puts e.response[:body]
29
+ raise
30
+ end
31
+ end
32
+
33
+ def conn
34
+ @conn ||= Faraday.new(
35
+ url: api_endpoint,
36
+ headers: {'Content-Type' => 'application/json'},
37
+ request: { timeout: 30 }
38
+ ) do |conn|
39
+ conn.response :mashify, content_type: /\bjson$/
40
+ conn.response :json, content_type: /\bjson$/
41
+ conn.response :raise_error
42
+ conn.adapter :net_http_persistent
43
+ end
44
+ end
45
+
46
+ def default_params
47
+ { api_key: @api_key }
48
+ end
49
+
50
+ def add_version(path)
51
+ "#{path}?platform=unione_ruby_#{UniOne::VERSION}"
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,37 @@
1
+ module UniOne
2
+ class Mail
3
+ attr_accessor :template, :body, :track, :from, :subject, :metadata, :headers, :options,
4
+ :global_substitutions, :recipients, :attachments, :inline_attachments,
5
+ :reply_to
6
+
7
+ def initialize
8
+ @template = {}
9
+ @from = {}
10
+ @track = {}
11
+ @global_substitutions = {}
12
+ @recipients = []
13
+ @attachments = []
14
+ @inline_attachments = []
15
+ end
16
+
17
+ def to_json(*)
18
+ {
19
+ message: {
20
+ global_substitutions: self.global_substitutions,
21
+ body: self.body,
22
+ subject: self.subject,
23
+ reply_to: self.reply_to,
24
+ recipients: self.recipients,
25
+ metadata: self.metadata,
26
+ headers: self.headers,
27
+ attachments: self.attachments,
28
+ inline_attachments: self.inline_attachments,
29
+ options: self.options
30
+ }.merge(self.template)
31
+ .merge(self.from)
32
+ .merge(self.track)
33
+ .delete_if { |_, value| value.to_s.strip == '' || value == [] || value == {}}
34
+ }
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,31 @@
1
+ module UniOne
2
+ class Template
3
+ attr_accessor :name, :subject, :template_engine, :global_substitutions, :from,
4
+ :headers, :body, :attachments, :inline_attachments, :options
5
+
6
+ def initialize
7
+ @global_substitutions = {}
8
+ @from = {}
9
+ @attachments = []
10
+ @inline_attachments = []
11
+ end
12
+
13
+ def to_json(*)
14
+ {
15
+ template: {
16
+ id: '',
17
+ name: self.name,
18
+ subject: self.subject,
19
+ template_engine: self.template_engine,
20
+ global_substitutions: self.global_substitutions,
21
+ headers: self.headers,
22
+ body: self.body,
23
+ attachments: self.attachments,
24
+ inline_attachments: self.inline_attachments,
25
+ options: self.options
26
+ }.merge(self.from)
27
+ .delete_if { |_, value| value.to_s.strip == '' || value == [] || value == {}}
28
+ }
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,17 @@
1
+ module UniOne
2
+ class Webhook
3
+ attr_accessor :url, :settings, :events
4
+
5
+ def initialize
6
+ @settings = {}
7
+ end
8
+
9
+ def to_json(*)
10
+ {
11
+ url: self.url,
12
+ events: self.events
13
+ }.merge(self.settings)
14
+ .delete_if { |_, value| value.to_s.strip == '' || value == [] || value == {}}
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ require 'faraday'
2
+ require 'unione/error'
3
+
4
+ module UniOne
5
+ # UniOne response middleware
6
+ module Response
7
+
8
+ # This class raises an UniOne-flavored exception based
9
+ # HTTP status codes returned by the API
10
+ class RaiseError < Faraday::Response::Middleware
11
+
12
+ private
13
+
14
+ def on_complete(response)
15
+ if error = UniOne::Error.from_response(response)
16
+ raise error
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ require 'json-schema'
2
+
3
+ module UniOne
4
+ module Validation
5
+
6
+ private
7
+
8
+ def validate_response(schema)
9
+ begin
10
+ JSON::Validator.validate!(schema, @last_response.body)
11
+ rescue JSON::Schema::ValidationError => e
12
+ puts @last_response.body
13
+ raise
14
+ end
15
+ @last_response
16
+ end
17
+
18
+ def remove_fields_from_schema(schema, fields)
19
+ schema.dup.tap do |s|
20
+ s['required'] = s['required'].reject { |f| fields.include?(f) }
21
+ s['properties'] = s['properties'].reject { |f| fields.include?(f) }
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module UniOne
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'unione/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.add_dependency 'json', '~> 2.0'
8
+ spec.add_dependency 'faraday', '~> 1.0'
9
+ spec.add_dependency 'faraday_middleware', '~> 1.0'
10
+ spec.add_dependency 'net-http-persistent', '~> 3.0'
11
+ spec.add_dependency 'hashie', '~> 4.0'
12
+ spec.add_dependency 'json-schema', '~> 2.0'
13
+ spec.authors = ['UniOne developer']
14
+ spec.description = 'Official UniOne Gem to Interact with UniOne Api in native Ruby'
15
+ spec.email = 'devlib@unione.io'
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.homepage = 'https://github.com/unione-repo/unione-ruby'
18
+ spec.licenses = ['MIT']
19
+ spec.name = 'unione-ruby'
20
+ spec.require_paths = ['lib']
21
+ spec.required_ruby_version = '>= 2.4'
22
+ spec.summary = 'Official UniOne Gem'
23
+ spec.version = UniOne::VERSION
24
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unione-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - UniOne developer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday_middleware
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: net-http-persistent
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: hashie
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: json-schema
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.0'
97
+ description: Official UniOne Gem to Interact with UniOne Api in native Ruby
98
+ email: devlib@unione.io
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - LICENSE
104
+ - README.md
105
+ - examples/1_setup.rb
106
+ - examples/2_mail_sending.rb
107
+ - examples/api/domain.rb
108
+ - examples/api/email.rb
109
+ - examples/api/project.rb
110
+ - examples/api/template.rb
111
+ - examples/api/unsubscribed.rb
112
+ - examples/api/webhook.rb
113
+ - lib/unione-ruby.rb
114
+ - lib/unione/client.rb
115
+ - lib/unione/client/domain.rb
116
+ - lib/unione/client/email.rb
117
+ - lib/unione/client/project.rb
118
+ - lib/unione/client/template.rb
119
+ - lib/unione/client/unsubscribed.rb
120
+ - lib/unione/client/webhook.rb
121
+ - lib/unione/connection.rb
122
+ - lib/unione/helpers/mail/mail.rb
123
+ - lib/unione/helpers/template/template.rb
124
+ - lib/unione/helpers/webhook/webhook.rb
125
+ - lib/unione/response/raise_error.rb
126
+ - lib/unione/validation.rb
127
+ - lib/unione/version.rb
128
+ - unione-ruby.gemspec
129
+ homepage: https://github.com/unione-repo/unione-ruby
130
+ licenses:
131
+ - MIT
132
+ metadata: {}
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '2.4'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubyforge_project:
149
+ rubygems_version: 2.7.6
150
+ signing_key:
151
+ specification_version: 4
152
+ summary: Official UniOne Gem
153
+ test_files: []