unione-ruby 0.0.2 → 1.0.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.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +90 -9
  3. data/Rakefile +49 -0
  4. data/config/response_schema/domain.yml +74 -0
  5. data/config/response_schema/email.yml +23 -0
  6. data/config/response_schema/event_dump.yml +75 -0
  7. data/config/response_schema/project.yml +59 -0
  8. data/config/response_schema/suppression.yml +71 -0
  9. data/config/response_schema/system.yml +33 -0
  10. data/config/response_schema/tag.yml +26 -0
  11. data/config/response_schema/template.yml +134 -0
  12. data/config/response_schema/unsubscribed.yml +45 -0
  13. data/config/response_schema/webhook.yml +141 -0
  14. data/examples/api/custom_api.rb +14 -0
  15. data/examples/api/domain.rb +14 -7
  16. data/examples/api/email/attachments.rb +28 -0
  17. data/examples/api/email/multiple_recipients.rb +24 -0
  18. data/examples/api/email/send.rb +25 -0
  19. data/examples/api/email/settings.rb +41 -0
  20. data/examples/api/email/subscribe.rb +14 -0
  21. data/examples/api/email/template.rb +26 -0
  22. data/examples/api/event_dump.rb +44 -0
  23. data/examples/api/project.rb +30 -23
  24. data/examples/api/suppression.rb +34 -0
  25. data/examples/api/system.rb +6 -6
  26. data/examples/api/tag.rb +15 -0
  27. data/examples/api/template.rb +40 -28
  28. data/examples/api/unsubscribed.rb +17 -11
  29. data/examples/api/webhook.rb +28 -25
  30. data/examples/helpers.rb +29 -0
  31. data/examples/send_mail.rb +44 -0
  32. data/examples/setup.rb +21 -14
  33. data/lib/unione/client/domain.rb +21 -45
  34. data/lib/unione/client/email.rb +16 -15
  35. data/lib/unione/client/event_dump.rb +37 -0
  36. data/lib/unione/client/project.rb +19 -61
  37. data/lib/unione/client/suppression.rb +38 -0
  38. data/lib/unione/client/system.rb +12 -17
  39. data/lib/unione/client/tag.rb +24 -0
  40. data/lib/unione/client/template.rb +19 -66
  41. data/lib/unione/client/unsubscribed.rb +18 -31
  42. data/lib/unione/client/webhook.rb +19 -65
  43. data/lib/unione/client.rb +33 -11
  44. data/lib/unione/connection.rb +7 -3
  45. data/lib/unione/helpers.rb +22 -0
  46. data/lib/unione/response/raise_error.rb +4 -5
  47. data/lib/unione/validation.rb +34 -9
  48. data/lib/unione/version.rb +1 -1
  49. data/lib/unione-ruby.rb +0 -3
  50. data/test/CONFIGFILE.yml +71 -0
  51. data/unione-ruby.gemspec +2 -2
  52. metadata +32 -10
  53. data/examples/api/email.rb +0 -51
  54. data/examples/sending_mail.rb +0 -32
  55. data/lib/unione/helpers/mail/mail.rb +0 -42
  56. data/lib/unione/helpers/template/template.rb +0 -32
  57. data/lib/unione/helpers/webhook/webhook.rb +0 -18
@@ -1,16 +1,22 @@
1
1
  require 'unione-ruby'
2
2
 
3
- unione = UniOne::Client.new(data_center: 'eu1', lang: 'en', api_key: ENV['UNIONE_API_KEY'])
3
+ unione = UniOne::Client.new(
4
+ hostname: 'eu1.unione.io',
5
+ lang: 'en',
6
+ api_key: ENV['UNIONE_API_KEY']
7
+ )
4
8
 
5
- # unsubscribe email
6
- response = unione.unsubscribe('email@example.com')
9
+ # Unsubscribe email
10
+ response = unione.unsubscribe(
11
+ address: 'email@example.com'
12
+ )
7
13
 
8
- puts response.status
9
- puts response.body.to_h
10
- puts response.headers
14
+ # Check email is unsubscribed
15
+ response = unione.check_unsubscribed(
16
+ address: 'email@example.com'
17
+ )
11
18
 
12
- # check email is unsubscribed
13
- response = unione.check_unsubscribed('email@example.com')
14
-
15
- # list unsubscribed emails
16
- response = unione.list_unsubscribed('2019-01-01')
19
+ # List unsubscribed emails
20
+ response = unione.list_unsubscribed(
21
+ date_from: Date.today # Or String: '2019-01-01'
22
+ )
@@ -1,39 +1,42 @@
1
1
  require 'unione-ruby'
2
2
  require 'json'
3
3
 
4
- unione = UniOne::Client.new(data_center: 'eu1', lang: 'en', api_key: ENV['UNIONE_API_KEY'])
4
+ unione = UniOne::Client.new(
5
+ hostname: 'eu1.unione.io',
6
+ lang: 'en',
7
+ api_key: ENV['UNIONE_API_KEY']
8
+ )
5
9
 
6
- # set webhook
7
- webhook = UniOne::Webhook.new
10
+ # Set webhook
11
+ params = {}
8
12
 
9
- webhook.url = 'http://example.com'
10
- webhook.status = :active
13
+ params[:url] = 'http://example.com'
14
+ params[:status] = :active
11
15
 
12
- webhook.settings = {
13
- event_format: :json_post,
14
- delivery_info: 1,
15
- single_event: 0,
16
- max_parallel: 10
17
- }
16
+ params[:event_format] = :json_post
17
+ params[:delivery_info] = 1
18
+ params[:single_event] = 0
19
+ params[:max_parallel] = 10
18
20
 
19
- webhook.events = {
21
+ params[:events] = {
20
22
  email_status: %w(sent delivered opened hard_bounced soft_bounced spam clicked unsubscribed),
21
23
  spam_block: ["*"]
22
24
  }
23
25
 
24
- puts webhook.to_json
25
-
26
- response = unione.set_webhook(webhook.to_json)
27
-
28
- puts response.status
29
- puts response.body.to_h
30
- puts response.headers
26
+ response = unione.set_webhook(params)
31
27
 
32
- # get webhook
33
- response = unione.get_webhook('http://example.com')
28
+ # Get webhook
29
+ response = unione.get_webhook(
30
+ url: 'http://example.com'
31
+ )
34
32
 
35
- # list all webhooks
36
- response = unione.list_webhooks(50, 0)
33
+ # List all webhooks
34
+ response = unione.list_webhooks(
35
+ limit: 50,
36
+ offset: 0
37
+ )
37
38
 
38
- # delete webhook
39
- response = unione.delete_webhook('http://example.com')
39
+ # Delete webhook
40
+ response = unione.delete_webhook(
41
+ url: 'http://example.com'
42
+ )
@@ -0,0 +1,29 @@
1
+ require 'unione-ruby'
2
+
3
+ unione = UniOne::Client.new(
4
+ hostname: 'eu1.unione.io',
5
+ lang: 'en',
6
+ api_key: ENV['UNIONE_API_KEY']
7
+ )
8
+
9
+ # Verify webhook callback message
10
+ # Will raise Unione::Client::InvalidCallbackAuth error unless `auth' field is correct.
11
+ unione.verify_callback_auth!(params)
12
+
13
+ # Callback helper usage
14
+ # Will raise Unione::Client::InvalidCallbackAuth error unless `auth' field is correct.
15
+ unione.callback_helper(params) do |events|
16
+ # [
17
+ # {
18
+ # 'event_name' => 'transactional_email_status',
19
+ # 'event_data' =>
20
+ # {
21
+ # 'email' => 'recipient.email@example.com',
22
+ # 'status' => 'sent',
23
+ # 'event_time' => '2015-11-30 15:09:42',
24
+ # ...
25
+ # }
26
+ # },
27
+ # ...
28
+ # ]
29
+ end
@@ -0,0 +1,44 @@
1
+ require 'unione-ruby'
2
+ require 'json'
3
+
4
+ unione = UniOne::Client.new(
5
+ hostname: 'eu1.unione.io',
6
+ lang: 'en',
7
+ api_key: ENV['UNIONE_API_KEY'],
8
+ enable_logging: true
9
+ )
10
+
11
+ message = {}
12
+
13
+ message[:template_engine] = 'simple'
14
+ message[:template_id] = 'template_id'
15
+
16
+ # In case template_id is not specified, subject and body are mandatory
17
+ message[:subject] = 'test letter'
18
+ message[:body] = {"html": "tests", "plaintext": "plaintext"}
19
+
20
+ message[:from_email] = 'test@qadns.net'
21
+ message[:from_name] = 'userName'
22
+
23
+ message[:recipients] = []
24
+ message[:recipients] << {
25
+ email: 'test1@example.com',
26
+ substitutions: { 'substitutionName' => 'substitutionVal', to_name: 'Name Surname' },
27
+ metadata: { key1: 'val' },
28
+ }
29
+
30
+ message[:global_metadata] = { key1: 'val' }
31
+ message[:headers] = { 'X-ReplyTo' => 'reply@example.com' }
32
+ message[:options] = { unsubscribe_url: 'someurl' }
33
+
34
+ message[:attachments] = []
35
+ message[:attachments] << { type: 'text/plain', name: 'myfile.txt', content: 'ZXhhbXBsZSBmaWxl' }
36
+
37
+ message[:inline_attachments] = []
38
+ message[:inline_attachments] << { type: 'image/png', name: 'IMAGECID', content: 'iVBORw0KGgo' }
39
+
40
+ response = unione.send_email(message: message)
41
+
42
+ puts response.status
43
+ puts response.body.to_h
44
+ puts response.headers
data/examples/setup.rb CHANGED
@@ -1,27 +1,34 @@
1
1
  require 'unione-ruby'
2
2
  require 'json'
3
3
 
4
- unione = UniOne::Client.new(data_center: 'eu1', lang: 'en', api_key: ENV['UNIONE_API_KEY'])
4
+ unione = UniOne::Client.new(
5
+ hostname: 'eu1.unione.io',
6
+ lang: 'en',
7
+ api_key: ENV['UNIONE_API_KEY']
8
+ )
5
9
 
6
10
  # Validate domain verification record
7
- unione.validate_verification_record('example.com')
11
+ unione.validate_verification_record(domain: 'qadns.net')
8
12
 
9
13
  # Create template
10
- template = UniOne::Template.new
14
+ template = {}
11
15
 
12
- template.name = 'Template Name'
13
- template.subject = 'Email Subject'
14
- template.template_engine = 'simple'
15
- template.headers = { 'X-ReplyTo' => 'reply@example.com' }
16
- template.global_substitutions = { 'someVar' => 'someVal' }
16
+ template[:name] = 'Template Name'
17
+ template[:subject] = 'Email Subject'
18
+ template[:template_engine] = 'simple'
19
+ template[:headers] = {'X-ReplyTo' => 'reply@example.com'}
17
20
 
18
- template.from = { from_email: 'test@example.com', from_name: 'userName' }
19
- template.body = { html: '<b>Hello {{substitutionName}}</b>' }
21
+ template[:global_substitutions] = { 'someVar' => 'someVal' }
22
+ template[:from_email] = 'test@example.com'
23
+ template[:from_name] = 'userName'
24
+ template[:body] = { html: '<b>Hello {{substitutionName}}</b>' }
25
+ template[:options] = { unsubscribe_url: 'someurl' }
20
26
 
21
- template.attachments << { type: 'text/plain', name: 'myfile.txt', content: 'ZXhhbXBsZSBmaWxl' }
22
- template.inline_attachments << { type: 'image/png', name: 'IMAGECID', content: 'iVBORw0KGgo' }
27
+ template[:attachments] = []
28
+ template[:attachments] << { type: 'text/plain', name: 'myfile.txt', content: 'ZXhhbXBsZSBmaWxl' }
23
29
 
24
- template.options = { unsubscribe_url: 'someurl' }
30
+ template[:inline_attachments] = []
31
+ template[:inline_attachments] << { type: 'image/png', name: 'IMAGECID', content: 'iVBORw0KGgo' }
25
32
 
26
- response = unione.set_template(template.to_json)
33
+ response = unione.set_template(template: template)
27
34
  template_id = response.body.template.id
@@ -1,58 +1,34 @@
1
1
  module UniOne
2
2
  class Client
3
3
  module Domain
4
- def get_dns_records(domain)
5
- params = { domain: domain }
6
- post 'domain/get-dns-records.json', params
7
- validate_response({
8
- 'type' => 'object', 'required' => ['status', 'domain', 'verification-record', 'dkim'], 'properties' => {
9
- 'status' => {'type' => 'string'},
10
- 'domain' => {'type' => 'string'},
11
- 'verification-record' => {'type' => 'string'},
12
- 'dkim' => {'type' => 'string'}}
13
- })
4
+ extend UniOne::Validation::ClassMethods
5
+ include UniOne::Validation::InstanceMethods
6
+
7
+ def get_dns_records(params = {})
8
+ post('domain/get-dns-records.json', params)
14
9
  end
15
10
 
16
- def validate_verification_record(domain)
17
- params = { domain: domain }
18
- post 'domain/validate-verification-record.json', params
19
- validate_response({
20
- 'type' => 'object', 'required' => ['status', 'message'], 'properties' => {
21
- 'status' => {'type' => 'string'},
22
- 'message' => {'type' => 'string'}}
23
- })
11
+ def validate_verification_record(params = {})
12
+ post('domain/validate-verification-record.json', params)
24
13
  end
25
14
 
26
- def validate_dkim(domain)
27
- params = { domain: domain }
28
- post 'domain/validate-dkim.json', params
29
- validate_response({
30
- 'type' => 'object', 'required' => ['status', 'message'], 'properties' => {
31
- 'status' => {'type' => 'string'},
32
- 'message' => {'type' => 'string'}}
33
- })
15
+ def validate_dkim(params = {})
16
+ post('domain/validate-dkim.json', params)
34
17
  end
35
18
 
36
- def list_domains
37
- post 'domain/list.json', {}
38
- validate_response({
39
- 'type' => 'object', 'required' => ['status', 'domains'], 'properties' => {
40
- 'status' => {'type' => 'string'},
41
- 'domains' =>
42
- {'items' =>
43
- {'type' => 'object', 'required' => ['domain', 'verification-record', 'dkim'], 'properties' => [
44
- 'domain' => {'type' => 'string'},
45
- 'verification-record' =>
46
- {'type' => 'object', 'required' => ['value', 'status'], 'properties' => [
47
- 'value' => {'type' => 'string'},
48
- 'status' => {'type' => 'string'}]},
49
- 'dkim' =>
50
- {'type' => 'object', 'required' => ['key', 'status'], 'properties' => [
51
- 'key' => {'type' => 'string'},
52
- 'status' => {'type' => 'string'}]}
53
- ]}}}
54
- })
19
+ def list_domains(params = {})
20
+ post('domain/list.json', params)
55
21
  end
22
+
23
+ add_response_validations(
24
+ :domain,
25
+ %w(
26
+ get_dns_records
27
+ validate_verification_record
28
+ validate_dkim
29
+ list_domains
30
+ )
31
+ )
56
32
  end
57
33
  end
58
34
  end
@@ -1,24 +1,25 @@
1
1
  module UniOne
2
2
  class Client
3
3
  module Email
4
- def send_email(message)
5
- post 'email/send.json', message
6
- validate_response({
7
- 'type' => 'object', 'required' => ['status', 'job_id', 'emails'], 'properties' => {
8
- 'status' => {'type' => 'string'},
9
- 'job_id' => {'type' => 'string'},
10
- 'emails' => {'items' => {'type' => 'string'}},
11
- 'failed_emails' => {'type' => 'object'}}
12
- })
4
+ extend UniOne::Validation::ClassMethods
5
+ include UniOne::Validation::InstanceMethods
6
+
7
+ def send_email(params = {})
8
+ params[:message][:options][:send_at] = handle_time_param(params.dig(:message, :options, :send_at)) if params.dig(:message, :options, :send_at)
9
+ post('email/send.json', params)
13
10
  end
14
11
 
15
- def subscribe_email(params)
16
- post 'email/subscribe.json', params
17
- validate_response({
18
- 'type' => 'object', 'required' => ['status'], 'properties' => {
19
- 'status' => {'type' => 'string'}}
20
- })
12
+ def subscribe_email(params = {})
13
+ post('email/subscribe.json', params)
21
14
  end
15
+
16
+ add_response_validations(
17
+ :email,
18
+ %w(
19
+ send_email
20
+ subscribe_email
21
+ )
22
+ )
22
23
  end
23
24
  end
24
25
  end
@@ -0,0 +1,37 @@
1
+ module UniOne
2
+ class Client
3
+ module EventDump
4
+ extend UniOne::Validation::ClassMethods
5
+ include UniOne::Validation::InstanceMethods
6
+
7
+ def create_event_dump(params = {})
8
+ params[:start_time] = handle_time_param(params[:start_time]) if params[:start_time]
9
+ params[:end_time] = handle_time_param(params[:end_time]) if params[:end_time]
10
+
11
+ post('event-dump/create.json', params)
12
+ end
13
+
14
+ def get_event_dump(params = {})
15
+ post('event-dump/get.json', params)
16
+ end
17
+
18
+ def list_event_dumps(params = {})
19
+ post('event-dump/list.json', params)
20
+ end
21
+
22
+ def delete_event_dump(params = {})
23
+ post('event-dump/delete.json', params)
24
+ end
25
+
26
+ add_response_validations(
27
+ :event_dump,
28
+ %w(
29
+ create_event_dump
30
+ get_event_dump
31
+ list_event_dumps
32
+ delete_event_dump
33
+ )
34
+ )
35
+ end
36
+ end
37
+ end
@@ -1,76 +1,34 @@
1
1
  module UniOne
2
2
  class Client
3
3
  module Project
4
- def create_project(project)
5
- params = { project: project }
6
- post 'project/create.json', params
7
- validate_response({
8
- 'type' => 'object', 'required' => ['status', 'project_id', 'project_api_key'], 'properties' => {
9
- 'status' => {'type' => 'string'},
10
- 'project_id' => {'type' => 'string'},
11
- 'project_api_key' => {'type' => 'string'}}
12
- })
13
- end
4
+ extend UniOne::Validation::ClassMethods
5
+ include UniOne::Validation::InstanceMethods
14
6
 
15
- def update_project(project_identity, project)
16
- params = get_identity(project_identity).merge(project: project)
17
- post 'project/update.json', params
18
- validate_response({
19
- 'type' => 'object', 'required' => ['status'], 'properties' => {
20
- 'status' => {'type' => 'string'},
21
- 'project_api_key' => {'type' => 'string'}}
22
- })
7
+ def create_project(params = {})
8
+ post('project/create.json', params)
23
9
  end
24
10
 
25
- def list_projects(project_identity = nil)
26
- params = get_identity(project_identity)
27
- post 'project/list.json', params
28
- validate_response({
29
- 'type' => 'object',
30
- 'required' => %w{ status },
31
- 'properties' => {
32
- 'status' => { 'type' => 'string' },
33
- 'projects' => { 'items' => project_schema }
34
- }
35
- })
11
+ def update_project(params = {})
12
+ post('project/update.json', params)
36
13
  end
37
14
 
38
- def delete_project(project_identity)
39
- params = get_identity(project_identity)
40
- post 'project/delete.json', params
41
- validate_response({
42
- 'type' => 'object',
43
- 'required' => %w{ status },
44
- 'properties' => {
45
- 'status' => { 'type' => 'string' }
46
- }
47
- })
15
+ def list_projects(params = {})
16
+ post('project/list.json', params)
48
17
  end
49
18
 
50
- private
51
-
52
- def get_identity(project_identity)
53
- if project_identity.is_a?(Hash)
54
- project_identity
55
- elsif project_identity
56
- { project_api_key: project_identity }
57
- else
58
- {}
59
- end
19
+ def delete_project(params = {})
20
+ post('project/delete.json', params)
60
21
  end
61
22
 
62
- def project_schema
63
- {'type' => 'object',
64
- 'required' => %w{ id api_key name reg_time send_enabled custom_unsubscribe_url_enabled },
65
- 'properties' => {
66
- 'id' => {'type' => 'string'},
67
- 'api_key' => {'type' => 'string'},
68
- 'name' => {'type' => 'string'},
69
- 'reg_time' => {'type' => 'string'},
70
- 'send_enabled' => {'type' => 'boolean'},
71
- 'custom_unsubscribe_url_enabled' => {'type' => 'boolean'}
72
- }}
73
- end
23
+ add_response_validations(
24
+ :project,
25
+ %w(
26
+ create_project
27
+ update_project
28
+ list_projects
29
+ delete_project
30
+ )
31
+ )
74
32
  end
75
33
  end
76
34
  end
@@ -0,0 +1,38 @@
1
+ module UniOne
2
+ class Client
3
+ module Suppression
4
+ extend UniOne::Validation::ClassMethods
5
+ include UniOne::Validation::InstanceMethods
6
+
7
+ def set_suppression(params = {})
8
+ params[:created] = handle_time_param(params[:created]) if params[:created]
9
+
10
+ post('suppression/set.json', params)
11
+ end
12
+
13
+ def get_suppression(params = {})
14
+ post('suppression/get.json', params)
15
+ end
16
+
17
+ def list_suppressions(params = {})
18
+ params[:start_time] = handle_time_param(params[:start_time]) if params[:start_time]
19
+
20
+ post('suppression/list.json', params)
21
+ end
22
+
23
+ def delete_suppression(params = {})
24
+ post('suppression/delete.json', params)
25
+ end
26
+
27
+ add_response_validations(
28
+ :suppression,
29
+ %w(
30
+ set_suppression
31
+ get_suppression
32
+ list_suppressions
33
+ delete_suppression
34
+ )
35
+ )
36
+ end
37
+ end
38
+ end
@@ -1,24 +1,19 @@
1
1
  module UniOne
2
2
  class Client
3
3
  module System
4
- def info
5
- post 'system/info.json', {}
6
- validate_response({
7
- 'type' => 'object', 'required' => ['status', 'user_id', 'email'], 'properties' => {
8
- 'status' => {'type' => 'string'},
9
- 'user_id' => {'type' => 'integer'},
10
- 'email' => {'type' => 'string'},
11
- 'accounting' =>
12
- {'type' => 'object', 'required' => ['period_start', 'period_end', 'emails_included', 'emails_sent'], 'properties' => [
13
- 'period_start' => {'type' => 'string'},
14
- 'period_end' => {'type' => 'string'},
15
- 'emails_included' => {'type' => 'integer'},
16
- 'emails_sent' => {'type' => 'integer'}
17
- ]},
18
- 'project_id' => {'type' => 'string'},
19
- 'project_name' => {'type' => 'string'}}
20
- })
4
+ extend UniOne::Validation::ClassMethods
5
+ include UniOne::Validation::InstanceMethods
6
+
7
+ def info(params = {})
8
+ post('system/info.json', params)
21
9
  end
10
+
11
+ add_response_validations(
12
+ :system,
13
+ %w(
14
+ info
15
+ )
16
+ )
22
17
  end
23
18
  end
24
19
  end
@@ -0,0 +1,24 @@
1
+ module UniOne
2
+ class Client
3
+ module Tag
4
+ extend UniOne::Validation::ClassMethods
5
+ include UniOne::Validation::InstanceMethods
6
+
7
+ def list_tags(params = {})
8
+ post('tag/list.json', params)
9
+ end
10
+
11
+ def delete_tag(params = {})
12
+ post('tag/delete.json', params)
13
+ end
14
+
15
+ add_response_validations(
16
+ :tag,
17
+ %w(
18
+ list_tags
19
+ delete_tag
20
+ )
21
+ )
22
+ end
23
+ end
24
+ end