Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dtaniwaki committed Jun 28, 2014
0 parents commit 9f4b268
Show file tree
Hide file tree
Showing 22 changed files with 702 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
*.gem
*.rbc
/.config
/coverage/
/InstalledFiles
/pkg/
/spec/reports/
/test/tmp/
/test/version_tmp/
/tmp/
/log/

## Specific to RubyMotion:
.dat*
.repl_history
build/

## Documentation cache and generated files:
/.yardoc/
/_yardoc/
/doc/
/rdoc/

## Environment normalisation:
/.bundle/
/lib/bundler/man/

## Misc
Gemfile.lock
gemfiles/*.lock
.ruby-version
.ruby-gemset
.rvmrc
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
language: ruby

rvm:
- 1.9.3
- 2.0.0
- 2.1.0
gemfile:
- gemfiles/gemfile
- gemfiles/actionmailer.3.0.x.gemfile
- gemfiles/actionmailer.3.1.x.gemfile
- gemfiles/actionmailer.3.2.x.gemfile
- gemfiles/actionmailer.4.0.x.gemfile
- gemfiles/actionmailer.4.1.x.gemfile

script: "bundle exec rake spec"

branches:
only:
- master
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gemspec
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 Daisuke Taniwaki

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# mandriller

[![Gem Version](https://badge.fury.io/rb/mandriller.svg)](http://badge.fury.io/rb/mandriller) [![Build Status](https://secure.travis-ci.org/dtaniwaki/mandriller.png)](http://travis-ci.org/dtaniwaki/mandriller) [![Coverage Status](https://coveralls.io/repos/dtaniwaki/mandriller/badge.png)](https://coveralls.io/r/dtaniwaki/mandriller) [![Code Climate](https://codeclimate.com/github/dtaniwaki/mandriller.png)](https://codeclimate.com/github/dtaniwaki/mandriller)

Mandriller SMTP API integration for ActionMailer.

## Installation

Add the mandriller gem to your Gemfile.

```ruby
gem "mandriller"
```

And run `bundle install`.

## Usage

```ruby
class UserMailer < Mandriller::Base
set_google_analytics_campaign
set_open_track

def test_mail
set_click_track
mail from: '[email protected]', to: '[email protected]'
end
end
```

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new [Pull Request](../../pull/new/master)

## Copyright

Copyright (c) 2014 Daisuke Taniwaki. See [LICENSE](LICENSE) for details.
8 changes: 8 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "bundler/gem_tasks"

require 'rspec/core'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = FileList['spec/**/*_spec.rb']
end
task :default => :spec
4 changes: 4 additions & 0 deletions gemfiles/actionmailer.3.0.x.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source "http://rubygems.org"

gem 'actionmailer', '~> 3.0'
gemspec :path => '../'
4 changes: 4 additions & 0 deletions gemfiles/actionmailer.3.1.x.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source "http://rubygems.org"

gem 'actionmailer', '~> 3.1'
gemspec :path => '../'
4 changes: 4 additions & 0 deletions gemfiles/actionmailer.3.2.x.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source "http://rubygems.org"

gem 'actionmailer', '~> 3.2'
gemspec :path => '../'
4 changes: 4 additions & 0 deletions gemfiles/actionmailer.4.0.x.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source "http://rubygems.org"

gem 'actionmailer', '~> 4.0'
gemspec :path => '../'
4 changes: 4 additions & 0 deletions gemfiles/actionmailer.4.1.x.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source "http://rubygems.org"

gem 'actionmailer', '~> 4.1'
gemspec :path => '../'
3 changes: 3 additions & 0 deletions gemfiles/gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source "http://rubygems.org"

gemspec :path => '../'
6 changes: 6 additions & 0 deletions lib/mandriller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'mandriller/version'
require 'mandriller/errors'
require 'mandriller/base'

module Mandriller
end
102 changes: 102 additions & 0 deletions lib/mandriller/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
require 'action_mailer'
require_relative 'settings_methods'

class Mandriller::Base < ActionMailer::Base
include Mandriller::SettingsMethods

BOOLEAN_SETTINGS = {
autotext: 'X-MC-Autotext',
autohtml: 'X-MC-AutoHtml',
url_strip_qs: 'X-MC-URLStripQS',
preserve_recipients: 'X-MC-PreserveRecipients',
inline_css: 'X-MC-InlineCSS',
google_analytics_campaign: 'X-MC-GoogleAnalyticsCampaign',
view_content_link: 'X-MC-ViewContentLink',
import: 'X-MC-Important',
}
STRING_SETTINGS = {
tracking_domain: 'X-MC-TrackingDomain',
signing_domain: 'X-MC-SigningDomain',
subaccount: 'X-MC-Subaccount',
bcc_address: 'X-MC-BccAddress',
ip_pool: 'X-MC-IpPool',
return_path_domain: 'X-MC-ReturnPathDomain',
}
JSON_SETTINGS = {
metadata: 'X-MC-Metadata',
merge_vars: 'X-MC-MergeVars',
}
define_settings_methods BOOLEAN_SETTINGS.keys, default: true
define_settings_methods STRING_SETTINGS.keys
define_settings_methods JSON_SETTINGS.keys
define_settings_methods :open_track, default: true
define_settings_methods :click_track, default: 'clicks'
define_settings_methods :send_at

class_attribute :mandrill_template, :mandrill_google_analytics

class << self
def set_template(template_name, block_name)
self.mandrill_template = [template_name, block_name]
end

def set_google_analytics(*domains)
self.mandrill_google_analytics = domains.flatten
end
end

def set_template(template_name, block_name = nil)
@mandrill_template = [template_name, block_name].compact
end

def set_google_analytics(*domains)
@mandrill_google_analytics = domains.flatten
end

def mail(*args)
m = super(*args)

tracks = []
tracks << ((@mandrill_open_track.nil? ? self.mandrill_open_track : @mandrill_open_track) ? 'opens' : nil)
tracks << (@mandrill_click_track.nil? ? self.mandrill_click_track : @mandrill_click_track)
tracks = tracks.compact.map(&:to_s)
unless tracks.empty?
tracks.each do |track|
validate_values!(track, %w(opens clicks_all clicks clicks_htmlonly clicks_textonly))
end
self.headers['X-MC-Track'] = tracks.join(',')
end

v = get_mandrill_setting("template")
self.headers['X-MC-Template'] = v.join('|') unless v.nil?

v = get_mandrill_setting("google_analytics")
self.headers['X-MC-GoogleAnalytics'] = v.join(',') unless v.nil?

v = get_mandrill_setting("send_at")
self.headers['X-MC-SendAt'] = v.to_time.utc.strftime('%Y-%m-%d %H:%M:%S') unless v.nil?

BOOLEAN_SETTINGS.each do |key, header_name|
v = get_mandrill_setting(key)
self.headers[header_name] = v ? 'true' : 'false' unless v.nil?
end

STRING_SETTINGS.each do |key, header_name|
v = get_mandrill_setting(key)
self.headers[header_name] = v.to_s unless v.nil?
end

JSON_SETTINGS.each do |key, header_name|
v = get_mandrill_setting(key)
self.headers[header_name] = v.to_json unless v.nil?
end

m
end

private

def validate_values!(value, valid_values)
raise Mandriller::InvalidHeaderValue, "#{value} is not included in #{valid_values.join(', ')}" unless valid_values.include?(value)
end
end
4 changes: 4 additions & 0 deletions lib/mandriller/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Mandriller
class Exception < ::Exception; end;
class InvalidHeaderValue < Exception; end;
end
37 changes: 37 additions & 0 deletions lib/mandriller/settings_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Mandriller
module SettingsMethods
def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods
def define_settings_methods(*keys)
options = keys[-1].is_a?(Hash) ? keys.pop : {}
if default = options[:default]
arg_s = "v = #{default.inspect}"
else
arg_s = "v"
end

keys.flatten.each do |key|
class_eval <<-EOS
class_attribute :mandrill_#{key}
def self.set_#{key}(#{arg_s})
self.mandrill_#{key} = v
end
private_class_method :set_#{key}
def set_#{key}(#{arg_s})
@mandrill_#{key} = v
end
private :set_#{key}
EOS
end
end
end

def get_mandrill_setting(key)
instance_variable_defined?("@mandrill_#{key}") ? instance_variable_get("@mandrill_#{key}") : __send__("mandrill_#{key}")
end
private :get_mandrill_setting
end
end
3 changes: 3 additions & 0 deletions lib/mandriller/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Mandriller
VERSION = '0.0.2'
end
24 changes: 24 additions & 0 deletions mandriller.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require File.expand_path('../lib/mandriller/version', __FILE__)

Gem::Specification.new do |gem|
gem.name = "mandriller"
gem.version = Mandriller::VERSION
gem.platform = Gem::Platform::RUBY
gem.authors = ["Daisuke Taniwaki"]
gem.email = ["[email protected]"]
gem.homepage = "https://github.com/dtaniwaki/mandriller"
gem.summary = "Mandrill SMTP API integration for ActionMailer"
gem.description = "Mandrill SMTP API integration for ActionMailer"
gem.license = "MIT"

gem.files = `git ls-files`.split("\n")
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
gem.require_paths = ['lib']

gem.add_dependency "actionmailer", ">= 4.1"

gem.add_development_dependency "rake"
gem.add_development_dependency "rspec", ">= 3.0"
gem.add_development_dependency "coveralls"
end
Loading

0 comments on commit 9f4b268

Please sign in to comment.