sendgrid-rails 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/README.rdoc +61 -0
- data/Rakefile +2 -0
- data/lib/send_grid.rb +30 -0
- data/lib/send_grid/api_header.rb +35 -0
- data/lib/send_grid/version.rb +4 -0
- data/lib/sendgrid-rails.rb +5 -0
- data/send_grid.gemspec +25 -0
- data/spec/api_header_spec.rb +41 -0
- data/spec/fixtures/mailers/mailer.rb +10 -0
- data/spec/fixtures/views/mailer/email_with_multiple_recipients.text.erb +0 -0
- data/spec/mailer_spec.rb +20 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/version_spec.rb +8 -0
- metadata +130 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
= SendGrid gem for Rails
|
2
|
+
|
3
|
+
SendGrid gem provides ActionMailer extensions to use SendGrid API features in you emails.
|
4
|
+
It extends ActionMailer with next methods:
|
5
|
+
|
6
|
+
add_recipients(array_of_emails)
|
7
|
+
substitute(patters_string, array_of_substitunion_strings)
|
8
|
+
uniq_args(hash_of_unique_args)
|
9
|
+
category(category_string)
|
10
|
+
add_filter_setting(filter_name, setting_name, value)
|
11
|
+
|
12
|
+
== Examples
|
13
|
+
|
14
|
+
=== Rails 3 configuration
|
15
|
+
|
16
|
+
In your Gemfile:
|
17
|
+
|
18
|
+
gem 'sendgrid-rails', '>=1.0.0', :git => 'git://github.com/PavelTyk/sendgrid-rails.git'
|
19
|
+
|
20
|
+
In your config/environment.rb:
|
21
|
+
|
22
|
+
ActionMailer::Base.smtp_settings = {
|
23
|
+
:address => 'smtp.sendgrid.net',
|
24
|
+
:port => '25',
|
25
|
+
:domain => 'example.com',
|
26
|
+
:authentication => :plain,
|
27
|
+
:user_name => 'login@example.com',
|
28
|
+
:password => 'your password'
|
29
|
+
}
|
30
|
+
|
31
|
+
==== Adding multiple recipients:
|
32
|
+
|
33
|
+
class Mailer < ActionMailer::Base
|
34
|
+
default :from => 'no-reply@example.com',
|
35
|
+
:subject => 'An email sent via SendGrid'
|
36
|
+
|
37
|
+
def email_with_multiple_recipients
|
38
|
+
add_recipients %w(email1@email.com email2@email.com)
|
39
|
+
mail :body => 'Hello.'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
=== Adding substitution vars
|
44
|
+
|
45
|
+
Mailer class definition:
|
46
|
+
|
47
|
+
class Mailer < ActionMailer::Base
|
48
|
+
default :from => 'no-reply@example.com',
|
49
|
+
:subject => 'An email sent via SendGrid with substitutions'
|
50
|
+
|
51
|
+
def mail_with_substitutions
|
52
|
+
emails = %w(email1@email.com email2@email.com)
|
53
|
+
names = %w(User1 User2)
|
54
|
+
|
55
|
+
add_recipients emails
|
56
|
+
substitute '-user_name-', names
|
57
|
+
|
58
|
+
mail :body => "Hello, -user_name-."
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
data/Rakefile
ADDED
data/lib/send_grid.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
module SendGrid
|
2
|
+
autoload :ApiHeader, 'send_grid/api_header'
|
3
|
+
autoload :VERSION, 'send_grid/version'
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.class_eval do
|
7
|
+
include InstanceMethods
|
8
|
+
delegate :add_recipients, :substitute, :uniq_args, :category, :add_filter_setting, :to => :send_grid_header
|
9
|
+
alias_method_chain :mail, :send_grid
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module InstanceMethods
|
14
|
+
def send_grid_header
|
15
|
+
@send_grid_header ||= SendGrid::ApiHeader.new
|
16
|
+
end
|
17
|
+
|
18
|
+
def send_grid_stub_for_recipient_email
|
19
|
+
smtp_domain = self.class.smtp_settings[:domain]
|
20
|
+
self.class.smtp_settings[:user_name] || "group-delivery@#{smtp_domain}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def mail_with_send_grid(headers={}, &block)
|
24
|
+
headers[:to] ||= send_grid_stub_for_recipient_email
|
25
|
+
headers['X-SMTPAPI'] = send_grid_header.to_json
|
26
|
+
mail_without_send_grid(headers, &block)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class SendGrid::ApiHeader
|
2
|
+
def initialize
|
3
|
+
@data = {}
|
4
|
+
end
|
5
|
+
|
6
|
+
def add_recipients(recipients)
|
7
|
+
@data[:to] ||= []
|
8
|
+
@data[:to] |= Array.wrap(recipients)
|
9
|
+
end
|
10
|
+
|
11
|
+
def substitute(var, val)
|
12
|
+
@data[:sub] ||= {}
|
13
|
+
@data[:sub][var] = Array.wrap(val)
|
14
|
+
end
|
15
|
+
|
16
|
+
def uniq_args(val)
|
17
|
+
@data[:unique_args] = val if val.instance_of?(Hash)
|
18
|
+
end
|
19
|
+
|
20
|
+
def category(cat)
|
21
|
+
@data[:category] = cat
|
22
|
+
end
|
23
|
+
|
24
|
+
def add_filter_setting(fltr, setting, val)
|
25
|
+
@data[:filters] ||= {}
|
26
|
+
@data[:filters][fltr] ||= {}
|
27
|
+
@data[:filters][fltr][:settings] ||= {}
|
28
|
+
@data[:filters][fltr][:settings][setting] = val
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_json
|
32
|
+
@data.to_json
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
data/send_grid.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "send_grid/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "sendgrid-rails"
|
7
|
+
s.version = SendGrid::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["PavelTyk"]
|
10
|
+
s.email = ["paveltyk@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/PavelTyk/sendgrid-rails"
|
12
|
+
s.summary = %q{SendGrid gem fo Rails 3}
|
13
|
+
s.description = %q{Gem to extend ActionMailer with SendGrid API support}
|
14
|
+
|
15
|
+
s.rubyforge_project = "sendgrid-rails"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
s.add_development_dependency "rspec", "~> 2.5.0"
|
22
|
+
s.add_dependency "actionmailer", ">= 3.0.0"
|
23
|
+
s.add_dependency "activesupport", ">= 2.1.0"
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SendGrid::ApiHeader do
|
4
|
+
let(:header) { SendGrid::ApiHeader.new }
|
5
|
+
describe "#to_json" do
|
6
|
+
it "returns valid json if no data was set" do
|
7
|
+
header.to_json.should eql "{}"
|
8
|
+
end
|
9
|
+
|
10
|
+
it "contains 1 recipient (as array)" do
|
11
|
+
header.add_recipients 'email@email.com'
|
12
|
+
header.to_json.should eql '{"to":["email@email.com"]}'
|
13
|
+
end
|
14
|
+
|
15
|
+
it "contaions an array of recipients" do
|
16
|
+
header.add_recipients %w(email1@email.com email2@email.com)
|
17
|
+
header.to_json.should eql '{"to":["email1@email.com","email2@email.com"]}'
|
18
|
+
end
|
19
|
+
|
20
|
+
it "contains substitution" do
|
21
|
+
header.substitute :var1, 'Hello'
|
22
|
+
header.to_json.should eql '{"sub":{"var1":["Hello"]}}'
|
23
|
+
end
|
24
|
+
|
25
|
+
it "contains uniq args" do
|
26
|
+
header.uniq_args :arg1 => 'val1'
|
27
|
+
header.to_json.should eql '{"unique_args":{"arg1":"val1"}}'
|
28
|
+
end
|
29
|
+
|
30
|
+
it "contains category" do
|
31
|
+
header.category 'category_name'
|
32
|
+
header.to_json.should eql '{"category":"category_name"}'
|
33
|
+
end
|
34
|
+
|
35
|
+
it "contains filter settings" do
|
36
|
+
header.add_filter_setting :filter1, :setting1, 'val1'
|
37
|
+
header.to_json.should eql '{"filters":{"filter1":{"settings":{"setting1":"val1"}}}}'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
File without changes
|
data/spec/mailer_spec.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mailer do
|
4
|
+
describe 'email with multiple recipients' do
|
5
|
+
let(:recipients) { %w(email1@email.com email2@email.com) }
|
6
|
+
let(:mail) { Mailer.email_with_multiple_recipients recipients }
|
7
|
+
|
8
|
+
it 'set correct recipients in X-SMTAPI header' do
|
9
|
+
mail.to_s.should include('X-SMTPAPI: {"to":["email1@email.com","email2@email.com"]}')
|
10
|
+
end
|
11
|
+
|
12
|
+
# Send Grid will ignore this attr any way, but ActionMailer will raise error
|
13
|
+
# if :to attr is blank
|
14
|
+
it 'set default :to header if it was nil' do
|
15
|
+
ActionMailer::Base.smtp_settings = { :domain => 'sendgrid.com' }
|
16
|
+
mail.to.should eq ['group-delivery@sendgrid.com']
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sendgrid-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- PavelTyk
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-03-17 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 27
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 5
|
33
|
+
- 0
|
34
|
+
version: 2.5.0
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: actionmailer
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 7
|
46
|
+
segments:
|
47
|
+
- 3
|
48
|
+
- 0
|
49
|
+
- 0
|
50
|
+
version: 3.0.0
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: activesupport
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 11
|
62
|
+
segments:
|
63
|
+
- 2
|
64
|
+
- 1
|
65
|
+
- 0
|
66
|
+
version: 2.1.0
|
67
|
+
type: :runtime
|
68
|
+
version_requirements: *id003
|
69
|
+
description: Gem to extend ActionMailer with SendGrid API support
|
70
|
+
email:
|
71
|
+
- paveltyk@gmail.com
|
72
|
+
executables: []
|
73
|
+
|
74
|
+
extensions: []
|
75
|
+
|
76
|
+
extra_rdoc_files: []
|
77
|
+
|
78
|
+
files:
|
79
|
+
- .gitignore
|
80
|
+
- .rspec
|
81
|
+
- Gemfile
|
82
|
+
- README.rdoc
|
83
|
+
- Rakefile
|
84
|
+
- lib/send_grid.rb
|
85
|
+
- lib/send_grid/api_header.rb
|
86
|
+
- lib/send_grid/version.rb
|
87
|
+
- lib/sendgrid-rails.rb
|
88
|
+
- send_grid.gemspec
|
89
|
+
- spec/api_header_spec.rb
|
90
|
+
- spec/fixtures/mailers/mailer.rb
|
91
|
+
- spec/fixtures/views/mailer/email_with_multiple_recipients.text.erb
|
92
|
+
- spec/mailer_spec.rb
|
93
|
+
- spec/spec_helper.rb
|
94
|
+
- spec/version_spec.rb
|
95
|
+
has_rdoc: true
|
96
|
+
homepage: https://github.com/PavelTyk/sendgrid-rails
|
97
|
+
licenses: []
|
98
|
+
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
hash: 3
|
110
|
+
segments:
|
111
|
+
- 0
|
112
|
+
version: "0"
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
hash: 3
|
119
|
+
segments:
|
120
|
+
- 0
|
121
|
+
version: "0"
|
122
|
+
requirements: []
|
123
|
+
|
124
|
+
rubyforge_project: sendgrid-rails
|
125
|
+
rubygems_version: 1.5.0
|
126
|
+
signing_key:
|
127
|
+
specification_version: 3
|
128
|
+
summary: SendGrid gem fo Rails 3
|
129
|
+
test_files: []
|
130
|
+
|