banana_peels 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +30 -11
- data/lib/banana_peels/api.rb +11 -0
- data/lib/banana_peels/campaign.rb +42 -4
- data/lib/banana_peels/mailer_helper.rb +9 -0
- data/lib/banana_peels/version.rb +1 -1
- data/lib/banana_peels.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e808ac0025039ccf838899d253d539202a65d774
|
4
|
+
data.tar.gz: f5179fcc94851d3a16ba944e5e02ce7212dc98ee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4b9980108bd56b187debbe09d3eb537226f88bcf353b024aa365725cf3a74d6a46747d9ba54d8b1f27c3ae5b80b23bed13c024839ca993239ef45e01a595e44c
|
7
|
+
data.tar.gz: 9590126f26b371232de2d9c63cbaeae028b0fe873303d7207e2872fcd57554b49ccfd4b52e8d4243aea86711968341435aeb9ff2b15c6b0bd7f74c3d26ac93ef
|
data/README.md
CHANGED
@@ -26,11 +26,11 @@ app/controllers/campaigns_controller.rb:
|
|
26
26
|
class CampaignsController < ApplicationController
|
27
27
|
|
28
28
|
def index
|
29
|
-
@campaigns =
|
30
|
-
@env_vars = {}
|
31
|
-
by_id = @campaigns.index_by
|
29
|
+
@campaigns = BananaPeels.campaigns(ENV['MAILCHIMP_API_KEY'])
|
30
|
+
@env_vars = Hash.new{|h,k| h[k] = [] }
|
31
|
+
by_id = @campaigns.index_by(&:id)
|
32
32
|
ENV.each do |k,v|
|
33
|
-
@env_vars[v]
|
33
|
+
@env_vars[v].push(k) if by_id[v]
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
@@ -38,21 +38,39 @@ end
|
|
38
38
|
|
39
39
|
```
|
40
40
|
|
41
|
-
app/views/campaigns/index.html.slim:
|
41
|
+
app/views/campaigns/index.html.slim (http://slim-lang.com/, similar to haml):
|
42
42
|
```slim
|
43
43
|
h1 MailChimp Campaigns
|
44
44
|
table
|
45
45
|
thead
|
46
46
|
tr
|
47
47
|
th Campaign Title
|
48
|
-
th
|
49
|
-
|
48
|
+
th MailChimp ID
|
49
|
+
td
|
50
|
+
strong ENV Keys
|
51
|
+
| | Merge Tags
|
50
52
|
tbody
|
51
53
|
- @campaigns.each do |c|
|
52
54
|
tr
|
53
|
-
|
54
|
-
td = c
|
55
|
-
td
|
55
|
+
th = c.title
|
56
|
+
td = c.id
|
57
|
+
td
|
58
|
+
strong = @env_vars[c.id].join(', ')
|
59
|
+
| |
|
60
|
+
= c.merge_tags_in_content.keys.join(', ')
|
61
|
+
tr
|
62
|
+
td rowspan="4"
|
63
|
+
th Subject
|
64
|
+
td = c.mailchimp_meta['subject']
|
65
|
+
tr
|
66
|
+
th From
|
67
|
+
td = c.from
|
68
|
+
tr
|
69
|
+
th Text Content
|
70
|
+
td = CGI.escapeHTML(c.text_content_unmerged).gsub("\n","\n<br>").html_safe
|
71
|
+
tr
|
72
|
+
th HTML Content
|
73
|
+
td = c.html_content_unmerged
|
56
74
|
```
|
57
75
|
|
58
76
|
### Sending transactional emails using a MailChimp Campaign as a template
|
@@ -60,6 +78,7 @@ table
|
|
60
78
|
app/mailers/user_mailer.rb:
|
61
79
|
```ruby
|
62
80
|
class UserMailer < ActionMailer::Base
|
81
|
+
include BananaPeels::MailerHelper
|
63
82
|
|
64
83
|
def reset_password_instructions(record, token, opts={})
|
65
84
|
user = User.find(record.id)
|
@@ -71,7 +90,7 @@ class UserMailer < ActionMailer::Base
|
|
71
90
|
'LAST_NAME' => user.last_name,
|
72
91
|
'RESET_PASSWORD_URL' => edit_user_password_url(reset_password_token: token),
|
73
92
|
}
|
74
|
-
|
93
|
+
banana_mail(ENV['RESET_PASSWORD_CAMPAIGN_ID'], mail_options, merge_tags, ENV['MAILCHIMP_API_KEY'])
|
75
94
|
end
|
76
95
|
|
77
96
|
end
|
data/lib/banana_peels/api.rb
CHANGED
@@ -7,6 +7,17 @@ module BananaPeels
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def campaigns(api_key)
|
10
|
+
meta_list = campaigns_list(api_key)
|
11
|
+
campaigns = []
|
12
|
+
meta_list.each do |c_meta|
|
13
|
+
c = campaign(c_meta['id'], nil, api_key)
|
14
|
+
c.mailchimp_meta = c_meta
|
15
|
+
campaigns.push(c)
|
16
|
+
end
|
17
|
+
campaigns
|
18
|
+
end
|
19
|
+
|
20
|
+
def campaigns_list(api_key)
|
10
21
|
api_data = api(api_key).campaigns.list([], 0, 1000) # TODO: presumes <= 1000 campaigns
|
11
22
|
api_data['data']
|
12
23
|
end
|
@@ -21,6 +21,9 @@ class BananaPeels::Campaign
|
|
21
21
|
# TODO: Cache campaigns from mailchimp.
|
22
22
|
@mailchimp_meta ||= @api.campaigns.list({ campaign_id: @campaign_id })['data'].first
|
23
23
|
end
|
24
|
+
def mailchimp_meta=(mailchimp_meta)
|
25
|
+
@mailchimp_meta = mailchimp_meta
|
26
|
+
end
|
24
27
|
|
25
28
|
def mailchimp_content
|
26
29
|
# TODO: Cache.
|
@@ -34,6 +37,14 @@ class BananaPeels::Campaign
|
|
34
37
|
}
|
35
38
|
end
|
36
39
|
|
40
|
+
def id
|
41
|
+
mailchimp_meta['id']
|
42
|
+
end
|
43
|
+
|
44
|
+
def title
|
45
|
+
mailchimp_meta['title']
|
46
|
+
end
|
47
|
+
|
37
48
|
def from
|
38
49
|
BananaPeels.email_with_name(from_name, from_email)
|
39
50
|
end
|
@@ -54,22 +65,49 @@ class BananaPeels::Campaign
|
|
54
65
|
replace_merge_tags(mailchimp_meta['to_name'])
|
55
66
|
end
|
56
67
|
|
57
|
-
def
|
68
|
+
def merge_tags_in_content
|
69
|
+
merge_tags = Hash.new{|h,k| h[k] = [] }
|
70
|
+
{
|
71
|
+
to: mailchimp_meta['to_name'],
|
72
|
+
subject: mailchimp_meta['subject'],
|
73
|
+
html: html_content_unmerged,
|
74
|
+
text: text_content_unmerged,
|
75
|
+
}.each do |location, content|
|
76
|
+
find_merge_tags_in(content).each do |tag|
|
77
|
+
merge_tags[tag].push(location)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
merge_tags
|
81
|
+
end
|
82
|
+
|
83
|
+
def find_merge_tags_in(string)
|
84
|
+
string.scan(/\*\|(\w+)\|\*/).flatten.uniq
|
85
|
+
end
|
86
|
+
|
87
|
+
def text_content_unmerged
|
58
88
|
content = mailchimp_content['text'].to_s
|
59
89
|
content = content.sub(/=*\s*Unsubscribe\s\*\|HTML:EMAIL\|\*.*\z/m,'') # Auto-replaced if deleted.
|
60
90
|
content = content.sub(/=*\s*This\semail\swas\ssent\sto\s\*\|EMAIL\|\*.*\z/m,'') # When generated from HTML version.
|
61
91
|
content = content.sub(/=*\s*[^\n]*\*\|UNSUB\|\*.*\z/m,'') # Original generation.
|
62
|
-
|
92
|
+
content
|
63
93
|
end
|
64
94
|
|
65
|
-
def
|
95
|
+
def text_content
|
96
|
+
replace_merge_tags(text_content_unmerged)
|
97
|
+
end
|
98
|
+
|
99
|
+
def html_content_unmerged
|
66
100
|
content = mailchimp_content['html'].to_s
|
67
101
|
content = content.sub(/(.*)<center>.*?canspamBarWrapper.*?<\/center>/m,'\1')
|
102
|
+
content
|
103
|
+
end
|
104
|
+
|
105
|
+
def html_content
|
68
106
|
merge_tags = @merge_tags.dup
|
69
107
|
merge_tags.each do |k,v|
|
70
108
|
merge_tags[k] = CGI.escapeHTML(v.to_s).gsub("\n","\n<br>")
|
71
109
|
end
|
72
|
-
replace_merge_tags(
|
110
|
+
replace_merge_tags(html_content_unmerged, merge_tags)
|
73
111
|
end
|
74
112
|
|
75
113
|
def replace_merge_tags(string, merge_tags=nil)
|
data/lib/banana_peels/version.rb
CHANGED
data/lib/banana_peels.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: banana_peels
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steven Bull
|
@@ -56,6 +56,7 @@ files:
|
|
56
56
|
- lib/banana_peels.rb
|
57
57
|
- lib/banana_peels/api.rb
|
58
58
|
- lib/banana_peels/campaign.rb
|
59
|
+
- lib/banana_peels/mailer_helper.rb
|
59
60
|
- lib/banana_peels/version.rb
|
60
61
|
homepage: https://github.com/sbull/banana_peels
|
61
62
|
licenses:
|