mailtrap 1.1.1 → 1.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 23acecf40de87c9ec6c68cee1a63244548f699819ce122b9f37877e6429a25e9
4
- data.tar.gz: 5e2009cefd12428d8274ac79a57644f3b5bfcd6b4e266afc62ee5d274b22daa2
3
+ metadata.gz: 7dee4a202b02e16f840e8fc74526903e10418a0073c2392faf964dcdbcdfcde4
4
+ data.tar.gz: d35cf9fa125e8ec587f96626be7830f4398002173b7dc07c4560dddbbb0ddb3e
5
5
  SHA512:
6
- metadata.gz: '00157801f3ae86ee16ffb51850c4af77f0213d736371fffacd3799c045f60955455f27c03283d8df16b41c5f0c635cfc22c1808c7d268c7b40c3d2ccd672e92c'
7
- data.tar.gz: f8b80546c0dd317a64882cfb61ddbc8e646057da495508913fbefc9f26cd75a71129d807752a3cd41aa4df5ad38888e7c4a06247d2bbce8233af52d6507aa3e3
6
+ metadata.gz: 5b4c0ba57ab2cab64cec15704bd34c04be134360a2600d2fc530bcae47ab8caf2e8dd79ce7ba5fd342e46dad599153d473006613c953da868182dcff2bb1e2be
7
+ data.tar.gz: 45d64a548b0cd54e0f98394a8c6739d1bc5dcd90716105100f7bae92f7aa5913c9ff66fde412d8965d3818bc7d6594c1c218d96cfcde59eabc6bd92c1901e2c3
data/.rubocop.yml CHANGED
@@ -3,6 +3,8 @@ require: rubocop-rspec
3
3
  AllCops:
4
4
  TargetRubyVersion: 2.7
5
5
  NewCops: enable
6
+ Exclude:
7
+ - 'gemfiles/**/*'
6
8
 
7
9
  Layout/LineLength:
8
10
  Max: 120
@@ -10,6 +12,9 @@ Layout/LineLength:
10
12
  Naming/MethodParameterName:
11
13
  MinNameLength: 2
12
14
 
15
+ Naming/VariableNumber:
16
+ EnforcedStyle: snake_case
17
+
13
18
  RSpec/MultipleExpectations:
14
19
  Max: 4
15
20
 
data/Appraisals ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ appraise 'mail-2.7' do
4
+ gem 'mail', '~> 2.7.0'
5
+ end
6
+
7
+ appraise 'mail-2.8' do
8
+ gem 'mail', '~> 2.8.0'
9
+ end
data/CHANGELOG.md CHANGED
@@ -1,16 +1,27 @@
1
- ## [1.0.0] - 2022-06-14
1
+ ## [1.2.1] - 2023-04-12
2
2
 
3
- - Initial release
3
+ - Set custom user agent
4
4
 
5
- ## [1.0.1] - 2022-06-20
5
+ ## [1.2.0] - 2023-01-27
6
6
 
7
- - Update packed files list
7
+ - Breaking changes:
8
+ - move `Mailtrap::Sending::Mail` class to `Mailtrap::Mail::Base`
9
+ - move `Mailtrap::Sending::Convert` to `Mailtrap::Mail`
10
+ - Add mail gem 2.8 support
11
+ - Add email template support
8
12
 
13
+ ## [1.1.1] - 2022-10-14
14
+
15
+ - Fix custom port and host usage
9
16
 
10
17
  ## [1.1.0] - 2022-07-22
11
18
 
12
19
  - Add ActionMailer support
13
20
 
14
- ## [1.1.1] - 2022-10-14
21
+ ## [1.0.1] - 2022-06-20
15
22
 
16
- - Fix custom port and host usage
23
+ - Update packed files list
24
+
25
+ ## [1.0.0] - 2022-06-14
26
+
27
+ - Initial release
data/Gemfile CHANGED
@@ -4,6 +4,7 @@ source 'https://rubygems.org'
4
4
 
5
5
  gemspec
6
6
 
7
+ gem 'appraisal'
7
8
  gem 'mail'
8
9
  gem 'net-smtp'
9
10
  gem 'rake', '~> 13.0'
data/Gemfile.lock CHANGED
@@ -1,13 +1,17 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mailtrap (1.1.1)
4
+ mailtrap (1.2.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  addressable (2.8.0)
10
10
  public_suffix (>= 2.0.2, < 5.0)
11
+ appraisal (2.4.1)
12
+ bundler
13
+ rake
14
+ thor (>= 0.14.0)
11
15
  ast (2.4.2)
12
16
  crack (0.4.5)
13
17
  rexml
@@ -55,6 +59,7 @@ GEM
55
59
  rubocop-rspec (2.11.1)
56
60
  rubocop (~> 1.19)
57
61
  ruby-progressbar (1.11.0)
62
+ thor (1.2.1)
58
63
  unicode-display_width (2.1.0)
59
64
  vcr (6.1.0)
60
65
  webmock (3.14.0)
@@ -66,6 +71,7 @@ PLATFORMS
66
71
  ruby
67
72
 
68
73
  DEPENDENCIES
74
+ appraisal
69
75
  mail
70
76
  mailtrap!
71
77
  net-smtp
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2022 Railsware Products Studio, Inc.
3
+ Copyright (c) 2022 Railsware Products Studio LLC
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -30,7 +30,7 @@ Or install it yourself as:
30
30
  require 'mailtrap'
31
31
 
32
32
  # create mail object
33
- mail = Mailtrap::Sending::Mail.new(
33
+ mail = Mailtrap::Mail::Base.new(
34
34
  from: { email: 'mailtrap@example.com', name: 'Mailtrap Test' },
35
35
  to: [
36
36
  { email: 'your@email.com' }
@@ -50,7 +50,7 @@ client.send(mail)
50
50
  require 'mailtrap'
51
51
  require 'base64'
52
52
 
53
- mail = Mailtrap::Sending::Mail.new(
53
+ mail = Mailtrap::Mail::Base.new(
54
54
  from: { email: 'mailtrap@example.com', name: 'Mailtrap Test' },
55
55
  to: [
56
56
  { email: 'your@email.com', name: 'Your name' }
@@ -87,6 +87,28 @@ client = Mailtrap::Sending::Client.new(api_key: 'your-api-key')
87
87
  client.send(mail)
88
88
  ```
89
89
 
90
+ ### Using email template
91
+
92
+ ```ruby
93
+ require 'mailtrap'
94
+
95
+ # create mail object
96
+ mail = Mailtrap::Mail::FromTemplate.new(
97
+ from: { email: 'mailtrap@example.com', name: 'Mailtrap Test' },
98
+ to: [
99
+ { email: 'your@email.com' }
100
+ ],
101
+ template_uuid: '2f45b0aa-bbed-432f-95e4-e145e1965ba2',
102
+ template_variables: {
103
+ 'user_name' => 'John Doe'
104
+ }
105
+ )
106
+
107
+ # create client and send
108
+ client = Mailtrap::Sending::Client.new(api_key: 'your-api-key')
109
+ client.send(mail)
110
+ ```
111
+
90
112
  ### ActionMailer
91
113
 
92
114
  This gem also adds ActionMailer delivery method. To configure it, add following to your ActionMailer configuration (in Rails projects located in `config/$ENVIRONMENT.rb`):
@@ -10,7 +10,7 @@ module Mailtrap
10
10
  end
11
11
 
12
12
  def deliver!(message)
13
- mail = Mailtrap::Sending::Convert.from_message(message)
13
+ mail = Mailtrap::Mail.from_message(message)
14
14
 
15
15
  client.send(mail)
16
16
  end
@@ -3,9 +3,9 @@
3
3
  require 'json'
4
4
 
5
5
  module Mailtrap
6
- module Sending
7
- class Mail
8
- attr_accessor :from, :to, :cc, :bcc, :subject, :text, :html, :headers, :category, :custom_variables
6
+ module Mail
7
+ class Base
8
+ attr_accessor :from, :to, :cc, :bcc, :headers, :custom_variables, :subject, :text, :html, :category
9
9
  attr_reader :attachments
10
10
 
11
11
  def initialize( # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength
@@ -18,8 +18,8 @@ module Mailtrap
18
18
  html: nil,
19
19
  attachments: [],
20
20
  headers: {},
21
- category: nil,
22
- custom_variables: {}
21
+ custom_variables: {},
22
+ category: nil
23
23
  )
24
24
  @from = from
25
25
  @to = to
@@ -30,24 +30,24 @@ module Mailtrap
30
30
  @html = html
31
31
  self.attachments = attachments
32
32
  @headers = headers
33
- @category = category
34
33
  @custom_variables = custom_variables
34
+ @category = category
35
35
  end
36
36
 
37
37
  def as_json # rubocop:disable Metrics/MethodLength
38
38
  {
39
- 'to' => to,
40
39
  'from' => from,
40
+ 'to' => to,
41
41
  'cc' => cc,
42
42
  'bcc' => bcc,
43
43
  'subject' => subject,
44
- 'html' => html,
45
44
  'text' => text,
45
+ 'html' => html,
46
46
  'attachments' => attachments.map(&:as_json),
47
47
  # TODO: update headers and custom_variables with as_json method
48
48
  'headers' => headers,
49
- 'category' => category,
50
- 'custom_variables' => custom_variables
49
+ 'custom_variables' => custom_variables,
50
+ 'category' => category
51
51
  }.compact
52
52
  end
53
53
 
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailtrap
4
+ module Mail
5
+ class FromTemplate < Base
6
+ attr_accessor :template_uuid, :template_variables
7
+
8
+ def initialize( # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength
9
+ from: nil,
10
+ to: [],
11
+ cc: [],
12
+ bcc: [],
13
+ attachments: [],
14
+ headers: {},
15
+ custom_variables: {},
16
+ template_uuid: nil,
17
+ template_variables: {}
18
+ )
19
+ super(
20
+ from: from,
21
+ to: to,
22
+ cc: cc,
23
+ bcc: bcc,
24
+ attachments: attachments,
25
+ headers: headers,
26
+ custom_variables: custom_variables
27
+ )
28
+ @template_uuid = template_uuid
29
+ @template_variables = template_variables
30
+ end
31
+
32
+ def as_json
33
+ super.merge(
34
+ {
35
+ 'template_uuid' => template_uuid,
36
+ 'template_variables' => template_variables
37
+ }
38
+ ).compact
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'base64'
4
+
5
+ require_relative 'mail/base'
6
+ require_relative 'mail/from_template'
7
+
8
+ module Mailtrap
9
+ module Mail
10
+ class << self
11
+ def from_message(message) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity
12
+ Mailtrap::Mail::Base.new(
13
+ from: prepare_address(address_list(message['from'])&.addresses&.first),
14
+ to: prepare_addresses(address_list(message['to'])&.addresses),
15
+ cc: prepare_addresses(address_list(message['cc'])&.addresses),
16
+ bcc: prepare_addresses(address_list(message['bcc'])&.addresses),
17
+ subject: message.subject,
18
+ text: prepare_text_part(message),
19
+ html: prepare_html_part(message),
20
+ headers: prepare_headers(message),
21
+ attachments: prepare_attachments(message.attachments),
22
+ category: message['category']&.unparsed_value,
23
+ custom_variables: message['custom_variables']&.unparsed_value
24
+ )
25
+ end
26
+
27
+ private
28
+
29
+ PROCESSED_HEADERS = %w[
30
+ from
31
+ to
32
+ cc
33
+ bcc
34
+ subject
35
+ category
36
+ customvariables
37
+ contenttype
38
+ ].freeze
39
+
40
+ def address_list(header)
41
+ header.respond_to?(:element) ? header.element : header&.address_list
42
+ end
43
+
44
+ def prepare_addresses(addresses)
45
+ Array(addresses).map { |address| prepare_address(address) }
46
+ end
47
+
48
+ def prepare_headers(message)
49
+ message
50
+ .header_fields
51
+ .reject { |header| PROCESSED_HEADERS.include?(header.name.downcase.delete('-')) }
52
+ .to_h { |header| [header.name, header.value] }
53
+ .compact
54
+ end
55
+
56
+ def prepare_address(address)
57
+ {
58
+ email: address.address,
59
+ name: address.display_name
60
+ }.compact
61
+ end
62
+
63
+ def prepare_attachments(attachments_list = [])
64
+ attachments_list.map do |attachment|
65
+ {
66
+ content: Base64.strict_encode64(attachment.body.decoded),
67
+ type: attachment.mime_type,
68
+ filename: attachment.filename,
69
+ disposition: attachment.header[:content_disposition]&.disposition_type,
70
+ content_id: attachment&.cid
71
+ }.compact
72
+ end
73
+ end
74
+
75
+ def prepare_html_part(message)
76
+ return message.body.decoded if message.mime_type == 'text/html'
77
+
78
+ message.html_part&.decoded
79
+ end
80
+
81
+ def prepare_text_part(message)
82
+ return message.body.decoded if message.mime_type == 'text/plain' || message.mime_type.nil?
83
+
84
+ message.text_part&.decoded
85
+ end
86
+ end
87
+ end
88
+ end
@@ -19,7 +19,7 @@ module Mailtrap
19
19
  end
20
20
 
21
21
  def send(mail)
22
- raise ArgumentError, 'should be Mailtrap::Sending::Mail object' unless mail.is_a? Mailtrap::Sending::Mail
22
+ raise ArgumentError, 'should be Mailtrap::Mail::Base object' unless mail.is_a? Mail::Base
23
23
 
24
24
  request = post_request('/api/send', mail.to_json)
25
25
  response = http_client.request(request)
@@ -38,6 +38,7 @@ module Mailtrap
38
38
  request.body = body
39
39
  request['Authorization'] = "Bearer #{api_key}"
40
40
  request['Content-Type'] = 'application/json'
41
+ request['User-Agent'] = 'mailtrap-ruby (https://github.com/railsware/mailtrap-ruby)'
41
42
 
42
43
  request
43
44
  end
@@ -2,8 +2,6 @@
2
2
 
3
3
  require_relative 'sending/attachment'
4
4
  require_relative 'sending/client'
5
- require_relative 'sending/convert'
6
- require_relative 'sending/mail'
7
5
 
8
6
  module Mailtrap
9
7
  module Sending
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mailtrap
4
- VERSION = '1.1.1'
4
+ VERSION = '1.2.1'
5
5
  end
data/lib/mailtrap.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'mailtrap/action_mailer' if defined? ActionMailer
4
+ require_relative 'mailtrap/mail'
4
5
  require_relative 'mailtrap/sending'
5
6
  require_relative 'mailtrap/version'
6
7
 
data/mailtrap.gemspec CHANGED
@@ -5,7 +5,7 @@ require_relative 'lib/mailtrap/version'
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'mailtrap'
7
7
  spec.version = Mailtrap::VERSION
8
- spec.authors = ['Railsware Products Studio, Inc.']
8
+ spec.authors = ['Railsware Products Studio LLC']
9
9
  spec.email = ['support@mailtrap.io']
10
10
 
11
11
  spec.summary = 'Official mailtrap.io API client'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailtrap
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
- - Railsware Products Studio, Inc.
7
+ - Railsware Products Studio LLC
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-14 00:00:00.000000000 Z
11
+ date: 2023-04-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Official mailtrap.io API client
14
14
  email:
@@ -20,6 +20,7 @@ files:
20
20
  - ".rspec"
21
21
  - ".rubocop.yml"
22
22
  - ".ruby-version"
23
+ - Appraisals
23
24
  - CHANGELOG.md
24
25
  - CODE_OF_CONDUCT.md
25
26
  - Gemfile
@@ -31,11 +32,12 @@ files:
31
32
  - lib/mailtrap/action_mailer.rb
32
33
  - lib/mailtrap/action_mailer/delivery_method.rb
33
34
  - lib/mailtrap/action_mailer/railtie.rb
35
+ - lib/mailtrap/mail.rb
36
+ - lib/mailtrap/mail/base.rb
37
+ - lib/mailtrap/mail/from_template.rb
34
38
  - lib/mailtrap/sending.rb
35
39
  - lib/mailtrap/sending/attachment.rb
36
40
  - lib/mailtrap/sending/client.rb
37
- - lib/mailtrap/sending/convert.rb
38
- - lib/mailtrap/sending/mail.rb
39
41
  - lib/mailtrap/version.rb
40
42
  - mailtrap.gemspec
41
43
  homepage: https://github.com/railsware/mailtrap-ruby
@@ -1,83 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'base64'
4
-
5
- module Mailtrap
6
- module Sending
7
- module Convert
8
- class << self
9
- def from_message(message) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
10
- Mailtrap::Sending::Mail.new(
11
- from: prepare_address(message['from']&.address_list&.addresses&.first),
12
- to: prepare_addresses(message['to']&.address_list&.addresses),
13
- cc: prepare_addresses(message['cc']&.address_list&.addresses),
14
- bcc: prepare_addresses(message['bcc']&.address_list&.addresses),
15
- subject: message.subject,
16
- text: prepare_text_part(message),
17
- html: prepare_html_part(message),
18
- headers: prepare_headers(message),
19
- attachments: prepare_attachments(message.attachments),
20
- category: message['category']&.unparsed_value,
21
- custom_variables: message['custom_variables']&.unparsed_value
22
- )
23
- end
24
-
25
- private
26
-
27
- PROCESSED_HEADERS = %w[
28
- from
29
- to
30
- cc
31
- bcc
32
- subject
33
- category
34
- customvariables
35
- contenttype
36
- ].freeze
37
-
38
- def prepare_addresses(addresses)
39
- Array(addresses).map { |address| prepare_address(address) }
40
- end
41
-
42
- def prepare_headers(message)
43
- message
44
- .header_fields
45
- .reject { |header| PROCESSED_HEADERS.include?(header.name.downcase.delete('-')) }
46
- .to_h { |header| [header.name, header.value] }
47
- .compact
48
- end
49
-
50
- def prepare_address(address)
51
- {
52
- email: address.address,
53
- name: address.display_name
54
- }.compact
55
- end
56
-
57
- def prepare_attachments(attachments_list = [])
58
- attachments_list.map do |attachment|
59
- {
60
- content: Base64.strict_encode64(attachment.body.decoded),
61
- type: attachment.mime_type,
62
- filename: attachment.filename,
63
- disposition: attachment.header[:content_disposition]&.disposition_type,
64
- content_id: attachment.header[:content_id]&.field&.content_id
65
- }.compact
66
- end
67
- end
68
-
69
- def prepare_html_part(message)
70
- return message.body.decoded if message.mime_type == 'text/html'
71
-
72
- message.html_part&.decoded
73
- end
74
-
75
- def prepare_text_part(message)
76
- return message.body.decoded if message.mime_type == 'text/plain' || message.mime_type.nil?
77
-
78
- message.text_part&.decoded
79
- end
80
- end
81
- end
82
- end
83
- end