sparkpost_rails 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +22 -0
- data/README.md +76 -0
- data/lib/sparkpost_rails/delivery_method.rb +126 -0
- data/lib/sparkpost_rails/exceptions.rb +4 -0
- data/lib/sparkpost_rails/railtie.rb +9 -0
- data/lib/sparkpost_rails/version.rb +4 -0
- data/lib/sparkpost_rails.rb +35 -0
- data/spec/content_spec.rb +34 -0
- data/spec/from_spec.rb +24 -0
- data/spec/options_spec.rb +69 -0
- data/spec/recipients_spec.rb +50 -0
- data/spec/reply_to_spec.rb +25 -0
- data/spec/response_spec.rb +26 -0
- data/spec/spec_helper.rb +50 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 368eabc1112630441a50e7b6c9b2024905900755
|
4
|
+
data.tar.gz: ed9358eb97aa57d35579b23825d537570b1563de
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 032d4a48dfd4946223695f197872e1abd4e111f85431017a443bbb620352a1f369a9296dfa9b0bc42047587ff474818326e67988ef776fda170ea8080b18f8c4
|
7
|
+
data.tar.gz: ef0a2aaa4f7ab3fdf4a8962195658c0265886ef7c682e2a6f29f5a517695ef29ee480e65868080d831822ec816d179fa17f59e52d4a6837c332067ac5d24d50b
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Kevin Kimball
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/sparkpost_rails.svg)](https://badge.fury.io/rb/sparkpost_rails)
|
2
|
+
[![Build Status](https://travis-ci.org/the-refinery/sparkpost_rails.svg?branch=master)](https://travis-ci.org/the-refinery/sparkpost_rails)
|
3
|
+
|
4
|
+
# SparkPost Rails
|
5
|
+
|
6
|
+
In `Gemfile` add
|
7
|
+
|
8
|
+
```
|
9
|
+
gem 'sparkpost_rails'
|
10
|
+
```
|
11
|
+
|
12
|
+
By default, the gem will look for your SparkPost API key in your environment, with the key
|
13
|
+
'SPARKPOST_API_KEY'. You can override this setting by identifying a different key in the initializer
|
14
|
+
(`config/initializers/sparkpost_rails.rb`):
|
15
|
+
|
16
|
+
```
|
17
|
+
SparkPostRails.configure do |c|
|
18
|
+
c.api_key = 'YOUR API KEY'
|
19
|
+
end
|
20
|
+
```
|
21
|
+
|
22
|
+
Additionally, the following configuration options are available to be set in your initializer
|
23
|
+
( `config/initializers/sparkpost_rails.rb`):
|
24
|
+
|
25
|
+
```
|
26
|
+
SparkPostRails.configure do |c|
|
27
|
+
c.track_opens = true
|
28
|
+
c.track_clicks = true
|
29
|
+
c.return_path = 'BOUNCE-EMAIL@YOUR-DOMAIN.COM'
|
30
|
+
c.campaign_id = 'YOUR-CAMPAIGN'
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
The default values for these optional configuration settings are:
|
35
|
+
|
36
|
+
```
|
37
|
+
track_opens = false
|
38
|
+
track_clicks = false
|
39
|
+
return_path = nil
|
40
|
+
campaign_id = nil
|
41
|
+
```
|
42
|
+
|
43
|
+
In `config/environments/production.rb` add
|
44
|
+
|
45
|
+
```
|
46
|
+
config.action_mailer.delivery_method = :sparkpost
|
47
|
+
```
|
48
|
+
|
49
|
+
The deliver! method returns the response data from the SparkPost API call as a hash:
|
50
|
+
|
51
|
+
```
|
52
|
+
response = UserMailer.welcome_email(user).deliver_now!
|
53
|
+
```
|
54
|
+
|
55
|
+
Example:
|
56
|
+
|
57
|
+
```
|
58
|
+
{"total_rejected_recipients"=>0, "total_accepted_recipients"=>1, "id"=>"00000000000000"}
|
59
|
+
```
|
60
|
+
|
61
|
+
# Update Note!
|
62
|
+
|
63
|
+
If you have been using Version 0.0.5 or earlier of this gem, when you upgrade, you'll need to
|
64
|
+
change your initalizer as follows:
|
65
|
+
|
66
|
+
```
|
67
|
+
SparkpostRails.configure do |c|
|
68
|
+
```
|
69
|
+
|
70
|
+
becomes:
|
71
|
+
|
72
|
+
```
|
73
|
+
SparkPostRails.configure do |c|
|
74
|
+
```
|
75
|
+
|
76
|
+
We have changed the module name to align with the official SparkPost gem's naming convention.
|
@@ -0,0 +1,126 @@
|
|
1
|
+
module SparkPostRails
|
2
|
+
class DeliveryMethod
|
3
|
+
require 'net/http'
|
4
|
+
|
5
|
+
attr_accessor :settings, :data, :response
|
6
|
+
|
7
|
+
def initialize(options = {})
|
8
|
+
@settings = options
|
9
|
+
end
|
10
|
+
|
11
|
+
def deliver!(mail)
|
12
|
+
@data = {content: {}}
|
13
|
+
|
14
|
+
prepare_recipients_from mail
|
15
|
+
prepare_from_address_from mail
|
16
|
+
prepare_reply_to_address_from mail
|
17
|
+
|
18
|
+
prepare_subject_from mail
|
19
|
+
prepare_content_from mail
|
20
|
+
|
21
|
+
prepare_options
|
22
|
+
prepare_headers
|
23
|
+
|
24
|
+
result = post_to_api
|
25
|
+
|
26
|
+
process_result result
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def prepare_recipients_from mail
|
31
|
+
@data[:recipients] = prepare_addresses(mail.to, mail[:to].display_names)
|
32
|
+
end
|
33
|
+
|
34
|
+
def prepare_addresses emails, names
|
35
|
+
emails = [emails] unless emails.is_a?(Array)
|
36
|
+
emails.each_with_index.map {|email, index| prepare_address(email, index, names) }
|
37
|
+
end
|
38
|
+
|
39
|
+
def prepare_address email, index, names
|
40
|
+
if !names[index].nil?
|
41
|
+
{ address: { email: email, name: names[index] } }
|
42
|
+
else
|
43
|
+
{ address: { email: email } }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def prepare_from_address_from mail
|
48
|
+
if !mail[:from].display_names.first.nil?
|
49
|
+
from = { email: mail.from.first, name: mail[:from].display_names.first }
|
50
|
+
else
|
51
|
+
from = { email: mail.from.first }
|
52
|
+
end
|
53
|
+
|
54
|
+
@data[:content][:from] = from
|
55
|
+
end
|
56
|
+
|
57
|
+
def prepare_reply_to_address_from mail
|
58
|
+
unless mail.reply_to.nil?
|
59
|
+
@data[:content][:reply_to] = mail.reply_to.first
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def prepare_subject_from mail
|
64
|
+
@data[:content][:subject] = mail.subject
|
65
|
+
end
|
66
|
+
|
67
|
+
def prepare_content_from mail
|
68
|
+
if mail.multipart?
|
69
|
+
@data[:content][:html] = cleanse_encoding(mail.html_part.body.to_s)
|
70
|
+
@data[:content][:text] = cleanse_encoding(mail.text_part.body.to_s)
|
71
|
+
else
|
72
|
+
@data[:content][:text] = cleanse_encoding(mail.body.to_s)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def cleanse_encoding content
|
77
|
+
::JSON.parse({c: content}.to_json)["c"]
|
78
|
+
end
|
79
|
+
|
80
|
+
def prepare_options
|
81
|
+
@data[:options] = {
|
82
|
+
:open_tracking => SparkPostRails.configuration.track_opens,
|
83
|
+
:click_tracking => SparkPostRails.configuration.track_clicks
|
84
|
+
}
|
85
|
+
|
86
|
+
unless SparkPostRails.configuration.campaign_id.nil?
|
87
|
+
@data[:campaign_id] = SparkPostRails.configuration.campaign_id
|
88
|
+
end
|
89
|
+
|
90
|
+
unless SparkPostRails.configuration.return_path.nil?
|
91
|
+
@data[:return_path] = SparkPostRails.configuration.return_path
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def prepare_headers
|
96
|
+
@headers = {
|
97
|
+
"Authorization" => SparkPostRails.configuration.api_key,
|
98
|
+
"Content-Type" => "application/json"
|
99
|
+
}
|
100
|
+
end
|
101
|
+
|
102
|
+
def post_to_api
|
103
|
+
url = "https://api.sparkpost.com/api/v1/transmissions"
|
104
|
+
|
105
|
+
uri = URI.parse(url)
|
106
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
107
|
+
http.use_ssl = true
|
108
|
+
|
109
|
+
request = Net::HTTP::Post.new(uri.path, @headers)
|
110
|
+
request.body = JSON.generate(@data)
|
111
|
+
|
112
|
+
http.request(request)
|
113
|
+
end
|
114
|
+
|
115
|
+
def process_result result
|
116
|
+
result_data = JSON.parse(result.body)
|
117
|
+
|
118
|
+
if result_data["errors"]
|
119
|
+
@response = result_data["errors"]
|
120
|
+
raise SparkPostRails::DeliveryException, @response
|
121
|
+
else
|
122
|
+
@response = result_data["results"]
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module SparkPostRails
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
initializer "sparkpost_rails.add_delivery_method" do
|
4
|
+
ActiveSupport.on_load :action_mailer do
|
5
|
+
ActionMailer::Base.add_delivery_method :sparkpost, SparkPostRails::DeliveryMethod, return_response: true
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "sparkpost_rails/delivery_method"
|
2
|
+
require "sparkpost_rails/exceptions"
|
3
|
+
require "sparkpost_rails/railtie"
|
4
|
+
|
5
|
+
module SparkPostRails
|
6
|
+
class << self
|
7
|
+
attr_accessor :configuration
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.configure
|
11
|
+
self.configuration ||= Configuration.new
|
12
|
+
yield(configuration)
|
13
|
+
end
|
14
|
+
|
15
|
+
class Configuration
|
16
|
+
attr_accessor :api_key
|
17
|
+
attr_accessor :track_opens
|
18
|
+
attr_accessor :track_clicks
|
19
|
+
attr_accessor :campaign_id
|
20
|
+
attr_accessor :return_path
|
21
|
+
|
22
|
+
def initialize
|
23
|
+
if ENV.has_key?("SPARKPOST_API_KEY")
|
24
|
+
@api_key = ENV["SPARKPOST_API_KEY"]
|
25
|
+
else
|
26
|
+
@api_key = ""
|
27
|
+
end
|
28
|
+
|
29
|
+
@track_opens = false
|
30
|
+
@track_clicks = false
|
31
|
+
@campaign_id = nil
|
32
|
+
@return_path = nil
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SparkPostRails::DeliveryMethod do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@delivery_method = SparkPostRails::DeliveryMethod.new
|
7
|
+
end
|
8
|
+
|
9
|
+
context "Content" do
|
10
|
+
it "sets the subject" do
|
11
|
+
test_email = Mailer.test_email
|
12
|
+
@delivery_method.deliver!(test_email)
|
13
|
+
|
14
|
+
expect(@delivery_method.data[:content][:subject]).to eq("Test Email")
|
15
|
+
end
|
16
|
+
|
17
|
+
it "handles single part content" do
|
18
|
+
test_email = Mailer.test_email
|
19
|
+
@delivery_method.deliver!(test_email)
|
20
|
+
|
21
|
+
expect(@delivery_method.data[:content][:text]).to eq("Hello, Testing!")
|
22
|
+
expect(@delivery_method.data[:content].has_key?(:html)).to eq(false)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "handles multi-part content" do
|
26
|
+
test_email = Mailer.test_email html_part: "<h1>Hello, Testing!</h1>"
|
27
|
+
@delivery_method.deliver!(test_email)
|
28
|
+
|
29
|
+
expect(@delivery_method.data[:content][:text]).to eq("Hello, Testing!")
|
30
|
+
expect(@delivery_method.data[:content][:html]).to eq("<h1>Hello, Testing!</h1>")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
data/spec/from_spec.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SparkPostRails::DeliveryMethod do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@delivery_method = SparkPostRails::DeliveryMethod.new
|
7
|
+
end
|
8
|
+
|
9
|
+
context "From" do
|
10
|
+
it "handles email only" do
|
11
|
+
test_email = Mailer.test_email
|
12
|
+
@delivery_method.deliver!(test_email)
|
13
|
+
|
14
|
+
expect(@delivery_method.data[:content][:from]).to eq({email: "from@example.com"})
|
15
|
+
end
|
16
|
+
|
17
|
+
it "handles name and email" do
|
18
|
+
test_email = Mailer.test_email from: "Joe Test <from@example.com>"
|
19
|
+
@delivery_method.deliver!(test_email)
|
20
|
+
|
21
|
+
expect(@delivery_method.data[:content][:from]).to eq({:email=>"from@example.com", :name=>"Joe Test"})
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SparkPostRails::DeliveryMethod do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@delivery_method = SparkPostRails::DeliveryMethod.new
|
7
|
+
end
|
8
|
+
|
9
|
+
context "Options" do
|
10
|
+
|
11
|
+
it "handles track_opens option" do
|
12
|
+
SparkPostRails.configure do |c|
|
13
|
+
c.track_opens = true
|
14
|
+
end
|
15
|
+
|
16
|
+
test_email = Mailer.test_email
|
17
|
+
@delivery_method.deliver!(test_email)
|
18
|
+
|
19
|
+
expect(@delivery_method.data[:options][:open_tracking]).to eq(true)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "handles track_clicks option" do
|
23
|
+
SparkPostRails.configure do |c|
|
24
|
+
c.track_clicks = true
|
25
|
+
end
|
26
|
+
|
27
|
+
test_email = Mailer.test_email
|
28
|
+
@delivery_method.deliver!(test_email)
|
29
|
+
|
30
|
+
expect(@delivery_method.data[:options][:click_tracking]).to eq(true)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "does not contain unset campaign_id" do
|
34
|
+
test_email = Mailer.test_email
|
35
|
+
@delivery_method.deliver!(test_email)
|
36
|
+
|
37
|
+
expect(@delivery_method.data.has_key?(:campaign_id)).to eq(false)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "contains supplied campaign_id" do
|
41
|
+
SparkPostRails.configure do |c|
|
42
|
+
c.campaign_id = "ABCD1234"
|
43
|
+
end
|
44
|
+
|
45
|
+
test_email = Mailer.test_email
|
46
|
+
@delivery_method.deliver!(test_email)
|
47
|
+
|
48
|
+
expect(@delivery_method.data[:campaign_id]).to eq("ABCD1234")
|
49
|
+
end
|
50
|
+
|
51
|
+
it "does not contain unset return_path" do
|
52
|
+
test_email = Mailer.test_email
|
53
|
+
@delivery_method.deliver!(test_email)
|
54
|
+
|
55
|
+
expect(@delivery_method.data.has_key?(:return_path)).to eq(false)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "contains supplied return_path" do
|
59
|
+
SparkPostRails.configure do |c|
|
60
|
+
c.return_path = "BOUNCE-EMAIL@EXAMPLE.COM"
|
61
|
+
end
|
62
|
+
|
63
|
+
test_email = Mailer.test_email
|
64
|
+
@delivery_method.deliver!(test_email)
|
65
|
+
|
66
|
+
expect(@delivery_method.data[:return_path]).to eq('BOUNCE-EMAIL@EXAMPLE.COM')
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SparkPostRails::DeliveryMethod do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@delivery_method = SparkPostRails::DeliveryMethod.new
|
7
|
+
end
|
8
|
+
|
9
|
+
context "Recipients" do
|
10
|
+
|
11
|
+
context "single recipient" do
|
12
|
+
it "handles email only" do
|
13
|
+
test_email = Mailer.test_email
|
14
|
+
@delivery_method.deliver!(test_email)
|
15
|
+
|
16
|
+
expect(@delivery_method.data[:recipients]).to eq([{address: {email: "to@example.com"}}])
|
17
|
+
end
|
18
|
+
|
19
|
+
it "handles name and email" do
|
20
|
+
test_email = Mailer.test_email to: "Joe Test <to@example.com>"
|
21
|
+
@delivery_method.deliver!(test_email)
|
22
|
+
|
23
|
+
expect(@delivery_method.data[:recipients]).to eq([{address: {email: "to@example.com", name: "Joe Test"}}])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "multiple recipients" do
|
28
|
+
it "handles email only" do
|
29
|
+
test_email = Mailer.test_email to: "to1@example.com, to2@example.com"
|
30
|
+
@delivery_method.deliver!(test_email)
|
31
|
+
|
32
|
+
expect(@delivery_method.data[:recipients]).to eq([{address: {email: "to1@example.com"}}, {address: {email: "to2@example.com"}}])
|
33
|
+
end
|
34
|
+
|
35
|
+
it "handles name and email" do
|
36
|
+
test_email = Mailer.test_email to: "Sam Test <to1@example.com>, Joe Test <to2@example.com>"
|
37
|
+
@delivery_method.deliver!(test_email)
|
38
|
+
|
39
|
+
expect(@delivery_method.data[:recipients]).to eq([{:address=>{:email=>"to1@example.com", :name=>"Sam Test"}}, {:address=>{:email=>"to2@example.com", :name=>"Joe Test"}}])
|
40
|
+
end
|
41
|
+
|
42
|
+
it "handles mix of email only and name/email" do
|
43
|
+
test_email = Mailer.test_email to: "Sam Test <to1@example.com>, to2@example.com"
|
44
|
+
@delivery_method.deliver!(test_email)
|
45
|
+
|
46
|
+
expect(@delivery_method.data[:recipients]).to eq([{:address=>{:email=>"to1@example.com", :name=>"Sam Test"}}, {:address=>{:email=>"to2@example.com"}}])
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SparkPostRails::DeliveryMethod do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@delivery_method = SparkPostRails::DeliveryMethod.new
|
7
|
+
end
|
8
|
+
|
9
|
+
context "Reply To" do
|
10
|
+
it "handles supplied value" do
|
11
|
+
test_email = Mailer.test_email reply_to: "reply_to@example.com"
|
12
|
+
@delivery_method.deliver!(test_email)
|
13
|
+
|
14
|
+
expect(@delivery_method.data[:content][:reply_to]).to eq("reply_to@example.com")
|
15
|
+
end
|
16
|
+
|
17
|
+
it "handles no value supplied" do
|
18
|
+
test_email = Mailer.test_email
|
19
|
+
@delivery_method.deliver!(test_email)
|
20
|
+
|
21
|
+
expect(@delivery_method.data[:content].has_key?(:reply_to)).to eq(false)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SparkPostRails::DeliveryMethod do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@delivery_method = SparkPostRails::DeliveryMethod.new
|
7
|
+
end
|
8
|
+
|
9
|
+
context "API Response Handling" do
|
10
|
+
it "returns result data on success" do
|
11
|
+
test_email = Mailer.test_email
|
12
|
+
response = @delivery_method.deliver!(test_email)
|
13
|
+
|
14
|
+
expect(response).to eq({"total_rejected_recipients"=>0, "total_accepted_recipients"=>1, "id"=>"00000000000000000"})
|
15
|
+
end
|
16
|
+
|
17
|
+
it "raises exception on error" do
|
18
|
+
stub_request(:any, "https://api.sparkpost.com/api/v1/transmissions").
|
19
|
+
to_return(body: "{\"errors\":[{\"message\":\"required field is missing\",\"description\":\"recipients or list_id required\",\"code\":\"1400\"}]}", status: 403)
|
20
|
+
|
21
|
+
test_email = Mailer.test_email
|
22
|
+
|
23
|
+
expect {@delivery_method.deliver!(test_email)}.to raise_exception(SparkPostRails::DeliveryException)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'webmock/rspec'
|
2
|
+
require 'rails'
|
3
|
+
require 'action_mailer'
|
4
|
+
require "sparkpost_rails"
|
5
|
+
|
6
|
+
RSpec.configure do |config|
|
7
|
+
|
8
|
+
config.before(:all) do
|
9
|
+
SparkPostRails.configure do |c|
|
10
|
+
c.api_key = "TESTKEY1234"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
config.before(:each) do
|
15
|
+
stub_request(:any, "https://api.sparkpost.com/api/v1/transmissions").
|
16
|
+
to_return(body: "{\"results\":{\"total_rejected_recipients\":0,\"total_accepted_recipients\":1,\"id\":\"00000000000000000\"}}", status: 200)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
#A default mailer to generate the mail object
|
22
|
+
class Mailer < ActionMailer::Base
|
23
|
+
def test_email options = {}
|
24
|
+
data = {
|
25
|
+
from: "from@example.com",
|
26
|
+
to: "to@example.com",
|
27
|
+
subject: "Test Email",
|
28
|
+
text_part: "Hello, Testing!"
|
29
|
+
}
|
30
|
+
|
31
|
+
data.merge! options
|
32
|
+
|
33
|
+
if data.has_key?(:html_part)
|
34
|
+
|
35
|
+
mail(data) do |format|
|
36
|
+
format.text {render text: data[:text_part]}
|
37
|
+
format.html {render text: data[:html_part]}
|
38
|
+
end
|
39
|
+
|
40
|
+
else
|
41
|
+
|
42
|
+
mail(data) do |format|
|
43
|
+
format.text {render text: data[:text_part]}
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sparkpost_rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kevin Kimball
|
8
|
+
- Dave Goerlich
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2016-04-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '4.0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '4.0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rspec
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 3.4.0
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 3.4.0
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: webmock
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 1.24.2
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 1.24.2
|
56
|
+
description: Delivery Method for Rails ActionMailer to send emails using the SparkPost
|
57
|
+
API
|
58
|
+
email:
|
59
|
+
- kwkimball@gmail.com
|
60
|
+
- dave.goerlich@the-refinery.io
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- LICENSE
|
66
|
+
- README.md
|
67
|
+
- lib/sparkpost_rails.rb
|
68
|
+
- lib/sparkpost_rails/delivery_method.rb
|
69
|
+
- lib/sparkpost_rails/exceptions.rb
|
70
|
+
- lib/sparkpost_rails/railtie.rb
|
71
|
+
- lib/sparkpost_rails/version.rb
|
72
|
+
- spec/content_spec.rb
|
73
|
+
- spec/from_spec.rb
|
74
|
+
- spec/options_spec.rb
|
75
|
+
- spec/recipients_spec.rb
|
76
|
+
- spec/reply_to_spec.rb
|
77
|
+
- spec/response_spec.rb
|
78
|
+
- spec/spec_helper.rb
|
79
|
+
homepage: https://github.com/the-refinery/sparkpost_rails
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata: {}
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 2.2.2
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: SparkPost for Rails
|
103
|
+
test_files:
|
104
|
+
- spec/spec_helper.rb
|
105
|
+
- spec/recipients_spec.rb
|
106
|
+
- spec/content_spec.rb
|
107
|
+
- spec/options_spec.rb
|
108
|
+
- spec/response_spec.rb
|
109
|
+
- spec/from_spec.rb
|
110
|
+
- spec/reply_to_spec.rb
|