mailtrap 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dd604b5702f48dbaa0cf3fb294c3d000af11891ca3e0d6453af0763c1aa0abd4
4
- data.tar.gz: 66baa71b1a5cb26cbd4012f6b6a4ab10fd585850f93792ffc9b2776bde9019ae
3
+ metadata.gz: e4f1415f049fa2132b1716a70d48c1e6fd5650d4fa263b15fe2339c024053abd
4
+ data.tar.gz: e6a299b5d04aeeb2ed80c0b47bf3f1177d5934ef214d0499a9c8d4c8f17a5f7c
5
5
  SHA512:
6
- metadata.gz: 853e6421d2d5d3c02bd0b090b4810383b5b30e6b8bfc178379ea602e56eb44ba1b298a2bb8e87575abfd0a37bc110a5bb8c8d14ca78d3c75729974cf389b2fd6
7
- data.tar.gz: b9ca3ed482f8077a35151237a6078ef88ce2cdc58f1f097f72730f6fe71eb7367235559ebbbeba24b1622a06af10f31c728deb93ee6ef4207d6091543e5944bb
6
+ metadata.gz: b594f448ac1fee0581ebac86107572e69c7f77ec8acc558671977e1301b762bedb3e962514485cfba87089c5587c63475dd57e5744c2c6225da58a36525f3bad
7
+ data.tar.gz: 94692a8b8a05646ddf49f92a13cfb630ed5e79176bed7f7b48f2f914b092becfd422ba21c38c5dddb6e802261d235c6e8ac2f2cdb190a0e3bc4c1d6339156b72
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
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
@@ -10,3 +10,15 @@
10
10
  ## [1.1.0] - 2022-07-22
11
11
 
12
12
  - Add ActionMailer support
13
+
14
+ ## [1.1.1] - 2022-10-14
15
+
16
+ - Fix custom port and host usage
17
+
18
+ ## [1.2.0] - 2023-01-27
19
+
20
+ - Breaking changes:
21
+ - move `Mailtrap::Sending::Mail` class to `Mailtrap::Mail::Base`
22
+ - move `Mailtrap::Sending::Convert` to `Mailtrap::Mail`
23
+ - Add mail gem 2.8 support
24
+ - Add email template support
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.0)
4
+ mailtrap (1.2.0)
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)
@@ -30,7 +30,7 @@ module Mailtrap
30
30
  private
31
31
 
32
32
  def http_client
33
- @http_client ||= Net::HTTP.new(API_HOST, API_PORT).tap { |client| client.use_ssl = true }
33
+ @http_client ||= Net::HTTP.new(api_host, api_port).tap { |client| client.use_ssl = true }
34
34
  end
35
35
 
36
36
  def post_request(path, body)
@@ -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.0'
4
+ VERSION = '1.2.0'
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
 
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.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
- - Railsware Products Studio, Inc.
8
- autorequire:
7
+ - Railsware Products Studio LLC
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-01 00:00:00.000000000 Z
11
+ date: 2023-01-31 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,13 +32,13 @@ 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
- - mailtrap.gemspec
41
42
  homepage: https://github.com/railsware/mailtrap-ruby
42
43
  licenses:
43
44
  - MIT
@@ -46,7 +47,7 @@ metadata:
46
47
  source_code_uri: https://github.com/railsware/mailtrap-ruby
47
48
  changelog_uri: https://github.com/railsware/mailtrap-ruby/blob/main/CHANGELOG.md
48
49
  rubygems_mfa_required: 'true'
49
- post_install_message:
50
+ post_install_message:
50
51
  rdoc_options: []
51
52
  require_paths:
52
53
  - lib
@@ -62,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
63
  version: '0'
63
64
  requirements: []
64
65
  rubygems_version: 3.1.6
65
- signing_key:
66
+ signing_key:
66
67
  specification_version: 4
67
68
  summary: Official mailtrap.io API client
68
69
  test_files: []
@@ -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
data/mailtrap.gemspec DELETED
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'lib/mailtrap/version'
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = 'mailtrap'
7
- spec.version = Mailtrap::VERSION
8
- spec.authors = ['Railsware Products Studio, Inc.']
9
- spec.email = ['support@mailtrap.io']
10
-
11
- spec.summary = 'Official mailtrap.io API client'
12
- spec.description = 'Official mailtrap.io API client'
13
- spec.homepage = 'https://github.com/railsware/mailtrap-ruby'
14
- spec.license = 'MIT'
15
- spec.required_ruby_version = '>= 2.7.0'
16
-
17
- spec.metadata['homepage_uri'] = spec.homepage
18
- spec.metadata['source_code_uri'] = 'https://github.com/railsware/mailtrap-ruby'
19
- spec.metadata['changelog_uri'] = 'https://github.com/railsware/mailtrap-ruby/blob/main/CHANGELOG.md'
20
- spec.metadata['rubygems_mfa_required'] = 'true'
21
-
22
- # Specify which files should be added to the gem when it is released.
23
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
- spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
- `git ls-files -z`.split("\x0").reject do |f|
26
- (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|github|travis|circleci)|appveyor)})
27
- end
28
- end
29
- spec.require_paths = ['lib']
30
- end