unigo-ruby 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +133 -0
- data/Rakefile +52 -0
- data/config/response_schema/domain.yml +74 -0
- data/config/response_schema/email.yml +23 -0
- data/config/response_schema/event_dump.yml +75 -0
- data/config/response_schema/project.yml +59 -0
- data/config/response_schema/suppression.yml +71 -0
- data/config/response_schema/system.yml +33 -0
- data/config/response_schema/tag.yml +26 -0
- data/config/response_schema/template.yml +134 -0
- data/config/response_schema/unsubscribed.yml +45 -0
- data/config/response_schema/webhook.yml +141 -0
- data/examples/api/custom_api.rb +14 -0
- data/examples/api/domain.rb +25 -0
- data/examples/api/email/attachments.rb +28 -0
- data/examples/api/email/multiple_recipients.rb +24 -0
- data/examples/api/email/send.rb +25 -0
- data/examples/api/email/settings.rb +41 -0
- data/examples/api/email/subscribe.rb +14 -0
- data/examples/api/email/template.rb +26 -0
- data/examples/api/email.rb +51 -0
- data/examples/api/event_dump.rb +44 -0
- data/examples/api/project.rb +44 -0
- data/examples/api/suppression.rb +34 -0
- data/examples/api/system.rb +10 -0
- data/examples/api/tag.rb +15 -0
- data/examples/api/template.rb +54 -0
- data/examples/api/unsubscribed.rb +22 -0
- data/examples/api/webhook.rb +42 -0
- data/examples/helpers.rb +29 -0
- data/examples/send_mail.rb +44 -0
- data/examples/sending_mail.rb +32 -0
- data/examples/setup.rb +34 -0
- data/lib/unigo/client/domain.rb +34 -0
- data/lib/unigo/client/email.rb +25 -0
- data/lib/unigo/client/event_dump.rb +37 -0
- data/lib/unigo/client/project.rb +34 -0
- data/lib/unigo/client/suppression.rb +38 -0
- data/lib/unigo/client/system.rb +19 -0
- data/lib/unigo/client/tag.rb +24 -0
- data/lib/unigo/client/template.rb +34 -0
- data/lib/unigo/client/unsubscribed.rb +31 -0
- data/lib/unigo/client/webhook.rb +34 -0
- data/lib/unigo/client.rb +69 -0
- data/lib/unigo/connection.rb +62 -0
- data/lib/unigo/helpers/mail/mail.rb +42 -0
- data/lib/unigo/helpers/template/template.rb +32 -0
- data/lib/unigo/helpers/webhook/webhook.rb +18 -0
- data/lib/unigo/helpers.rb +22 -0
- data/lib/unigo/response/raise_error.rb +20 -0
- data/lib/unigo/validation.rb +45 -0
- data/lib/unigo/version.rb +3 -0
- data/lib/unigo-ruby.rb +2 -0
- data/test/CONFIGFILE.yml +71 -0
- data/unigo-ruby.gemspec +24 -0
- metadata +182 -0
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'unigo-ruby'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
unigo = UniGo::Client.new(
|
5
|
+
hostname: 'go1.unisender.ru',
|
6
|
+
lang: 'ru',
|
7
|
+
api_key: ENV['UNIGO_API_KEY']
|
8
|
+
)
|
9
|
+
|
10
|
+
# Set webhook
|
11
|
+
params = {}
|
12
|
+
|
13
|
+
params[:url] = 'http://example.com'
|
14
|
+
params[:status] = :active
|
15
|
+
|
16
|
+
params[:event_format] = :json_post
|
17
|
+
params[:delivery_info] = 1
|
18
|
+
params[:single_event] = 0
|
19
|
+
params[:max_parallel] = 10
|
20
|
+
|
21
|
+
params[:events] = {
|
22
|
+
email_status: %w(sent delivered opened hard_bounced soft_bounced spam clicked unsubscribed),
|
23
|
+
spam_block: ["*"]
|
24
|
+
}
|
25
|
+
|
26
|
+
response = unigo.set_webhook(params)
|
27
|
+
|
28
|
+
# Get webhook
|
29
|
+
response = unigo.get_webhook(
|
30
|
+
url: 'http://example.com'
|
31
|
+
)
|
32
|
+
|
33
|
+
# List all webhooks
|
34
|
+
response = unigo.list_webhooks(
|
35
|
+
limit: 50,
|
36
|
+
offset: 0
|
37
|
+
)
|
38
|
+
|
39
|
+
# Delete webhook
|
40
|
+
response = unigo.delete_webhook(
|
41
|
+
url: 'http://example.com'
|
42
|
+
)
|
data/examples/helpers.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'unigo-ruby'
|
2
|
+
|
3
|
+
unigo = UniGo::Client.new(
|
4
|
+
hostname: 'go1.unisender.ru',
|
5
|
+
lang: 'ru',
|
6
|
+
api_key: ENV['UNIGO_API_KEY']
|
7
|
+
)
|
8
|
+
|
9
|
+
# Verify webhook callback message
|
10
|
+
# Will raise UniGo::Client::InvalidCallbackAuth error unless `auth' field is correct.
|
11
|
+
unigo.verify_callback_auth!(params)
|
12
|
+
|
13
|
+
# Callback helper usage
|
14
|
+
# Will raise UniGo::Client::InvalidCallbackAuth error unless `auth' field is correct.
|
15
|
+
unigo.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 'unigo-ruby'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
unigo = UniGo::Client.new(
|
5
|
+
hostname: 'go1.unisender.ru',
|
6
|
+
lang: 'ru',
|
7
|
+
api_key: ENV['UNIGO_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@example.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 = unigo.send_email(message: message)
|
41
|
+
|
42
|
+
puts response.status
|
43
|
+
puts response.body.to_h
|
44
|
+
puts response.headers
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'unigo-ruby'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
unigo = UniGo::Client.new(data_center: 'eu1', lang: 'ru', api_key: ENV['UNIGO_API_KEY'])
|
5
|
+
|
6
|
+
mail = UniGo::Mail.new
|
7
|
+
|
8
|
+
template_id = "bc0372d4-69f2-11eb-9d14-366b3a36c9e1"
|
9
|
+
mail.template = { template_engine: 'simple', template_id: template_id }
|
10
|
+
|
11
|
+
mail.from = { from_email: 'test@example.net', from_name: 'userName' }
|
12
|
+
|
13
|
+
substitutions = { 'substitutionName' => 'substitutionVal', to_name: 'Name Surname 1' }
|
14
|
+
recipient = { email: 'test1@example.com', substitutions: substitutions, metadata: { key1: 'val1' } }
|
15
|
+
mail.recipients << recipient
|
16
|
+
|
17
|
+
substitutions = { 'substitutionName' => 'substitutionVal', to_name: 'Name Surname 2' }
|
18
|
+
recipient = { email: 'test2@example.com', substitutions: substitutions, metadata: { key1: 'val1' } }
|
19
|
+
mail.recipients << recipient
|
20
|
+
|
21
|
+
mail.global_metadata = { key1: 'val1' }
|
22
|
+
mail.headers = { 'X-ReplyTo' => 'reply@example.com' }
|
23
|
+
mail.options = { unsubscribe_url: 'someurl' }
|
24
|
+
|
25
|
+
mail.attachments << { type: 'text/plain', name: 'myfile.txt', content: 'ZXhhbXBsZSBmaWxl' }
|
26
|
+
mail.inline_attachments << { type: 'image/png', name: 'IMAGECID', content: 'iVBORw0KGgo' }
|
27
|
+
|
28
|
+
response = unigo.send_email(mail.to_json)
|
29
|
+
|
30
|
+
puts response.status
|
31
|
+
puts response.body.to_h
|
32
|
+
puts response.headers
|
data/examples/setup.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'unigo-ruby'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
unigo = UniGo::Client.new(
|
5
|
+
hostname: 'go1.unisender.ru',
|
6
|
+
lang: 'ru',
|
7
|
+
api_key: ENV['UNIGO_API_KEY']
|
8
|
+
)
|
9
|
+
|
10
|
+
# Validate domain verification record
|
11
|
+
unigo.validate_verification_record(domain: 'example.net')
|
12
|
+
|
13
|
+
# Create template
|
14
|
+
template = {}
|
15
|
+
|
16
|
+
template[:name] = 'Template Name'
|
17
|
+
template[:subject] = 'Email Subject'
|
18
|
+
template[:template_engine] = 'simple'
|
19
|
+
template[:headers] = {'X-ReplyTo' => 'reply@example.com'}
|
20
|
+
|
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' }
|
26
|
+
|
27
|
+
template[:attachments] = []
|
28
|
+
template[:attachments] << { type: 'text/plain', name: 'myfile.txt', content: 'ZXhhbXBsZSBmaWxl' }
|
29
|
+
|
30
|
+
template[:inline_attachments] = []
|
31
|
+
template[:inline_attachments] << { type: 'image/png', name: 'IMAGECID', content: 'iVBORw0KGgo' }
|
32
|
+
|
33
|
+
response = unigo.set_template(template: template)
|
34
|
+
template_id = response.body.template.id
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module UniGo
|
2
|
+
class Client
|
3
|
+
module Domain
|
4
|
+
extend UniGo::Validation::ClassMethods
|
5
|
+
include UniGo::Validation::InstanceMethods
|
6
|
+
|
7
|
+
def get_dns_records(params = {})
|
8
|
+
post('domain/get-dns-records.json', params)
|
9
|
+
end
|
10
|
+
|
11
|
+
def validate_verification_record(params = {})
|
12
|
+
post('domain/validate-verification-record.json', params)
|
13
|
+
end
|
14
|
+
|
15
|
+
def validate_dkim(params = {})
|
16
|
+
post('domain/validate-dkim.json', params)
|
17
|
+
end
|
18
|
+
|
19
|
+
def list_domains(params = {})
|
20
|
+
post('domain/list.json', params)
|
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
|
+
)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module UniGo
|
2
|
+
class Client
|
3
|
+
module Email
|
4
|
+
extend UniGo::Validation::ClassMethods
|
5
|
+
include UniGo::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)
|
10
|
+
end
|
11
|
+
|
12
|
+
def subscribe_email(params = {})
|
13
|
+
post('email/subscribe.json', params)
|
14
|
+
end
|
15
|
+
|
16
|
+
add_response_validations(
|
17
|
+
:email,
|
18
|
+
%w(
|
19
|
+
send_email
|
20
|
+
subscribe_email
|
21
|
+
)
|
22
|
+
)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module UniGo
|
2
|
+
class Client
|
3
|
+
module EventDump
|
4
|
+
extend UniGo::Validation::ClassMethods
|
5
|
+
include UniGo::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
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module UniGo
|
2
|
+
class Client
|
3
|
+
module Project
|
4
|
+
extend UniGo::Validation::ClassMethods
|
5
|
+
include UniGo::Validation::InstanceMethods
|
6
|
+
|
7
|
+
def create_project(params = {})
|
8
|
+
post('project/create.json', params)
|
9
|
+
end
|
10
|
+
|
11
|
+
def update_project(params = {})
|
12
|
+
post('project/update.json', params)
|
13
|
+
end
|
14
|
+
|
15
|
+
def list_projects(params = {})
|
16
|
+
post('project/list.json', params)
|
17
|
+
end
|
18
|
+
|
19
|
+
def delete_project(params = {})
|
20
|
+
post('project/delete.json', params)
|
21
|
+
end
|
22
|
+
|
23
|
+
add_response_validations(
|
24
|
+
:project,
|
25
|
+
%w(
|
26
|
+
create_project
|
27
|
+
update_project
|
28
|
+
list_projects
|
29
|
+
delete_project
|
30
|
+
)
|
31
|
+
)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module UniGo
|
2
|
+
class Client
|
3
|
+
module Suppression
|
4
|
+
extend UniGo::Validation::ClassMethods
|
5
|
+
include UniGo::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
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module UniGo
|
2
|
+
class Client
|
3
|
+
module System
|
4
|
+
extend UniGo::Validation::ClassMethods
|
5
|
+
include UniGo::Validation::InstanceMethods
|
6
|
+
|
7
|
+
def info(params = {})
|
8
|
+
post('system/info.json', params)
|
9
|
+
end
|
10
|
+
|
11
|
+
add_response_validations(
|
12
|
+
:system,
|
13
|
+
%w(
|
14
|
+
info
|
15
|
+
)
|
16
|
+
)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module UniGo
|
2
|
+
class Client
|
3
|
+
module Tag
|
4
|
+
extend UniGo::Validation::ClassMethods
|
5
|
+
include UniGo::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
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module UniGo
|
2
|
+
class Client
|
3
|
+
module Template
|
4
|
+
extend UniGo::Validation::ClassMethods
|
5
|
+
include UniGo::Validation::InstanceMethods
|
6
|
+
|
7
|
+
def set_template(params = {})
|
8
|
+
post('template/set.json', params)
|
9
|
+
end
|
10
|
+
|
11
|
+
def get_template(params = {})
|
12
|
+
post('template/get.json', params)
|
13
|
+
end
|
14
|
+
|
15
|
+
def list_templates(params = {})
|
16
|
+
post('template/list.json', params)
|
17
|
+
end
|
18
|
+
|
19
|
+
def delete_template(params = {})
|
20
|
+
post('template/delete.json', params)
|
21
|
+
end
|
22
|
+
|
23
|
+
add_response_validations(
|
24
|
+
:template,
|
25
|
+
%w(
|
26
|
+
set_template
|
27
|
+
get_template
|
28
|
+
list_templates
|
29
|
+
delete_template
|
30
|
+
)
|
31
|
+
)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module UniGo
|
2
|
+
class Client
|
3
|
+
module Unsubscribed
|
4
|
+
extend UniGo::Validation::ClassMethods
|
5
|
+
include UniGo::Validation::InstanceMethods
|
6
|
+
|
7
|
+
def unsubscribe(params = {})
|
8
|
+
post('unsubscribed/set.json', params)
|
9
|
+
end
|
10
|
+
|
11
|
+
def check_unsubscribed(params = {})
|
12
|
+
post('unsubscribed/check.json', params)
|
13
|
+
end
|
14
|
+
|
15
|
+
def list_unsubscribed(params = {})
|
16
|
+
params[:date_from] = handle_date_param(params[:date_from]) if params[:date_from]
|
17
|
+
|
18
|
+
post('unsubscribed/list.json', params)
|
19
|
+
end
|
20
|
+
|
21
|
+
add_response_validations(
|
22
|
+
:unsubscribed,
|
23
|
+
%w(
|
24
|
+
unsubscribe
|
25
|
+
check_unsubscribed
|
26
|
+
list_unsubscribed
|
27
|
+
)
|
28
|
+
)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module UniGo
|
2
|
+
class Client
|
3
|
+
module Webhook
|
4
|
+
extend UniGo::Validation::ClassMethods
|
5
|
+
include UniGo::Validation::InstanceMethods
|
6
|
+
|
7
|
+
def set_webhook(params = {})
|
8
|
+
post('webhook/set.json', params)
|
9
|
+
end
|
10
|
+
|
11
|
+
def get_webhook(params = {})
|
12
|
+
post('webhook/get.json', params)
|
13
|
+
end
|
14
|
+
|
15
|
+
def list_webhooks(params = {})
|
16
|
+
post('webhook/list.json', params)
|
17
|
+
end
|
18
|
+
|
19
|
+
def delete_webhook(params = {})
|
20
|
+
post('webhook/delete.json', params)
|
21
|
+
end
|
22
|
+
|
23
|
+
add_response_validations(
|
24
|
+
:webhook,
|
25
|
+
%w(
|
26
|
+
set_webhook
|
27
|
+
get_webhook
|
28
|
+
list_webhooks
|
29
|
+
delete_webhook
|
30
|
+
)
|
31
|
+
)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/unigo/client.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require_relative 'connection'
|
2
|
+
require_relative 'validation'
|
3
|
+
require_relative 'helpers'
|
4
|
+
require_relative 'client/domain'
|
5
|
+
require_relative 'client/email'
|
6
|
+
require_relative 'client/event_dump'
|
7
|
+
require_relative 'client/project'
|
8
|
+
require_relative 'client/suppression'
|
9
|
+
require_relative 'client/system'
|
10
|
+
require_relative 'client/tag'
|
11
|
+
require_relative 'client/template'
|
12
|
+
require_relative 'client/unsubscribed'
|
13
|
+
require_relative 'client/webhook'
|
14
|
+
|
15
|
+
module UniGo
|
16
|
+
class Client
|
17
|
+
include UniGo::Connection
|
18
|
+
extend UniGo::Validation::ClassMethods
|
19
|
+
include UniGo::Validation::InstanceMethods
|
20
|
+
include UniGo::Client::Helpers
|
21
|
+
include UniGo::Client::Domain
|
22
|
+
include UniGo::Client::Email
|
23
|
+
include UniGo::Client::EventDump
|
24
|
+
include UniGo::Client::Project
|
25
|
+
include UniGo::Client::Suppression
|
26
|
+
include UniGo::Client::System
|
27
|
+
include UniGo::Client::Tag
|
28
|
+
include UniGo::Client::Template
|
29
|
+
include UniGo::Client::Unsubscribed
|
30
|
+
include UniGo::Client::Webhook
|
31
|
+
|
32
|
+
class BaseException < StandardError; end
|
33
|
+
class InvalidCallbackAuth < BaseException; end
|
34
|
+
|
35
|
+
API_ENDPOINT =
|
36
|
+
'https://%{hostname}/%{lang}/transactional/api/v1/'
|
37
|
+
|
38
|
+
# * *Args* :
|
39
|
+
# - +hostname+ -> string, API hostname, for example 'go1.unisender.ru'
|
40
|
+
# - +lang+ -> string, two-letter ISO 639-1 language abbreviation, e.g. 'ru'
|
41
|
+
# - +api_key+ -> string, your Unisender Go API key
|
42
|
+
# - +timeout+ -> integer, timeout in seconds, 5 by default
|
43
|
+
# - +api_key_in_params+ -> boolean, pass API key in parameters, otherwise pass in headers (default)
|
44
|
+
# - +enable_logging+ -> boolean, enable logging
|
45
|
+
def initialize(params = {})
|
46
|
+
@hostname = params[:hostname]
|
47
|
+
@lang = params[:lang]
|
48
|
+
@api_key = params[:api_key]
|
49
|
+
@timeout = params[:timeout] || 5
|
50
|
+
@api_key_in_params = params[:api_key_in_params]
|
51
|
+
@enable_logging = params[:enable_logging] || ENV['ENABLE_LOGGING']
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def api_endpoint
|
57
|
+
@api_endpoint ||=
|
58
|
+
API_ENDPOINT % { hostname: @hostname, lang: @lang }
|
59
|
+
end
|
60
|
+
|
61
|
+
def handle_time_param(param)
|
62
|
+
param.respond_to?(:strftime) ? param.strftime('%Y-%m-%d %H:%M:%S') : param
|
63
|
+
end
|
64
|
+
|
65
|
+
def handle_date_param(param)
|
66
|
+
param.respond_to?(:strftime) ? param.strftime('%Y-%m-%d') : param
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'faraday'
|
3
|
+
require 'faraday_middleware'
|
4
|
+
require 'hashie'
|
5
|
+
|
6
|
+
module UniGo
|
7
|
+
module Connection
|
8
|
+
|
9
|
+
def get(url, params = {})
|
10
|
+
prepare_params!(params)
|
11
|
+
|
12
|
+
# Assume HTTP library receives params as Hash
|
13
|
+
request(:get, url, params)
|
14
|
+
end
|
15
|
+
|
16
|
+
def post(url, params = {})
|
17
|
+
prepare_params!(params)
|
18
|
+
|
19
|
+
# Assume HTTP library receives payload body as String
|
20
|
+
request(:post, url, JSON.dump(params))
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def request(method, path, data)
|
26
|
+
path = add_version(path)
|
27
|
+
@last_response = conn.send(method, path, data)
|
28
|
+
end
|
29
|
+
|
30
|
+
def conn
|
31
|
+
headers = {'Content-Type' => 'application/json'}
|
32
|
+
prepare_headers!(headers)
|
33
|
+
|
34
|
+
@conn ||= Faraday.new(
|
35
|
+
url: api_endpoint,
|
36
|
+
headers: headers,
|
37
|
+
request: { timeout: @timeout }
|
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
|
+
|
44
|
+
if @enable_logging
|
45
|
+
conn.response(:logger, nil, { headers: true, bodies: true, errors: true })
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def prepare_params!(params)
|
51
|
+
params.merge!({api_key: @api_key}) if @api_key_in_params
|
52
|
+
end
|
53
|
+
|
54
|
+
def prepare_headers!(headers)
|
55
|
+
headers.merge!('X-API-KEY' => @api_key) unless @api_key_in_params
|
56
|
+
end
|
57
|
+
|
58
|
+
def add_version(path)
|
59
|
+
"#{path}?platform=unigo_ruby_#{UniGo::VERSION}"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module UniGo
|
2
|
+
class Mail
|
3
|
+
attr_accessor :template, :body, :track, :from, :subject, :global_metadata, :headers, :options,
|
4
|
+
:global_substitutions, :recipients, :attachments, :inline_attachments,
|
5
|
+
:reply_to, :skip_unsubscribe, :force_send
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@template = {}
|
9
|
+
@from = {}
|
10
|
+
@track = {}
|
11
|
+
@global_substitutions = {}
|
12
|
+
@recipients = []
|
13
|
+
@attachments = []
|
14
|
+
@inline_attachments = []
|
15
|
+
end
|
16
|
+
|
17
|
+
# backward compatibility
|
18
|
+
alias_method :"metadata=", :"global_metadata="
|
19
|
+
|
20
|
+
def to_json(*)
|
21
|
+
{
|
22
|
+
message: {
|
23
|
+
global_substitutions: self.global_substitutions,
|
24
|
+
body: self.body,
|
25
|
+
subject: self.subject,
|
26
|
+
reply_to: self.reply_to,
|
27
|
+
recipients: self.recipients,
|
28
|
+
global_metadata: self.global_metadata,
|
29
|
+
headers: self.headers,
|
30
|
+
attachments: self.attachments,
|
31
|
+
inline_attachments: self.inline_attachments,
|
32
|
+
options: self.options,
|
33
|
+
skip_unsubscribe: self.skip_unsubscribe,
|
34
|
+
force_send: self.force_send
|
35
|
+
}.merge(self.template)
|
36
|
+
.merge(self.from)
|
37
|
+
.merge(self.track)
|
38
|
+
.delete_if { |_, value| value.to_s.strip == '' || value == [] || value == {}}
|
39
|
+
}
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|