dashamail_transactional 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 92b17a0fa9a6bb122fbfd652a10260398a3483a436c3fa67a2d88a976d189d40
4
+ data.tar.gz: 6744d496614686e699adc0b53ce666facffce67884aebe5a209e0d8d6e6d9755
5
+ SHA512:
6
+ metadata.gz: 50eacf3ad356b15416978f99c2b31954afc6f5da5668aaf6fe71251d5db95e4dcc6019767e2c03357da9a274cb13d8f5907fa967252a4cd89b3aab6be5ab07eb
7
+ data.tar.gz: b4cc77e44821c1c970be7b72ce3a0e87c0fddd0484d1fcc4c23c78958340a4107c82ae2f079e37853b097591bcb8fee48492c75be9d2c6526f41540134bb845b
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: single_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Metrics/CyclomaticComplexity:
13
+ Enabled: false
14
+
15
+ Style/Documentation:
16
+ Enabled: false
17
+
18
+ Metrics/AbcSize:
19
+ Enabled: false
20
+
21
+ Layout/LineLength:
22
+ Max: 120
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in dashamail.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ gem 'minitest', '~> 5.0'
11
+
12
+ gem 'rubocop', '~> 1.21'
data/Gemfile.lock ADDED
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ dashamail (0.1.0)
5
+ mime-types (>= 3.4.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ json (2.6.1)
12
+ mime-types (3.4.1)
13
+ mime-types-data (~> 3.2015)
14
+ mime-types-data (3.2023.0218.1)
15
+ minitest (5.16.3)
16
+ parallel (1.22.1)
17
+ parser (3.1.3.0)
18
+ ast (~> 2.4.1)
19
+ rainbow (3.1.1)
20
+ rake (13.0.6)
21
+ regexp_parser (2.6.1)
22
+ rexml (3.2.5)
23
+ rubocop (1.39.0)
24
+ json (~> 2.3)
25
+ parallel (~> 1.10)
26
+ parser (>= 3.1.2.1)
27
+ rainbow (>= 2.2.2, < 4.0)
28
+ regexp_parser (>= 1.8, < 3.0)
29
+ rexml (>= 3.2.5, < 4.0)
30
+ rubocop-ast (>= 1.23.0, < 2.0)
31
+ ruby-progressbar (~> 1.7)
32
+ unicode-display_width (>= 1.4.0, < 3.0)
33
+ rubocop-ast (1.23.0)
34
+ parser (>= 3.1.1.0)
35
+ ruby-progressbar (1.11.0)
36
+ unicode-display_width (1.8.0)
37
+
38
+ PLATFORMS
39
+ x86_64-darwin-21
40
+
41
+ DEPENDENCIES
42
+ dashamail!
43
+ minitest (~> 5.0)
44
+ rake (~> 13.0)
45
+ rubocop (~> 1.21)
46
+
47
+ BUNDLED WITH
48
+ 2.3.26
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 creadone
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # DashaMail
2
+
3
+ Гем для работы с транзакционным API российского сервиса https://dashamail.ru, с его методами и параметрами можно ознакомиться на [официальном ресурсе](https://dashamail.ru/transactional/).
4
+
5
+ ## Установка
6
+
7
+ Укажите в проекте загрузку гема из гитхаба:
8
+
9
+ ```sh
10
+ gem 'dashamail', git: 'https://github.com/creadone/dashamail'
11
+ ```
12
+ Затем установите с помощью Bundler
13
+
14
+ ```sh
15
+ bundle install
16
+ ```
17
+
18
+ ## Использование
19
+
20
+ Сконфигурируйте гем в своем проекте:
21
+ ```ruby
22
+ DashaMail.configure do |config|
23
+ config.api_key = 'ваш API-ключ'
24
+ config.domain = 'my-domain.ru'
25
+ config.from_email = 'marketing@my-domain.ru'
26
+ config.from_name = 'Отдел маркетинга'
27
+ # config.http_debug = true
28
+ end
29
+ ```
30
+
31
+ Отправить письмо:
32
+ ```ruby
33
+ mailer = DashaMail::Mailer.new
34
+ mailer.to = 'marketing@customer.ru'
35
+ mailer.subject = 'Какой-то заголовок'
36
+ mailer.message = '<p>Какой-то текст</p><img src="cid:123">'
37
+
38
+ mailer.add_attachment './attachment.png'
39
+ mailer.add_inline './inline_attachment.png', 123
40
+
41
+ response = mailer.send
42
+
43
+ if response.success?
44
+ pp response.body
45
+ # {"msg"=>
46
+ # {"err_code"=>0,
47
+ # "text"=>"OK",
48
+ # "type"=>"message"},
49
+ # "data"=>
50
+ # {"transaction_id"=>"5a802b10ba82eccfd164f3c8be0fb678"}}
51
+
52
+ pp response.transaction_id #=> 5a802b10ba82eccfd164f3c8be0fb678
53
+ end
54
+ ```
55
+
56
+ Проверить статус отправки письма:
57
+
58
+ ```ruby
59
+ mailer = DashaMail::Mailer.new
60
+ response = mailer.check('5a802b10ba82eccfd164f3c8be0fb678')
61
+
62
+ pp response.data
63
+ # {"data"=>
64
+ # {"date"=>"2023-02-22 19:14:42",
65
+ # "datesent"=>"2023-02-22 19:14:42",
66
+ # "to"=>"marketing@customer.ru",
67
+ # "status"=>5,
68
+ # "statusname"=>"Sent",
69
+ # "statuschangedate"=>"2023-02-22 19:14:42"},
70
+ # "success"=>true}
71
+ ```
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/creadone/dashamail.
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/test_*.rb']
10
+ end
11
+
12
+ require 'rubocop/rake_task'
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DashaMail
4
+ class Composer
5
+ def initialize
6
+ @config = DashaMail.config
7
+ @container = {
8
+ to: nil,
9
+ from_name: @config.from_name,
10
+ from_email: @config.from_email,
11
+ ignore_delivery_policy: @config.ignore_delivery_policy,
12
+ no_track_clicks: @config.no_track_clicks,
13
+ no_track_opens: @config.no_track_opens
14
+ }
15
+ end
16
+
17
+ def to=(to)
18
+ @container[:to] = to
19
+ end
20
+
21
+ def cc=(cc)
22
+ @container[:cc] = cc
23
+ end
24
+
25
+ def bcc=(bcc)
26
+ @container[:bcc] = bcc
27
+ end
28
+
29
+ def from_email=(from_email)
30
+ @container[:from_email] = from_email
31
+ end
32
+
33
+ def from_name=(from_name)
34
+ @container[:from_name] = from_name
35
+ end
36
+
37
+ def subject=(subject)
38
+ @container[:subject] = subject
39
+ end
40
+
41
+ def message=(message)
42
+ @container[:message] = message
43
+ end
44
+
45
+ def plain_text=(plain_text)
46
+ @container[:plain_text] = plain_text
47
+ end
48
+
49
+ def message_id=(message_id)
50
+ @container[:message_id] = message_id
51
+ end
52
+
53
+ def delivery_time=(delivery_time)
54
+ @container[:delivery_time] = delivery_time
55
+ end
56
+
57
+ def replace=(replace)
58
+ @container[:replace] = replace
59
+ end
60
+
61
+ def domain=(domain)
62
+ @container[:domain] = domain
63
+ end
64
+
65
+ def headers=(headers)
66
+ @container[:headers] = headers
67
+ end
68
+
69
+ def template_data=(template_data)
70
+ @container[:template_data] = template_data
71
+ end
72
+
73
+ def add_attachment(file_path)
74
+ @container[:attachments] = [] unless @container.key?(:attachments)
75
+ @container[:attachments].push Utils.build_attachment(file_path)
76
+ end
77
+
78
+ def add_inline(file_path, cid)
79
+ @container[:inline] = [] unless @container.key?(:inline)
80
+ @container[:inline].push Utils.build_inline(file_path, cid)
81
+ end
82
+
83
+ def no_track_opens=(no_track_opens)
84
+ @container[:no_track_opens] = no_track_opens
85
+ end
86
+
87
+ def no_track_clicks=(no_track_clicks)
88
+ @container[:no_track_clicks] = no_track_clicks
89
+ end
90
+
91
+ def ignore_delivery_policy=(ignore_delivery_policy)
92
+ @container[:ignore_delivery_policy] = ignore_delivery_policy
93
+ end
94
+
95
+ def call
96
+ @container
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DashaMail
4
+ module Configuration
5
+ def self.api_key
6
+ @api_key
7
+ end
8
+
9
+ def self.api_key=(api_key)
10
+ @api_key = api_key
11
+ end
12
+
13
+ def self.domain
14
+ @domain
15
+ end
16
+
17
+ def self.domain=(domain)
18
+ @domain = domain
19
+ end
20
+
21
+ def self.from_email
22
+ @from_email
23
+ end
24
+
25
+ def self.from_email=(from_email)
26
+ @from_email = from_email
27
+ end
28
+
29
+ def self.from_name
30
+ @from_name
31
+ end
32
+
33
+ def self.from_name=(from_name)
34
+ @from_name = from_name
35
+ end
36
+
37
+ def self.no_track_opens
38
+ @no_track_opens
39
+ end
40
+
41
+ def self.no_track_opens=(no_track_opens)
42
+ @no_track_opens = no_track_opens
43
+ end
44
+
45
+ def self.no_track_clicks
46
+ @no_track_clicks
47
+ end
48
+
49
+ def self.no_track_clicks=(no_track_clicks)
50
+ @no_track_clicks = no_track_clicks
51
+ end
52
+
53
+ def self.ignore_delivery_policy
54
+ @ignore_delivery_policy
55
+ end
56
+
57
+ def self.ignore_delivery_policy=(ignore_delivery_policy)
58
+ @ignore_delivery_policy = ignore_delivery_policy
59
+ end
60
+
61
+ def self.end_point
62
+ @end_point
63
+ end
64
+
65
+ def self.end_point=(end_point)
66
+ @end_point = end_point
67
+ end
68
+
69
+ def self.response_format
70
+ @response_format
71
+ end
72
+
73
+ def self.response_format=(response_format)
74
+ @response_format = response_format
75
+ end
76
+
77
+ def self.logger
78
+ @logger
79
+ end
80
+
81
+ def self.logger=(logger)
82
+ @logger = logger
83
+ end
84
+
85
+ def self.http_debug
86
+ @http_debug
87
+ end
88
+
89
+ def self.http_debug=(http_debug)
90
+ @http_debug = http_debug
91
+ end
92
+
93
+ DEFAULT = {
94
+ end_point: 'https://api.dashamail.ru',
95
+ no_track_opens: true,
96
+ no_track_clicks: true,
97
+ response_format: 'JSON',
98
+ ignore_delivery_policy: false
99
+ }.freeze
100
+
101
+ DEFAULT.each do |param, default_value|
102
+ send("#{param}=", default_value)
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uri'
4
+ require 'json'
5
+ require 'net/http'
6
+
7
+ module DashaMail
8
+ class HTTP
9
+ VERBS = {
10
+ get: Net::HTTP::Get,
11
+ post: Net::HTTP::Post
12
+ }.freeze
13
+
14
+ def initialize(options = {})
15
+ @config = DashaMail.config
16
+ @credentials = {
17
+ end_point: @config.end_point,
18
+ api_key: @config.api_key
19
+ }.merge!(options)
20
+ end
21
+
22
+ def base_headers(options = {})
23
+ @headers = {
24
+ 'User-Agent' => "DashaMail(Ruby)/#{DashaMail::VERSION}",
25
+ 'Content-Type' => 'application/json'
26
+ }.merge!(options)
27
+ end
28
+
29
+ def get(path, options = {})
30
+ execute(path, :get, options)
31
+ end
32
+
33
+ def post(path, options = {})
34
+ execute(path, :post, options)
35
+ end
36
+
37
+ private
38
+
39
+ def execute(path, method, options = {})
40
+ uri_params = add_param(@credentials[:end_point], 'method', path)
41
+ uri = URI(uri_params)
42
+
43
+ req = VERBS[method].new(uri)
44
+
45
+ options.merge!(api_key: @credentials[:api_key])
46
+ options.transform_keys!(&:to_s)
47
+
48
+ headers = base_headers
49
+ headers.each { |k, v| req[k] = v }
50
+ req.body = (options || {}).to_json
51
+
52
+ http = Net::HTTP.new(uri.host, uri.port)
53
+ http.use_ssl = (uri.scheme == 'https')
54
+ http.set_debug_output($stdout) if @config.http_debug
55
+
56
+ http.request(req)
57
+ end
58
+
59
+ def add_param(url, param_name, param_value)
60
+ uri = URI(url)
61
+ params = URI.decode_www_form(uri.query || '') << [param_name, param_value]
62
+ uri.query = URI.encode_www_form(params)
63
+ uri.to_s
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DashaMail
4
+ class Mailer
5
+ def initialize
6
+ @composer = Composer.new
7
+ end
8
+
9
+ def send
10
+ message = @composer.call
11
+ Request.new.send(message)
12
+ end
13
+
14
+ def check(transaction_id)
15
+ Request.new.check(transaction_id)
16
+ end
17
+
18
+ def method_missing(method, *args)
19
+ @composer.send(method, *args)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DashaMail
4
+ class Request
5
+ def initialize
6
+ @http = DashaMail::HTTP.new
7
+ end
8
+
9
+ def send(message)
10
+ answer = @http.post('transactional.send', message)
11
+ Response.new(answer)
12
+ end
13
+
14
+ def check(transaction_id)
15
+ answer = @http.get('transactional.check', { transaction_id: transaction_id })
16
+ Response.new(answer)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DashaMail
4
+ class Response
5
+ def initialize(answer)
6
+ @answer = answer
7
+ @body = JSON.parse(@answer.body)['response']
8
+ end
9
+
10
+ def success?
11
+ @body.dig('msg', 'err_code').to_i.zero?
12
+ end
13
+
14
+ def http_code
15
+ @answer.code
16
+ end
17
+
18
+ def text
19
+ @body.dig('msg', 'text')
20
+ end
21
+
22
+ def transaction_id
23
+ @body.dig('data', 'transaction_id')
24
+ end
25
+
26
+ def data
27
+ @body['data']
28
+ end
29
+
30
+ attr_reader :body
31
+
32
+ def raw
33
+ @answer
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'base64'
4
+ require 'mime-types'
5
+
6
+ module DashaMail
7
+ class Utils
8
+ def self.build_attachment(file_path)
9
+ raise StandardError, 'File not exist' unless File.exist?(file_path)
10
+
11
+ file_name = File.basename(file_path)
12
+ file_body = File.open(file_path, 'rb') do |io|
13
+ Base64.strict_encode64(io.read)
14
+ end
15
+ { name: file_name, filebody: file_body }
16
+ end
17
+
18
+ def self.build_inline(file_path, cid)
19
+ raise StandardError, 'File not exist' unless File.exist?(file_path)
20
+
21
+ file_name = File.basename(file_path)
22
+ mime_type = MIME::Types.type_for(file_path)[0].to_s
23
+ file_body = File.open(file_path, 'rb') do |io|
24
+ Base64.strict_encode64(io.read)
25
+ end
26
+ {
27
+ mime_type: mime_type,
28
+ filename: file_name,
29
+ body: file_body,
30
+ cid: cid
31
+ }
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DashaMail
4
+ VERSION = '0.1.0'
5
+ end
data/lib/dashamail.rb ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dashamail/version'
4
+ require 'dashamail/configuration'
5
+ require 'dashamail/utils'
6
+ require 'dashamail/http'
7
+ require 'dashamail/composer'
8
+ require 'dashamail/mailer'
9
+ require 'dashamail/response'
10
+ require 'dashamail/request'
11
+
12
+ module DashaMail
13
+ def self.configure
14
+ yield DashaMail::Configuration
15
+ end
16
+
17
+ def self.config
18
+ DashaMail::Configuration
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dashamail_transactional
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Fedorov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-12-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mime-types
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.4.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.4.0
27
+ description: Ruby client DashaMail for transactional API
28
+ email:
29
+ - creadone@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rubocop.yml"
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - lib/dashamail.rb
41
+ - lib/dashamail/composer.rb
42
+ - lib/dashamail/configuration.rb
43
+ - lib/dashamail/http.rb
44
+ - lib/dashamail/mailer.rb
45
+ - lib/dashamail/request.rb
46
+ - lib/dashamail/response.rb
47
+ - lib/dashamail/utils.rb
48
+ - lib/dashamail/version.rb
49
+ homepage:
50
+ licenses:
51
+ - MIT
52
+ metadata:
53
+ homepage_uri: https://github.com/creadone/dashamail
54
+ source_code_uri: https://github.com/creadone/dashamail
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.7.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.3.27
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Ruby client DashaMail for transactional API
74
+ test_files: []