http_mailer 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/http_mailer.rb +4 -10
- data/lib/http_mailer/client.rb +29 -0
- data/lib/http_mailer/mailgun/mailgun_service_handler.rb +5 -1
- data/lib/http_mailer/mandrill/mandrill_message.rb +1 -1
- data/lib/http_mailer/mandrill/mandrill_service_handler.rb +2 -2
- data/lib/http_mailer/sendgrid/sendgrid_service_handler.rb +1 -1
- data/lib/http_mailer/service_handler.rb +5 -0
- data/lib/http_mailer/version.rb +1 -1
- data/spec/client_spec.rb +34 -0
- data/spec/http_mailer_spec.rb +8 -27
- data/spec/mailgun/mailgun_service_handler_spec.rb +32 -0
- data/spec/mandrill/mandrill_message_spec.rb +1 -1
- data/spec/mandrill/mandrill_service_handler_spec.rb +2 -1
- data/spec/sendgrid/sendgrid_service_handler_spec.rb +1 -0
- data/spec/service_handler_spec.rb +28 -0
- data/spec/spec_helper.rb +5 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb960b319971c84288901f6242b8b7e9ede0caf3
|
4
|
+
data.tar.gz: 42033f95265ad6afb618e5152b03d0ca09a5b689
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0553e51355e8f429873706facbd84ff5edfa5c3038981ebe9b614893b39b93339b0dc78dab7b5ef394e1d6547c3e014cb6b0ce9c64b1340975fc3f5305ddfdc3
|
7
|
+
data.tar.gz: 1ea8d630febe20b7f11afcc0b08fc1671e7fc928c66e3d5ad11f054a2e426288a1aed9e75858e7e8895a8ed8955a1a45bfa83f58e7bf9a96a90d9a765a3df443
|
data/lib/http_mailer.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'rest_client'
|
2
2
|
require "http_mailer/version"
|
3
|
+
require "http_mailer/client"
|
3
4
|
require "http_mailer/service_handler"
|
4
5
|
require "http_mailer/service_configuration"
|
5
6
|
require "http_mailer/mailgun/mailgun_service_api"
|
@@ -13,16 +14,9 @@ require "http_mailer/sendgrid/sendgrid_service_handler"
|
|
13
14
|
module HttpMailer
|
14
15
|
|
15
16
|
class << self
|
16
|
-
def
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
def sendgrid(settings)
|
21
|
-
SendGridServiceHandler.new(settings)
|
22
|
-
end
|
23
|
-
|
24
|
-
def mandrill(settings)
|
25
|
-
MandrillServiceHandler.new(settings)
|
17
|
+
def client(settings)
|
18
|
+
HttpMailer::Client.new(settings)
|
26
19
|
end
|
27
20
|
end
|
21
|
+
|
28
22
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module HttpMailer
|
2
|
+
class Client
|
3
|
+
attr_reader :configuration, :mailgun, :sendgrid, :mandrill
|
4
|
+
|
5
|
+
def initialize(settings)
|
6
|
+
configure(settings)
|
7
|
+
end
|
8
|
+
|
9
|
+
def configure(settings)
|
10
|
+
@mailgun = MailgunServiceHandler.new(settings[:mailgun])
|
11
|
+
@sendgrid = SendGridServiceHandler.new(settings[:sendgrid])
|
12
|
+
@mandrill = MandrillServiceHandler.new(settings[:mandrill])
|
13
|
+
end
|
14
|
+
|
15
|
+
def send_message(from, to, subject, body, from_name='', to_name='')
|
16
|
+
response = nil
|
17
|
+
|
18
|
+
[mailgun, sendgrid, mandrill].each do |service|
|
19
|
+
if service.configured?
|
20
|
+
response = service.send_message(from, to, subject, body, to_name, from_name)
|
21
|
+
break if response.code == 200
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
return response
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -10,7 +10,11 @@ module HttpMailer
|
|
10
10
|
)
|
11
11
|
end
|
12
12
|
|
13
|
-
def
|
13
|
+
def configured?
|
14
|
+
super && !self.service_configuration.settings.subdomain.nil?
|
15
|
+
end
|
16
|
+
|
17
|
+
def send_message(from, to, subject, text, from_name='', to_name='')
|
14
18
|
::RestClient.post self.service_api.send_messages_url,
|
15
19
|
:from => from,
|
16
20
|
:to => to,
|
@@ -3,7 +3,7 @@ module HttpMailer
|
|
3
3
|
attr_accessor :to, :to_name, :from, :from_name, :subject, :text, :html
|
4
4
|
attr_reader :structure
|
5
5
|
|
6
|
-
def initialize(
|
6
|
+
def initialize(from, to, subject, text=nil, html=nil, from_name=nil, to_name=nil)
|
7
7
|
@to = to
|
8
8
|
@from = from
|
9
9
|
@subject = subject
|
@@ -7,8 +7,8 @@ module HttpMailer
|
|
7
7
|
@service_api = ::HttpMailer::MandrillServiceApi.new(self.service_configuration.settings.host)
|
8
8
|
end
|
9
9
|
|
10
|
-
def send_message(from, to, subject, text,
|
11
|
-
message = MandrillMessage.new(from, to, subject, text, nil,
|
10
|
+
def send_message(from, to, subject, text, from_name='', to_name='')
|
11
|
+
message = MandrillMessage.new(from, to, subject, text, nil, from_name, to_name)
|
12
12
|
payload = {
|
13
13
|
:key => self.service_configuration.settings.api_key,
|
14
14
|
:message => message.to_h
|
@@ -7,7 +7,7 @@ module HttpMailer
|
|
7
7
|
@service_api = ::HttpMailer::SendGridServiceApi.new(self.service_configuration.settings.host)
|
8
8
|
end
|
9
9
|
|
10
|
-
def send_message(from, to, subject, text,
|
10
|
+
def send_message(from, to, subject, text, from_name='', to_name='')
|
11
11
|
::RestClient.post self.service_api.send_messages_url,
|
12
12
|
:api_user => self.service_configuration.settings.api_user,
|
13
13
|
:api_key => self.service_configuration.settings.api_key,
|
data/lib/http_mailer/version.rb
CHANGED
data/spec/client_spec.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HttpMailer::Client do
|
4
|
+
|
5
|
+
context "when configured" do
|
6
|
+
let(:settings){ {:mailgun => {:host => "api.mailgun.net", :api_key => "12345", :subdomain => "sandbox12345.mailgun.org"},
|
7
|
+
:sendgrid => {:host => "sendgrid.com", :api_user=> "test", :api_key => "1234"},
|
8
|
+
:mandrill => {:host => "mandrillapp.com", :api_key => "1234567890"}} }
|
9
|
+
|
10
|
+
describe "#initialize" do
|
11
|
+
it "creates a client instance" do
|
12
|
+
expect(HttpMailer::Client.new(settings)).to be_a HttpMailer::Client
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#send_message' do
|
17
|
+
let(:settings){ {:mailgun => {:host => "api.mailgun.net", :api_key => "12345", :subdomain => "sandbox12345.mailgun.org"},
|
18
|
+
:sendgrid => {:host => "sendgrid.com", :api_user=> "test", :api_key => "1234"},
|
19
|
+
:mandrill => {:host => "mandrillapp.com", :api_key => "1234567890"}} }
|
20
|
+
let(:mailer_client){ HttpMailer::Client.new(settings) }
|
21
|
+
|
22
|
+
let(:sender){ "sender@test.com"}
|
23
|
+
let(:reciever){ "reciever@test.com>" }
|
24
|
+
let(:subject){ "Hello" }
|
25
|
+
let(:email_body){ "Congratulations, you just sent an email with HttpMailer! You are truly awesome!" }
|
26
|
+
|
27
|
+
it 'sends an email' do
|
28
|
+
response = mailer_client.send_message(sender, reciever, subject, email_body)
|
29
|
+
expect(response.code).to eq(200)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
data/spec/http_mailer_spec.rb
CHANGED
@@ -2,33 +2,14 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe HttpMailer do
|
4
4
|
|
5
|
-
context "when configured
|
6
|
-
let(:settings){ {:host => "api.mailgun.net", :api_key => "12345", :subdomain => "sandbox12345.mailgun.org"}
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
end
|
14
|
-
|
15
|
-
context "when configured for SendGrid" do
|
16
|
-
let(:settings){ {:host => "sendgrid.com", :api_user=> "test", :api_key => "1234"} }
|
17
|
-
|
18
|
-
describe ".sendgrid" do
|
19
|
-
it "creates a service handler for SendGrid" do
|
20
|
-
expect(HttpMailer.sendgrid(settings)).to be_a HttpMailer::SendGridServiceHandler
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
context "when configured for Mandrill" do
|
27
|
-
let(:settings){ {:host => "mandrillapp.com", :api_key => "1234567890"} }
|
28
|
-
|
29
|
-
describe ".mandrill" do
|
30
|
-
it "creates a service handler for Mandrill" do
|
31
|
-
expect(HttpMailer.mandrill(settings)).to be_a HttpMailer::MandrillServiceHandler
|
5
|
+
context "when configured" do
|
6
|
+
let(:settings){ {:mailgun => {:host => "api.mailgun.net", :api_key => "12345", :subdomain => "sandbox12345.mailgun.org"},
|
7
|
+
:sendgrid => {:host => "sendgrid.com", :api_user=> "test", :api_key => "1234"},
|
8
|
+
:mandrill => {:host => "mandrillapp.com", :api_key => "1234567890"}} }
|
9
|
+
|
10
|
+
describe ".client" do
|
11
|
+
it "creates a client instance" do
|
12
|
+
expect(HttpMailer.client(settings)).to be_a HttpMailer::Client
|
32
13
|
end
|
33
14
|
end
|
34
15
|
end
|
@@ -15,4 +15,36 @@ describe HttpMailer::MailgunServiceHandler do
|
|
15
15
|
expect(response.code).to eq(200)
|
16
16
|
end
|
17
17
|
end
|
18
|
+
|
19
|
+
describe "#configured?" do
|
20
|
+
context "fully configured" do
|
21
|
+
let(:settings){ {:host => "api.mailgun.net", :api_key => "12345", :subdomain => "sandbox12345.mailgun.org"} }
|
22
|
+
let(:mailer){ HttpMailer::MailgunServiceHandler.new(settings) }
|
23
|
+
|
24
|
+
it 'returns true' do
|
25
|
+
expect(mailer.configured?).to be true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context "not fully configured" do
|
30
|
+
let(:no_settings){ {} }
|
31
|
+
let(:mailer_with_no_settings){ HttpMailer::MailgunServiceHandler.new(no_settings) }
|
32
|
+
|
33
|
+
let(:settings_missing_host){ {:api_key => "12345", :subdomain => "sandbox12345.mailgun.org"} }
|
34
|
+
let(:mailer_missing_host){ HttpMailer::MailgunServiceHandler.new(settings_missing_host) }
|
35
|
+
|
36
|
+
let(:settings_missing_api_key){ {:host => "api.mailgun.net", :subdomain => "sandbox12345.mailgun.org"} }
|
37
|
+
let(:mailer_missing_api_key){ HttpMailer::MailgunServiceHandler.new(settings_missing_api_key) }
|
38
|
+
|
39
|
+
let(:settings_missing_subdomain){ {:host => "api.mailgun.net", :api_key => "12345"} }
|
40
|
+
let(:mailer_missing_subdomain){ HttpMailer::MailgunServiceHandler.new(settings_missing_subdomain) }
|
41
|
+
|
42
|
+
it 'returns false' do
|
43
|
+
expect(mailer_with_no_settings.configured?).to be false
|
44
|
+
expect(mailer_missing_host.configured?).to be false
|
45
|
+
expect(mailer_missing_api_key.configured?).to be false
|
46
|
+
expect(mailer_missing_subdomain.configured?).to be false
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
18
50
|
end
|
@@ -10,7 +10,7 @@ describe HttpMailer::MandrillMessage do
|
|
10
10
|
let(:subject){ "Hello" }
|
11
11
|
let(:text_body){ "Congratulations, you just sent an email with Mandrill! You are truly awesome!" }
|
12
12
|
let(:html_body){ "<h1>Congratulations, you just sent an email with Mandrill! You are truly awesome!</h1>"}
|
13
|
-
let(:message){ HttpMailer::MandrillMessage.new(
|
13
|
+
let(:message){ HttpMailer::MandrillMessage.new(from, to, subject, text_body, html_body, from_name, to_name) }
|
14
14
|
|
15
15
|
it 'builds a propery formated Mandrill message' do
|
16
16
|
expect(message.to_h[:to]).to eq [{:email => to, :name => to_name}]
|
@@ -13,8 +13,9 @@ describe HttpMailer::MandrillServiceHandler do
|
|
13
13
|
let(:email_body){ "Congratulations, you just sent an email with Mandrill! You are truly awesome!" }
|
14
14
|
|
15
15
|
it 'sends an email message' do
|
16
|
-
response = mailer.send_message(
|
16
|
+
response = mailer.send_message(from, to, subject, email_body, from_name, to_name)
|
17
17
|
expect(response.code).to eq(200)
|
18
18
|
end
|
19
19
|
end
|
20
|
+
|
20
21
|
end
|
@@ -12,4 +12,32 @@ describe HttpMailer::ServiceHandler do
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
+
describe "#configured?" do
|
16
|
+
context "fully configured" do
|
17
|
+
let(:settings){ {:host => "mandrillapp.com", :api_key => "1234567890"} }
|
18
|
+
let(:mailer){ HttpMailer::ServiceHandler.new(settings) }
|
19
|
+
|
20
|
+
it 'returns true' do
|
21
|
+
expect(mailer.configured?).to be true
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "not fully configured" do
|
26
|
+
let(:no_settings){ {} }
|
27
|
+
let(:mailer_with_no_settings){ HttpMailer::ServiceHandler.new(no_settings) }
|
28
|
+
|
29
|
+
let(:settings_missing_host){ {:api_key => "1234567890"} }
|
30
|
+
let(:mailer_missing_host){ HttpMailer::ServiceHandler.new(settings_missing_host) }
|
31
|
+
|
32
|
+
let(:settings_missing_api_key){ {:host => "mandrillapp.com"} }
|
33
|
+
let(:mailer_missing_api_key){ HttpMailer::ServiceHandler.new(settings_missing_api_key) }
|
34
|
+
|
35
|
+
it 'returns false' do
|
36
|
+
expect(mailer_with_no_settings.configured?).to be false
|
37
|
+
expect(mailer_missing_host.configured?).to be false
|
38
|
+
expect(mailer_missing_api_key.configured?).to be false
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
15
43
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -12,6 +12,11 @@ RSpec.configure do |config|
|
|
12
12
|
:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'224', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}).
|
13
13
|
to_return(:status => 200, :body => "", :headers => {})
|
14
14
|
|
15
|
+
stub_request(:post, "https://api:key-12345@api.mailgun.net/v2/sandbox12345.mailgun.org/messages").
|
16
|
+
with(:body => {"from"=>"sender@test.com", "subject"=>"Hello", "text"=>"Congratulations, you just sent an email with HttpMailer! You are truly awesome!", "to"=>"reciever@test.com>"},
|
17
|
+
:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'174', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}).
|
18
|
+
to_return(:status => 200, :body => "", :headers => {})
|
19
|
+
|
15
20
|
# Stub SendGrid HTTP requests
|
16
21
|
stub_request(:post, "https://sendgrid.com/api/mail.send.json").
|
17
22
|
with(:body => {"api_key"=>/.*/, "api_user"=>/.*/, "from"=>/.*/, "fromname"=>/.*/, "subject"=>/.*/, "text"=>/.*/, "to"=>/.*/, "toname"=>/.*/},
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: http_mailer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dru Ibarra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -94,6 +94,7 @@ files:
|
|
94
94
|
- Rakefile
|
95
95
|
- http_mailer.gemspec
|
96
96
|
- lib/http_mailer.rb
|
97
|
+
- lib/http_mailer/client.rb
|
97
98
|
- lib/http_mailer/mailgun/mailgun_service_api.rb
|
98
99
|
- lib/http_mailer/mailgun/mailgun_service_handler.rb
|
99
100
|
- lib/http_mailer/mandrill/mandrill_message.rb
|
@@ -104,6 +105,7 @@ files:
|
|
104
105
|
- lib/http_mailer/service_configuration.rb
|
105
106
|
- lib/http_mailer/service_handler.rb
|
106
107
|
- lib/http_mailer/version.rb
|
108
|
+
- spec/client_spec.rb
|
107
109
|
- spec/http_mailer_spec.rb
|
108
110
|
- spec/mailgun/mailgun_service_api_spec.rb
|
109
111
|
- spec/mailgun/mailgun_service_handler_spec.rb
|
@@ -140,6 +142,7 @@ signing_key:
|
|
140
142
|
specification_version: 4
|
141
143
|
summary: Sends email via HTTP APIs
|
142
144
|
test_files:
|
145
|
+
- spec/client_spec.rb
|
143
146
|
- spec/http_mailer_spec.rb
|
144
147
|
- spec/mailgun/mailgun_service_api_spec.rb
|
145
148
|
- spec/mailgun/mailgun_service_handler_spec.rb
|