banana_peels 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1256b534deb0dc6e3c0d9b47361b3d6cfb42347c
4
+ data.tar.gz: 95efea6d6d804b85d7a384f89cf2bba48a2586e4
5
+ SHA512:
6
+ metadata.gz: 28b9e17db54a9722bff8c19beababc247683763c519677d03d24b8eb05b71db149c6f76a520966c5f70ed7d0bfa3740ff479db874d4a992325e80599093d09ac
7
+ data.tar.gz: 8176ca9afdf3bb786866d412cf7ab448cbf75bfcd4f8348feea7147a21b8f641dc4542fa811650ee5ede92e3ea5b2eff7471dcb8a71a81147a7fde7098b43dec
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in banana_peels.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Steven Bull
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # BananaPeels
2
+
3
+ Interface for using MailChimp as a template repository for transactional emails. MailChimp campaigns define email content, using merge tags as placeholders for injectable content pieces.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'banana_peels'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install banana_peels
18
+
19
+ ## Usage
20
+
21
+ ### Campaign listing with ENV info (Rails)
22
+
23
+ app/controllers/campaigns_controller.rb:
24
+
25
+ ```ruby
26
+ class CampaignsController < ApplicationController
27
+
28
+ def index
29
+ @campaigns = Chimp.campaigns(ENV['MAILCHIMP_API_KEY'])
30
+ @env_vars = {}
31
+ by_id = @campaigns.index_by{ |c| c['id'] }
32
+ ENV.each do |k,v|
33
+ @env_vars[v] = k if by_id[v]
34
+ end
35
+ end
36
+
37
+ end
38
+
39
+ ```
40
+
41
+ app/views/campaigns/index.html.slim:
42
+ ```slim
43
+ h1 MailChimp Campaigns
44
+ table
45
+ thead
46
+ tr
47
+ th Campaign Title
48
+ th Mailchimp ID
49
+ th ENV
50
+ tbody
51
+ - @campaigns.each do |c|
52
+ tr
53
+ td = c['title']
54
+ td = c['id']
55
+ td = @env_vars[c['id']]
56
+ ```
57
+
58
+ ### Sending transactional emails using a MailChimp Campaign as a template
59
+
60
+ app/mailers/user_mailer.rb:
61
+ ```ruby
62
+ class UserMailer < ActionMailer::Base
63
+
64
+ def reset_password_instructions(record, token, opts={})
65
+ user = User.find(record.id)
66
+ mail_options = {
67
+ to: BananaPeels.email_with_name("#{user.first_name} #{user.last_name}", user.email),
68
+ }
69
+ merge_tags = {
70
+ 'FIRST_NAME' => user.first_name,
71
+ 'LAST_NAME' => user.last_name,
72
+ 'RESET_PASSWORD_URL' => edit_user_password_url(reset_password_token: token),
73
+ }
74
+ BananaPeels.mail(self, ENV['RESET_PASSWORD_CAMPAIGN_ID'], mail_options, merge_tags, ENV['MAILCHIMP_API_KEY'])
75
+ end
76
+
77
+ end
78
+ ```
79
+
80
+ And then use the merge tags `*|FIRST_NAME|*`, `*|LAST_NAME|*`, `*|RESET_PASSWORD_URL|*` in your MailChimp Campaign.
81
+
82
+
83
+ ## Contributing
84
+
85
+ 1. Fork it
86
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
87
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
88
+ 4. Push to the branch (`git push origin my-new-feature`)
89
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'banana_peels/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "banana_peels"
8
+ spec.version = BananaPeels::VERSION
9
+ spec.authors = ["Steven Bull"]
10
+ spec.email = ["steven@thebulls.us"]
11
+ spec.description = "Interface for using MailChimp as a template repository for transactional emails. MailChimp campaigns define email content, using merge tags as placeholders for injectable content pieces."
12
+ spec.summary = "Interface for using MailChimp as a template repository for transactional emails."
13
+ spec.homepage = "https://github.com/sbull/banana_peels"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+
23
+ spec.add_runtime_dependency('mailchimp-api', '~> 2.0')
24
+ end
@@ -0,0 +1,5 @@
1
+ require 'mailchimp'
2
+
3
+ require 'banana_peels/version'
4
+ require 'banana_peels/api'
5
+ require 'banana_peels/campaign'
@@ -0,0 +1,37 @@
1
+ module BananaPeels
2
+
3
+ class << self
4
+
5
+ def api(api_key)
6
+ Mailchimp::API.new(api_key)
7
+ end
8
+
9
+ def campaigns(api_key)
10
+ api_data = api(api_key).campaigns.list([], 0, 1000) # TODO: presumes <= 1000 campaigns
11
+ api_data['data']
12
+ end
13
+
14
+ def campaign(campaign_id, merge_tags, api_key)
15
+ Campaign.new(campaign_id, merge_tags, api_key)
16
+ end
17
+
18
+ # Use this instead of #mail() in ActionMailer.
19
+ def mail(mailer, campaign_id, mail_options, merge_tags, api_key)
20
+ c = campaign(campaign_id, merge_tags, api_key)
21
+ mailer.mail(c.default_mail_options.merge(mail_options)) do |format|
22
+ format.text { mailer.render text: c.text_content }
23
+ format.html(content_transfer_encoding: 'quoted-printable') do
24
+ mailer.render text: [c.html_content].pack('M')
25
+ end
26
+ end
27
+ end
28
+
29
+ # Utility method.
30
+ def email_with_name(name, email)
31
+ name = name.gsub(/([\\"])/, '\\'=>'\\\\', '"'=>'\\"')
32
+ "\"#{name}\" <#{email}>"
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,91 @@
1
+ class BananaPeels::Campaign
2
+
3
+ class << self
4
+
5
+ def replace_merge_tags(string, merge_tags)
6
+ string.gsub(/\*\|(\w+)\|\*/) do
7
+ merge_tags[$1.upcase]
8
+ end
9
+ end
10
+
11
+ end
12
+
13
+ # merge_tags: optional, nil for default
14
+ def initialize(campaign_id, merge_tags, api_key)
15
+ @campaign_id = campaign_id
16
+ @merge_tags = normalize_merge_tags(merge_tags || {})
17
+ @api = BananaPeels.api(api_key)
18
+ end
19
+
20
+ def mailchimp_meta
21
+ # TODO: Cache campaigns from mailchimp.
22
+ @mailchimp_meta ||= @api.campaigns.list({ campaign_id: @campaign_id })['data'].first
23
+ end
24
+
25
+ def mailchimp_content
26
+ # TODO: Cache.
27
+ @mailchimp_content ||= @api.campaigns.content(@campaign_id, { view: 'raw' })
28
+ end
29
+
30
+ def default_mail_options
31
+ {
32
+ from: from,
33
+ subject: subject,
34
+ }
35
+ end
36
+
37
+ def from
38
+ BananaPeels.email_with_name(from_name, from_email)
39
+ end
40
+
41
+ def from_name
42
+ mailchimp_meta['from_name']
43
+ end
44
+
45
+ def from_email
46
+ mailchimp_meta['from_email']
47
+ end
48
+
49
+ def subject
50
+ replace_merge_tags(mailchimp_meta['subject'])
51
+ end
52
+
53
+ def to_name
54
+ replace_merge_tags(mailchimp_meta['to_name'])
55
+ end
56
+
57
+ def text_content
58
+ content = mailchimp_content['text'].to_s
59
+ content = content.sub(/=*\s*Unsubscribe\s\*\|HTML:EMAIL\|\*.*\z/m,'') # Auto-replaced if deleted.
60
+ content = content.sub(/=*\s*This\semail\swas\ssent\sto\s\*\|EMAIL\|\*.*\z/m,'') # When generated from HTML version.
61
+ content = content.sub(/=*\s*[^\n]*\*\|UNSUB\|\*.*\z/m,'') # Original generation.
62
+ replace_merge_tags(content)
63
+ end
64
+
65
+ def html_content
66
+ content = mailchimp_content['html'].to_s
67
+ content = content.sub(/(.*)<center>.*?canspamBarWrapper.*?<\/center>/m,'\1')
68
+ merge_tags = @merge_tags.dup
69
+ merge_tags.each do |k,v|
70
+ merge_tags[k] = CGI.escapeHTML(v.to_s).gsub("\n","\n<br>")
71
+ end
72
+ replace_merge_tags(content, merge_tags)
73
+ end
74
+
75
+ def replace_merge_tags(string, merge_tags=nil)
76
+ self.class.replace_merge_tags(string, merge_tags || @merge_tags)
77
+ end
78
+
79
+ def normalize_merge_tags(merge_tags)
80
+ normalized = {}
81
+ if merge_tags
82
+ merge_tags.each do |k,v|
83
+ normalized[k.to_s.upcase] = v
84
+ end
85
+ end
86
+ normalized['FNAME'] ||= normalized['FIRST_NAME']
87
+ normalized['LNAME'] ||= normalized['LAST_NAME']
88
+ normalized
89
+ end
90
+
91
+ end
@@ -0,0 +1,3 @@
1
+ module BananaPeels
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: banana_peels
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Steven Bull
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mailchimp-api
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ description: Interface for using MailChimp as a template repository for transactional
42
+ emails. MailChimp campaigns define email content, using merge tags as placeholders
43
+ for injectable content pieces.
44
+ email:
45
+ - steven@thebulls.us
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - .gitignore
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - banana_peels.gemspec
56
+ - lib/banana_peels.rb
57
+ - lib/banana_peels/api.rb
58
+ - lib/banana_peels/campaign.rb
59
+ - lib/banana_peels/version.rb
60
+ homepage: https://github.com/sbull/banana_peels
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.0.3
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Interface for using MailChimp as a template repository for transactional
84
+ emails.
85
+ test_files: []