sendgrid_actionmailer_adapter 0.1.1 → 0.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
  SHA1:
3
- metadata.gz: 4d38361de9e37da89fda355531df6a6eeb9b4918
4
- data.tar.gz: 6484a052301a6f940e0ca129b2a994e2c30fb2a4
3
+ metadata.gz: ee79cf7cc71fee2d3bd2212d2bfb1221d9629d90
4
+ data.tar.gz: f35a57e974388b72188ae92d9dd168763a662a6c
5
5
  SHA512:
6
- metadata.gz: f5fbe19d6d53ffb020a1d5fb9cfe464368aebf17ba062de71752022b0d0dc601d3f96a0a969b4ff57b726417da9368e68fa11473d2ab1f1f527aac28aa170b84
7
- data.tar.gz: a940aeb57e9755646c88463f2647efefdfcb5cf7e98f3d052730233be54a0aa966439d72c6bc641199d4963da16e10e8cdbba571fc36798989de21c5b083b41a
6
+ metadata.gz: c37de30e3e2b91fb8cac5860bda517d44eea91eb04d60403ed6af2b3652a6fcb3b003aaafb6bf4550287c6bb115d5d8ad2e22b18d83e68f608a32c2706292585
7
+ data.tar.gz: d82abaf00a36eec76085fe8df9db0311634dd89ec49df7a799be08b55a13bd04bc1018ee6258775b5609854ffeaaab89e6ceb4930134371bfe03ddc180016b20
@@ -1,67 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'base64'
4
- require 'mail'
5
3
  require 'sendgrid-ruby'
6
4
  require_relative 'errors'
5
+ require_relative 'converters/subject'
6
+ require_relative 'converters/from'
7
+ require_relative 'converters/personalizations'
8
+ require_relative 'converters/attachments'
9
+ require_relative 'converters/contents'
10
+ require_relative 'converters/categories'
11
+ require_relative 'converters/send_at'
12
+ require_relative 'converters/reply_to'
7
13
 
8
14
  module SendGridActionMailerAdapter
9
15
  class Converter
10
- VALIDATORS = [
11
- ->(mail) { "'from' is required." if mail.from_addrs.empty? },
12
- ->(mail) { "'to_addrs' must not be empty." if mail.to_addrs.empty? },
13
- ->(mail) { "'subject' is required." if mail.subject.nil? || mail.subject.empty? },
16
+ CONVERTERS = [
17
+ ::SendGridActionMailerAdapter::Converters::From.new,
18
+ ::SendGridActionMailerAdapter::Converters::Subject.new,
19
+ ::SendGridActionMailerAdapter::Converters::Personalizations.new,
20
+ ::SendGridActionMailerAdapter::Converters::Attachments.new,
21
+ ::SendGridActionMailerAdapter::Converters::Contents.new,
22
+ ::SendGridActionMailerAdapter::Converters::Categories.new,
23
+ ::SendGridActionMailerAdapter::Converters::SendAt.new,
24
+ ::SendGridActionMailerAdapter::Converters::ReplyTo.new,
14
25
  ].freeze
15
26
 
16
- CONVERTERS = {
17
- from: ->(mail) {
18
- addr = mail[:from].addrs.first
19
- ::SendGrid::Email.new(email: addr.address, name: addr.display_name)
20
- },
21
- subject: ->(mail) { mail.subject },
22
- personalizations: ->(mail) {
23
- # Separate emails per each To address.
24
- # Cc and Bcc addresses are shared with each emails.
25
- mail[:to].addrs.map do |to_addr|
26
- ::SendGrid::Personalization.new.tap do |p|
27
- p.to = ::SendGrid::Email.new(email: to_addr.address, name: to_addr.display_name)
28
- Array(mail[:cc]&.addrs).each do |addr|
29
- p.cc = ::SendGrid::Email.new(email: addr.address, name: addr.display_name)
30
- end
31
- Array(mail[:bcc]&.addrs).each do |addr|
32
- p.bcc = ::SendGrid::Email.new(email: addr.address, name: addr.display_name)
33
- end
34
- end
35
- end
36
- },
37
- contents: ->(mail) {
38
- ::SendGrid::Content.new(type: mail.mime_type, value: mail.body.to_s)
39
- },
40
- attachments: ->(mail) {
41
- mail.attachments.map do |attachment|
42
- ::SendGrid::Attachment.new.tap do |sendgrid_attachment|
43
- sendgrid_attachment.type = attachment.mime_type
44
- sendgrid_attachment.content = ::Base64.strict_encode64(attachment.body.raw_source)
45
- sendgrid_attachment.filename = ::Mail::Encodings.decode_encode(
46
- attachment.content_type_parameters['filename'], :decode
47
- )
48
- sendgrid_attachment.content_id = attachment.cid
49
- end
50
- end
51
- },
52
- categories: ->(mail) {
53
- return nil if mail['categories'].nil?
54
- # FIXME: Separator ', ' is dependant on Mail::UnstructuredField implementation,
55
- # this may occur unexpected behaviour on 'mail' gem update.
56
- mail['categories'].value.split(', ').map { |c| ::SendGrid::Category.new(name: c) }
57
- },
58
- send_at: ->(mail) { mail['send_at'].value.to_i if mail['send_at'] },
59
- reply_to: ->(mail) {
60
- addr = mail[:reply_to]&.addrs&.first
61
- ::SendGrid::Email.new(email: addr.address, name: addr.display_name) if addr
62
- },
63
- }.freeze
64
-
65
27
  class << self
66
28
  # Convert Mail::Message to SendGrid::Mail.
67
29
  #
@@ -76,18 +38,18 @@ module SendGridActionMailerAdapter
76
38
  private
77
39
 
78
40
  def validate!(mail)
79
- error_messages = VALIDATORS.map { |validator| validator.call(mail) }.compact
41
+ error_messages = CONVERTERS.flat_map { |converter| converter.validate(mail) }.compact
80
42
  unless error_messages.empty?
81
- raise SendGridActionMailerAdapter::ValidationError, "Validation error, #{error_messages}"
43
+ raise ::SendGridActionMailerAdapter::ValidationError,
44
+ "Validation error, #{error_messages}"
82
45
  end
83
46
  end
84
47
 
85
48
  def convert(mail)
86
49
  sendgrid_mail = ::SendGrid::Mail.new
87
- CONVERTERS.each do |attr_name, converter|
88
- Array(converter.call(mail)).each do |attr_val|
89
- sendgrid_mail.send("#{attr_name}=", attr_val)
90
- end
50
+ CONVERTERS.each do |converter|
51
+ result = converter.convert(mail)
52
+ converter.assign_attributes(sendgrid_mail, result)
91
53
  end
92
54
  sendgrid_mail
93
55
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mail'
4
+ require 'base64'
5
+ require 'sendgrid-ruby'
6
+
7
+ module SendGridActionMailerAdapter
8
+ module Converters
9
+ class Attachments
10
+ def validate(_mail)
11
+ end
12
+
13
+ def convert(mail)
14
+ mail.attachments.map do |attachment|
15
+ ::SendGrid::Attachment.new.tap do |sendgrid_attachment|
16
+ sendgrid_attachment.type = attachment.mime_type
17
+ sendgrid_attachment.content = ::Base64.strict_encode64(attachment.body.raw_source)
18
+ sendgrid_attachment.filename = ::Mail::Encodings.decode_encode(
19
+ attachment.content_type_parameters['filename'], :decode
20
+ )
21
+ sendgrid_attachment.content_id = attachment.cid
22
+ end
23
+ end
24
+ end
25
+
26
+ def assign_attributes(sendgrid_mail, value)
27
+ Array(value).each do |attachment|
28
+ sendgrid_mail.add_attachment(attachment)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sendgrid-ruby'
4
+
5
+ module SendGridActionMailerAdapter
6
+ module Converters
7
+ class Categories
8
+ def validate(_mail)
9
+ end
10
+
11
+ def convert(mail)
12
+ categories_str = mail['categories']&.value
13
+ return if categories_str.nil? || categories_str.empty?
14
+ categories_str.split(', ').map { |c| ::SendGrid::Category.new(name: c) }
15
+ end
16
+
17
+ def assign_attributes(sendgrid_mail, value)
18
+ Array(value).each do |category|
19
+ sendgrid_mail.add_category(category)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sendgrid-ruby'
4
+
5
+ module SendGridActionMailerAdapter
6
+ module Converters
7
+ class Contents
8
+ def validate(_mail)
9
+ end
10
+
11
+ def convert(mail)
12
+ ::SendGrid::Content.new(type: mail.mime_type, value: mail.body.to_s)
13
+ end
14
+
15
+ def assign_attributes(sendgrid_mail, value)
16
+ Array(value).each do |content|
17
+ sendgrid_mail.add_content(content)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sendgrid-ruby'
4
+
5
+ module SendGridActionMailerAdapter
6
+ module Converters
7
+ class From
8
+ def validate(mail)
9
+ error_messages = []
10
+ if mail.from_addrs.empty?
11
+ error_messages << "'from' is required."
12
+ end
13
+ error_messages
14
+ end
15
+
16
+ def convert(mail)
17
+ addr = mail[:from].addrs.first
18
+ ::SendGrid::Email.new(email: addr.address, name: addr.display_name)
19
+ end
20
+
21
+ def assign_attributes(sendgrid_mail, value)
22
+ sendgrid_mail.from = value
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sendgrid-ruby'
4
+
5
+ module SendGridActionMailerAdapter
6
+ module Converters
7
+ class Personalizations
8
+ def validate(mail)
9
+ error_messages = []
10
+ if mail.to_addrs.empty?
11
+ error_messages << "'to_addrs' must not be empty."
12
+ end
13
+ error_messages
14
+ end
15
+
16
+ def convert(mail)
17
+ # Separate emails per each To address.
18
+ # Cc and Bcc addresses are shared with each emails.
19
+ cc_addrs = mail[:cc]&.addrs
20
+ bcc_addrs = mail[:bcc]&.addrs
21
+ mail[:to].addrs.map do |to_addr|
22
+ to_personalization(to_addr, cc_addrs, bcc_addrs)
23
+ end
24
+ end
25
+
26
+ def assign_attributes(sendgrid_mail, value)
27
+ Array(value).each do |personalization|
28
+ sendgrid_mail.add_personalization(personalization)
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def to_personalization(to_addr, cc_addrs, bcc_addrs)
35
+ ::SendGrid::Personalization.new.tap do |p|
36
+ p.add_to(::SendGrid::Email.new(email: to_addr.address, name: to_addr.display_name))
37
+ Array(cc_addrs).each do |addr|
38
+ p.add_cc(::SendGrid::Email.new(email: addr.address, name: addr.display_name))
39
+ end
40
+ Array(bcc_addrs).each do |addr|
41
+ p.add_bcc(::SendGrid::Email.new(email: addr.address, name: addr.display_name))
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sendgrid-ruby'
4
+
5
+ module SendGridActionMailerAdapter
6
+ module Converters
7
+ class ReplyTo
8
+ def validate(_mail)
9
+ end
10
+
11
+ def convert(mail)
12
+ addr = mail[:reply_to]&.addrs&.first
13
+ return if addr.nil?
14
+ ::SendGrid::Email.new(email: addr.address, name: addr.display_name)
15
+ end
16
+
17
+ def assign_attributes(sendgrid_mail, value)
18
+ sendgrid_mail.reply_to = value
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sendgrid-ruby'
4
+
5
+ module SendGridActionMailerAdapter
6
+ module Converters
7
+ class SendAt
8
+ def validate(_mail)
9
+ end
10
+
11
+ def convert(mail)
12
+ send_at_str = mail['send_at']&.value
13
+ return if send_at_str.nil? || send_at_str.empty?
14
+ send_at_str.to_i
15
+ end
16
+
17
+ def assign_attributes(sendgrid_mail, value)
18
+ sendgrid_mail.send_at = value
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SendGridActionMailerAdapter
4
+ module Converters
5
+ class Subject
6
+ def validate(mail)
7
+ error_messages = []
8
+ if mail.subject.nil? || mail.subject.empty?
9
+ error_messages << "'subject' is required."
10
+ end
11
+ error_messages
12
+ end
13
+
14
+ def convert(mail)
15
+ mail.subject
16
+ end
17
+
18
+ def assign_attributes(sendgrid_mail, value)
19
+ sendgrid_mail.subject = value
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SendGridActionMailerAdapter
4
- VERSION = '0.1.1'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sendgrid_actionmailer_adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ryu39
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-01 00:00:00.000000000 Z
11
+ date: 2017-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sendgrid-ruby
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '4.0'
19
+ version: '5.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '4.0'
26
+ version: '5.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -53,47 +53,47 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rspec
56
+ name: ryu39cop
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '3.0'
61
+ version: 0.49.1.0
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '3.0'
68
+ version: 0.49.1.0
69
69
  - !ruby/object:Gem::Dependency
70
- name: webmock
70
+ name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: '3.0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: '3.0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: rubocop
84
+ name: webmock
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
- version: 0.48.1
89
+ version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - "~>"
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
- version: 0.48.1
96
+ version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: mail
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -133,6 +133,14 @@ files:
133
133
  - lib/sendgrid_actionmailer_adapter.rb
134
134
  - lib/sendgrid_actionmailer_adapter/configuration.rb
135
135
  - lib/sendgrid_actionmailer_adapter/converter.rb
136
+ - lib/sendgrid_actionmailer_adapter/converters/attachments.rb
137
+ - lib/sendgrid_actionmailer_adapter/converters/categories.rb
138
+ - lib/sendgrid_actionmailer_adapter/converters/contents.rb
139
+ - lib/sendgrid_actionmailer_adapter/converters/from.rb
140
+ - lib/sendgrid_actionmailer_adapter/converters/personalizations.rb
141
+ - lib/sendgrid_actionmailer_adapter/converters/reply_to.rb
142
+ - lib/sendgrid_actionmailer_adapter/converters/send_at.rb
143
+ - lib/sendgrid_actionmailer_adapter/converters/subject.rb
136
144
  - lib/sendgrid_actionmailer_adapter/delivery_method.rb
137
145
  - lib/sendgrid_actionmailer_adapter/errors.rb
138
146
  - lib/sendgrid_actionmailer_adapter/version.rb