mail_male_mail 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,9 +4,11 @@ require "action_mailer"
4
4
  require "mail_male_mail/sendgrid"
5
5
  require "mail_male_mail/mailgun"
6
6
  require "mail_male_mail/configuration"
7
+ require "mail_male_mail/postal_service/delivery_method"
8
+ require "mail_male_mail/postal_service/mailer"
7
9
 
8
10
  module MailMaleMail
9
- PROVIDERS = %w(sendgrid mailgun)
11
+ PROVIDERS = %w(sendgrid mailgun postal_service)
10
12
 
11
13
  if defined? Rails::Railtie
12
14
  class MailMaleMailRailtie < Rails::Railtie
@@ -15,6 +17,7 @@ module MailMaleMail
15
17
  raise LoadError, "#{MailMaleMail::Configuration.filepath} is required for MailMaleMail and does not exist"
16
18
  end
17
19
  ActionMailer::Base.send(:include, MailMaleMail)
20
+ ActionMailer::Base.add_delivery_method :postal_service, MailMaleMail::PostalService::DeliveryMethod, {}
18
21
  end
19
22
  end
20
23
  end
@@ -25,6 +28,7 @@ module MailMaleMail
25
28
  Configuration.load
26
29
  include(Sendgrid)
27
30
  include(Mailgun)
31
+ include(PostalService::Mailer)
28
32
  class << self
29
33
  attr_accessor :mmm_provider
30
34
  end
@@ -57,8 +61,8 @@ module MailMaleMail
57
61
  def mailman(name)
58
62
  if config = Configuration.get(name)
59
63
  self.delivery_method = config['delivery_method'].to_sym if config.key?('delivery_method')
60
- if config.key?('smtp_settings') && config['smtp_settings'].is_a?(Hash)
61
- self.smtp_settings = config['smtp_settings'].symbolize_keys
64
+ if config.key?("#{self.delivery_method}_settings") && config["#{self.delivery_method}_settings"].is_a?(Hash)
65
+ self.send("#{self.delivery_method}_settings=", config["#{self.delivery_method}_settings"].symbolize_keys)
62
66
  end
63
67
  if config.key?('provider') && PROVIDERS.include?(config['provider'])
64
68
  self.mmm_provider = config['provider']
@@ -0,0 +1,52 @@
1
+ module MailMaleMail
2
+ module PostalService
3
+ def self.api_request(settings, message)
4
+ if settings[:method].to_s == "iron_mq"
5
+ queue = IronMQ::Client.new(token: settings[:token], project_id: settings[:project_id]).queue(settings[:queue])
6
+ queue.post(message.to_json)
7
+ else
8
+ uri = URI(url)
9
+ req = Net::HTTP::Post.new("#{uri.path}?#{uri.query}")
10
+ req.body = message.to_json
11
+ req.content_type = 'application/json'
12
+ http = Net::HTTP.new(uri.host, uri.port)
13
+ response = http.request(req)
14
+ end
15
+ end
16
+ class DeliveryMethod
17
+ class InvalidOption < StandardError; end
18
+
19
+ attr_accessor :settings
20
+ def initialize(options = {})
21
+ unless options[:url].present? || options[:method].present?
22
+ raise InvalidOption, "A url or method option is required to send email using the Postal Service delivery method"
23
+ end
24
+ self.settings = options
25
+ end
26
+ def deliver!(mail)
27
+ message = {}
28
+ message[:to] = mail.to.join(", ")
29
+ message[:subject] = mail.subject.to_s
30
+ message[:from] = mail.from.first.to_s
31
+ message[:extra_provider_data] = ActiveSupport::JSON.decode(mail.header['X-PostalService-Data'].to_s) if mail.header['X-PostalService-Data']
32
+ if mail.header['X-PostalService-Provider']
33
+ message[:provider] = mail.header['X-PostalService-Provider'].to_s
34
+ elsif self.settings[:provider]
35
+ message[:provider] = self.settings[:provider]
36
+ end
37
+
38
+ message[:category] = mail.header['X-PostalService-Category'].to_s if mail.header['X-PostalService-Category']
39
+
40
+ message[:text] = mail.text_part.body.to_s if mail.text_part
41
+ message[:html] = mail.html_part.body.to_s if mail.html_part
42
+ unless mail.text_part || mail.html_part
43
+ part = mail.content_type =~ /html/ ? :html : :text
44
+ message[part] = mail.body.to_s
45
+ end
46
+ MailMaleMail::PostalService.api_request(self.settings, message)
47
+ end
48
+
49
+
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,14 @@
1
+ module MailMaleMail
2
+ module PostalService
3
+ module Mailer
4
+ def write_postal_service_headers
5
+ if mmm_category == :use_subject_lines
6
+ self.headers['X-PostalService-Category'] = message.subject
7
+ elsif mmm_category
8
+ self.headers['X-PostalService-Category'] = mmm_category
9
+ end
10
+ self.headers['X-PostalService-Data'] = mmm_variables.to_json.gsub(/(["\]}])([,:])(["\[{])/, '\\1\\2 \\3') if mmm_variables
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module MailMaleMail
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -28,5 +28,7 @@ production:
28
28
  enable_starttls_auto: true
29
29
  user_name: "test@example.com"
30
30
  password: "superamazingpasswordformailgun"
31
-
31
+ postal_service:
32
+ provider: postal_service
33
+ delivery_method: postal_service
32
34
 
@@ -3,15 +3,15 @@ require 'spec_helper'
3
3
  class MMMMailgunMailer < MMMailer
4
4
  mailman :mailgun
5
5
  def test_mail
6
- set_mmm_cateogry("MailgunCategory1")
7
- set_mmm_variables(:color => "green", :sound => "bark")
6
+ mail_male_mail_category("MailgunCategory1")
7
+ mail_male_mail_variables(:color => "green", :sound => "bark")
8
8
  mail(:subject => "Mailgun Testing", :from => "test@example.com", :to => "test2@example.com")
9
9
  end
10
10
  end
11
11
 
12
12
  module MailMaleMail
13
- describe Sendgrid do
14
- it "should set the X-SMTPAPI header with category unique_args" do
13
+ describe Mailgun do
14
+ it "should set the mailgun headers with tag and variables" do
15
15
  mail = MMMMailgunMailer.test_mail
16
16
  mail.header['X-Mailgun-Tag'].to_s.should == "MailgunCategory1"
17
17
  mail.header['X-Mailgun-Variables'].to_s.should == "{\"color\": \"green\", \"sound\": \"bark\"}"
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ class MMMPostalServiceMailer < MMMailer
4
+ mailman :postal_service
5
+ def test_mail
6
+ mail_male_mail_category("PostalServiceCategory1")
7
+ mail_male_mail_variables(:color => "yellow", :sound => "moo")
8
+ mail(:subject => "PostalService Testing", :from => "test@example.com", :to => "test2@example.com")
9
+ end
10
+ end
11
+
12
+ module MailMaleMail
13
+ module PostalService
14
+ describe Mailer do
15
+ it "should set the postal service headers for category and data" do
16
+ mail = MMMPostalServiceMailer.test_mail
17
+ mail.header['X-PostalService-Category'].to_s.should == "PostalServiceCategory1"
18
+ mail.header['X-PostalService-Data'].to_s.should == "{\"color\": \"yellow\", \"sound\": \"moo\"}"
19
+ end
20
+ end
21
+ end
22
+ end
23
+
@@ -3,8 +3,8 @@ require 'spec_helper'
3
3
  class MMMSendGridMailer < MMMailer
4
4
  mailman :sendgrid
5
5
  def test_mail
6
- set_mmm_cateogry("SendgridCategory1")
7
- set_mmm_variables(:color => "blue", sound: "meow")
6
+ mail_male_mail_category("SendgridCategory1")
7
+ mail_male_mail_variables(:color => "blue", sound: "meow")
8
8
  mail(:subject => "Sengrid Testing", :from => "test@example.com", :to => "test2@example.com")
9
9
  end
10
10
  end
data/spec/spec_helper.rb CHANGED
@@ -13,6 +13,7 @@ end
13
13
 
14
14
  require "mail_male_mail"
15
15
  ActionMailer::Base.send(:include, MailMaleMail)
16
+ ActionMailer::Base.add_delivery_method :postal_service, MailMaleMail::PostalService::DeliveryMethod, {:url => "http://example.com"}
16
17
  class MMMailer < ActionMailer::Base
17
18
  end
18
19
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mail_male_mail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-22 00:00:00.000000000 Z
12
+ date: 2013-05-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: actionmailer
@@ -74,11 +74,14 @@ files:
74
74
  - lib/mail_male_mail.rb
75
75
  - lib/mail_male_mail/configuration.rb
76
76
  - lib/mail_male_mail/mailgun.rb
77
+ - lib/mail_male_mail/postal_service/delivery_method.rb
78
+ - lib/mail_male_mail/postal_service/mailer.rb
77
79
  - lib/mail_male_mail/sendgrid.rb
78
80
  - lib/mail_male_mail/version.rb
79
81
  - mail_male_mail.gemspec
80
82
  - spec/config/mail_male_mail.yml
81
83
  - spec/mail_male_mail/mailgun_spec.rb
84
+ - spec/mail_male_mail/postal_service/mailer_spec.rb
82
85
  - spec/mail_male_mail/sendgrid_spec.rb
83
86
  - spec/mail_male_mail_spec.rb
84
87
  - spec/spec_helper.rb
@@ -109,6 +112,7 @@ summary: extend actionmailer to work with multiple mail providers
109
112
  test_files:
110
113
  - spec/config/mail_male_mail.yml
111
114
  - spec/mail_male_mail/mailgun_spec.rb
115
+ - spec/mail_male_mail/postal_service/mailer_spec.rb
112
116
  - spec/mail_male_mail/sendgrid_spec.rb
113
117
  - spec/mail_male_mail_spec.rb
114
118
  - spec/spec_helper.rb