sg_mailer 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +162 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/sg_mailer.rb +35 -0
- data/lib/sg_mailer/base.rb +84 -0
- data/lib/sg_mailer/client.rb +42 -0
- data/lib/sg_mailer/configuration_error.rb +3 -0
- data/lib/sg_mailer/delayed_delivery_processor.rb +31 -0
- data/lib/sg_mailer/immediate_delivery_processor.rb +25 -0
- data/lib/sg_mailer/mail_builder.rb +41 -0
- data/lib/sg_mailer/response_error.rb +19 -0
- data/lib/sg_mailer/test_client.rb +13 -0
- data/lib/sg_mailer/version.rb +3 -0
- data/sg_mailer.gemspec +27 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d2db0fcf74a3885da0b577a1bff7812ad788d4d3
|
4
|
+
data.tar.gz: ad525cef3c2fd30edbfc1c94f526258beb8b671b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b819209e28731ad1a3105cd3d64e117fb7404cb936365fd96a74f932e0b44250df9f43cbf5182e503ad8fcc4e0941b01515f32f177caf00ef95a077f30d7dcdb
|
7
|
+
data.tar.gz: 04a1a72c3b1b053009185e736795668eb461abbccde6404b0901569d8260ea967dcdec2fc60eb881c5957315c59f7de341363fc6502e34a1e0cdf26ca14f0d01
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at gsamokovarov@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Genadi Samokovarov
|
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,162 @@
|
|
1
|
+
# SGMailer
|
2
|
+
|
3
|
+
SG Mailer is a mailing framework with similar to Action Mailer interface for SendGrid
|
4
|
+
transactional emails.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'sg_mailer'
|
12
|
+
```
|
13
|
+
|
14
|
+
## Usage
|
15
|
+
|
16
|
+
To start using the framework, you need to configure it first. It requires a
|
17
|
+
SendGrid API KEY, which you can setup like this:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
SGMailer.configure(api_key: ENV['SENDGRID_API_KEY')
|
21
|
+
```
|
22
|
+
|
23
|
+
Once setup, you can use SG Mailer alongside or as a replacement to Action
|
24
|
+
Mailer. Creating a base mailer (a class like `ApplicationMailer`) is a good
|
25
|
+
starting point.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
# app/mailers/sendgrid_mailer.rb
|
29
|
+
|
30
|
+
class SendGridMailer < SGMailer::Base
|
31
|
+
# you may wanna include helper modules here, as they are not automgically
|
32
|
+
# included like in action mailer.
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
Having the base mailer around, you may start introducing specific ones.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
class SubscriptionMailer < SendGridMailer
|
40
|
+
def successful_subscription_mail(parent, kid)
|
41
|
+
template_id = 'd6c0eb74-b280-4d25-a835-479818a450e8'
|
42
|
+
|
43
|
+
substitutions = {
|
44
|
+
'KIDNAME' => kid.name
|
45
|
+
'PARENTTYPE' => parent.gender
|
46
|
+
}
|
47
|
+
|
48
|
+
mail from: 'charlie@bp.com', to: parent.email,
|
49
|
+
template_id: template_id, substitutions : substitutions
|
50
|
+
end
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
Working with transaction emails, you are quite likely to use templates for
|
55
|
+
them. Since this is so common, there is nice interface for annotating the
|
56
|
+
emails with the template id.
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
class SubscriptionMailer < SendGridMailer
|
60
|
+
template_id 'd6c0eb74-b280-4d25-a835-479818a450e8'
|
61
|
+
def successful_subscription_mail(parent, kid)
|
62
|
+
substitutions = {
|
63
|
+
'KIDNAME' => kid.name
|
64
|
+
'PARENTTYPE' => parent.gender
|
65
|
+
}
|
66
|
+
|
67
|
+
mail from: 'charlie@bp.com', to: parent.email,
|
68
|
+
substitutions : substitutions
|
69
|
+
end
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
You can also extract the common sender into a default value like so:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
class SubscriptionMailer < SendGridMailer
|
77
|
+
default from: { name: 'Charlie Brown', email: 'charlie@bp.com' }
|
78
|
+
|
79
|
+
template_id 'd6c0eb74-b280-4d25-a835-479818a450e8'
|
80
|
+
def successful_subscription_mail(parent, kid)
|
81
|
+
substitutions = {
|
82
|
+
'KIDNAME' => kid.name
|
83
|
+
'PARENTTYPE' => parent.gender
|
84
|
+
}
|
85
|
+
|
86
|
+
mail to: parent.email, substitutions : substitutions
|
87
|
+
end
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
In fact the base mailer (`SendGridMailer`) is a good place to define such
|
92
|
+
defaults.
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
# app/mailers/sendgrid_mailer.rb
|
96
|
+
|
97
|
+
class SendGridMailer < SGMailer::Base
|
98
|
+
default from: { name: 'Charlie Brown', email: 'charlie@bp.com' }
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
102
|
+
We were able to clean a lot of cruft from the mailers above, however, can we do
|
103
|
+
something about the substitutions. They do seem to have noisy API. Let's think
|
104
|
+
about Ruby instance variables. The Rails controller to view integration uses
|
105
|
+
them as an API and it works pretty well.
|
106
|
+
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
# app/mailers/sendgrid_mailer.rb
|
110
|
+
|
111
|
+
class SendGridMailer < SGMailer::Base
|
112
|
+
default from: { name: 'Charlie Brown', email: 'charlie@bp.com' }
|
113
|
+
|
114
|
+
private
|
115
|
+
|
116
|
+
# This is the hook that processes the options given to `SGMailer::Base#mail`.
|
117
|
+
# Don't forget to call super and return a Hash in here.
|
118
|
+
def normalize_options(options)
|
119
|
+
automatic_substitutions = instance_values.transform_keys do |key|
|
120
|
+
"#{key.to_s.remove('_').upcase}"
|
121
|
+
end
|
122
|
+
|
123
|
+
super.deep_merge(substitutions: automatic_substitutions)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
```
|
127
|
+
|
128
|
+
With the code above the mails can look pretty tidy.
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
class SubscriptionMailer < SendGridMailer
|
132
|
+
template_id 'd6c0eb74-b280-4d25-a835-479818a450e8'
|
133
|
+
def successful_subscription_mail(parent, kid)
|
134
|
+
@kid_name = kid.name
|
135
|
+
@parent_type = parent.gender
|
136
|
+
|
137
|
+
mail to: parent.email
|
138
|
+
end
|
139
|
+
end
|
140
|
+
```
|
141
|
+
|
142
|
+
We don't conventionalize the substitutions, because their format varies from
|
143
|
+
team to team. When you settle on one, you can easily turn your instance
|
144
|
+
variables into proper substitutions, should you want to.
|
145
|
+
|
146
|
+
Until now, we only defined mails, but how to send them?
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
# To deliver the mail now.
|
150
|
+
SubscriptionMailer.successful_subscription_mail.deliver_now
|
151
|
+
|
152
|
+
# To deliver the mail later on, with ActionJob, if installed.
|
153
|
+
SubscriptionMailer.successful_subscription_mail.deliver_later
|
154
|
+
```
|
155
|
+
|
156
|
+
The interface should be pretty familiar to you, if you have used Action Mailer
|
157
|
+
before.
|
158
|
+
|
159
|
+
## License
|
160
|
+
|
161
|
+
The gem is available as open source under the terms of the [MIT
|
162
|
+
License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "sg_mailer"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/sg_mailer.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'sg_mailer/configuration_error'
|
2
|
+
require 'sg_mailer/response_error'
|
3
|
+
require 'sg_mailer/mail_builder'
|
4
|
+
require 'sg_mailer/client'
|
5
|
+
require 'sg_mailer/test_client'
|
6
|
+
require 'sg_mailer/base'
|
7
|
+
|
8
|
+
module SGMailer extend self
|
9
|
+
attr_accessor :client
|
10
|
+
attr_accessor :delivery_processor
|
11
|
+
|
12
|
+
def configure(delivery_processor: default_delivery_processor,
|
13
|
+
test_client: false, **client_options)
|
14
|
+
self.client = test_client ? TestClient.new : Client.new(**client_options)
|
15
|
+
self.delivery_processor = delivery_processor
|
16
|
+
end
|
17
|
+
|
18
|
+
def send(mail)
|
19
|
+
raise ConfigurationError if client.nil?
|
20
|
+
|
21
|
+
client.send(mail)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def default_delivery_processor
|
27
|
+
if defined?(::ActiveJob)
|
28
|
+
require 'sg_mailer/delayed_delivery_processor'
|
29
|
+
DelayedDeliveryProcessor
|
30
|
+
else
|
31
|
+
require 'sg_mailer/immediate_delivery_processor'
|
32
|
+
ImmediateDeliveryProcessor
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module SGMailer
|
2
|
+
class Base
|
3
|
+
class << self
|
4
|
+
def default(values = nil)
|
5
|
+
@default ||= {}
|
6
|
+
@default.merge!(values) if values
|
7
|
+
@default
|
8
|
+
end
|
9
|
+
|
10
|
+
def template_id(id)
|
11
|
+
@current_template_id = id
|
12
|
+
end
|
13
|
+
|
14
|
+
def template_ids
|
15
|
+
@template_ids ||= {}
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def instance
|
21
|
+
@instance ||= new
|
22
|
+
end
|
23
|
+
|
24
|
+
def inherited(klass)
|
25
|
+
klass.instance_variable_set(:@default, default.dup)
|
26
|
+
end
|
27
|
+
|
28
|
+
def method_missing(name, *args, &block)
|
29
|
+
if public_instance_methods.include?(name)
|
30
|
+
SGMailer.delivery_processor.new \
|
31
|
+
instance.public_send(name, *args, &block)
|
32
|
+
else
|
33
|
+
super
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def method_added(method_name)
|
38
|
+
if @current_template_id
|
39
|
+
template_ids[method_name.to_s] = @current_template_id
|
40
|
+
end
|
41
|
+
|
42
|
+
@current_template_id = nil
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def mail(**options)
|
49
|
+
# Guess the template id here, so we don't have to account for extra
|
50
|
+
# calls.
|
51
|
+
guessed_template_id =
|
52
|
+
self.class.template_ids[caller_locations.first.base_label]
|
53
|
+
|
54
|
+
normalized = normalize_options({
|
55
|
+
template_id: guessed_template_id
|
56
|
+
}.merge(options))
|
57
|
+
|
58
|
+
MailBuilder.build(normalized)
|
59
|
+
end
|
60
|
+
|
61
|
+
def normalize_options(options)
|
62
|
+
deep_merge(self.class.default, options)
|
63
|
+
end
|
64
|
+
|
65
|
+
# Based on the Active Support Hash#deep_merge! core extension. Inlined
|
66
|
+
# here, so we don't need to explicitly depend on Active Support.
|
67
|
+
def deep_merge(hash, other_hash)
|
68
|
+
merged = hash.dup
|
69
|
+
|
70
|
+
other_hash.each_pair do |current_key, other_value|
|
71
|
+
this_value = merged[current_key]
|
72
|
+
|
73
|
+
merged[current_key] =
|
74
|
+
if this_value.is_a?(Hash) && other_value.is_a?(Hash)
|
75
|
+
deep_merge(this_value, other_value)
|
76
|
+
else
|
77
|
+
other_value
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
merged
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'net/http'
|
3
|
+
|
4
|
+
module SGMailer
|
5
|
+
class Client
|
6
|
+
attr_reader :api_key
|
7
|
+
|
8
|
+
def initialize(api_key:, api_url: nil)
|
9
|
+
@api_key = api_key
|
10
|
+
@api_url ||= 'https://api.sendgrid.com/v3/mail/send'
|
11
|
+
@api_uri = URI(@api_url)
|
12
|
+
end
|
13
|
+
|
14
|
+
def send(mail)
|
15
|
+
response =
|
16
|
+
Net::HTTP.start(*http_options) do |http|
|
17
|
+
http.request(request(mail.to_json))
|
18
|
+
end
|
19
|
+
|
20
|
+
raise ResponseError, response if response.code.to_i > 299
|
21
|
+
|
22
|
+
response
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def http_options
|
28
|
+
[@api_uri.host, @api_uri.port, use_ssl: @api_uri.scheme == 'https']
|
29
|
+
end
|
30
|
+
|
31
|
+
def request(body)
|
32
|
+
request = Net::HTTP::Post.new(@api_uri, request_headers)
|
33
|
+
request.body = body
|
34
|
+
request
|
35
|
+
end
|
36
|
+
|
37
|
+
def request_headers
|
38
|
+
{ 'Authorization' => "Bearer #{@api_key}",
|
39
|
+
'Content-Type' => 'application/json' }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module SGMailer
|
2
|
+
class DelayedDeliveryProcessor
|
3
|
+
class Job < ActiveJob::Base
|
4
|
+
def perform(mail)
|
5
|
+
SGMailer.send(mail)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(mail)
|
10
|
+
@mail = mail
|
11
|
+
end
|
12
|
+
|
13
|
+
def deliver_now(**options)
|
14
|
+
return if early_return?
|
15
|
+
|
16
|
+
Job.set(options).perform_now(@mail)
|
17
|
+
end
|
18
|
+
|
19
|
+
def deliver_later(**options)
|
20
|
+
return if early_return?
|
21
|
+
|
22
|
+
Job.set(options).perform_later(@mail)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def early_return?
|
28
|
+
@mail.nil? || @mail.empty?
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module SGMailer
|
2
|
+
class ImmediateDeliveryProcessor
|
3
|
+
def initialize(mail)
|
4
|
+
@mail = mail
|
5
|
+
end
|
6
|
+
|
7
|
+
def deliver_now(**)
|
8
|
+
return if early_return?
|
9
|
+
|
10
|
+
SGMailer.send(@mail)
|
11
|
+
end
|
12
|
+
|
13
|
+
def deliver_later(**)
|
14
|
+
return if early_return?
|
15
|
+
|
16
|
+
SGMailer.send(@mail)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def early_return?
|
22
|
+
@mail.nil? || @mail.empty?
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module SGMailer
|
2
|
+
class MailBuilder
|
3
|
+
def self.build(*args)
|
4
|
+
new(*args).build
|
5
|
+
end
|
6
|
+
|
7
|
+
def initialize(from:, to:, template_id:, substitutions: {})
|
8
|
+
@from = normalize_email(from)
|
9
|
+
@to = Array(to).map { |email| normalize_email(email) }
|
10
|
+
@substitutions = normalize_substitutions(substitutions)
|
11
|
+
@template_id = template_id
|
12
|
+
end
|
13
|
+
|
14
|
+
def build
|
15
|
+
{ from: normalize_email(@from),
|
16
|
+
personalizations: [to: @to, substitutions: @substitutions],
|
17
|
+
content: content_for_template,
|
18
|
+
template_id: @template_id }
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def normalize_email(email)
|
24
|
+
if email.is_a?(String)
|
25
|
+
{ email: email }
|
26
|
+
else
|
27
|
+
email
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def normalize_substitutions(substitutions)
|
32
|
+
Hash[substitutions.map do |key, value|
|
33
|
+
[key.to_s, value.to_s]
|
34
|
+
end]
|
35
|
+
end
|
36
|
+
|
37
|
+
def content_for_template
|
38
|
+
[type: "text/html", value: "<html><body></body></html>"]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module SGMailer
|
2
|
+
class ResponseError < StandardError
|
3
|
+
def initialize(response)
|
4
|
+
@response = response
|
5
|
+
|
6
|
+
super(build_message)
|
7
|
+
end
|
8
|
+
|
9
|
+
def status
|
10
|
+
@response.status
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def build_message
|
16
|
+
"Error from SendGrid:\n#{@response.body}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/sg_mailer.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sg_mailer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'sg_mailer'
|
8
|
+
spec.version = SGMailer::VERSION
|
9
|
+
spec.authors = ['Genadi Samokovarov']
|
10
|
+
spec.email = ['gsamokovarov@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Action Mailer-like framework for SendGrid transactional mails'
|
13
|
+
spec.description = 'Action Mailer-like framework for SendGrid transactional mails'
|
14
|
+
spec.homepage = 'https://github.com/gsamokovarov/sg_mailer'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sg_mailer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Genadi Samokovarov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: Action Mailer-like framework for SendGrid transactional mails
|
56
|
+
email:
|
57
|
+
- gsamokovarov@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- CODE_OF_CONDUCT.md
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/sg_mailer.rb
|
72
|
+
- lib/sg_mailer/base.rb
|
73
|
+
- lib/sg_mailer/client.rb
|
74
|
+
- lib/sg_mailer/configuration_error.rb
|
75
|
+
- lib/sg_mailer/delayed_delivery_processor.rb
|
76
|
+
- lib/sg_mailer/immediate_delivery_processor.rb
|
77
|
+
- lib/sg_mailer/mail_builder.rb
|
78
|
+
- lib/sg_mailer/response_error.rb
|
79
|
+
- lib/sg_mailer/test_client.rb
|
80
|
+
- lib/sg_mailer/version.rb
|
81
|
+
- sg_mailer.gemspec
|
82
|
+
homepage: https://github.com/gsamokovarov/sg_mailer
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
metadata: {}
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 2.5.1
|
103
|
+
signing_key:
|
104
|
+
specification_version: 4
|
105
|
+
summary: Action Mailer-like framework for SendGrid transactional mails
|
106
|
+
test_files: []
|