bgetting-hominid 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile ADDED
@@ -0,0 +1,63 @@
1
+ h1. Hominid
2
+
3
+ Hominid is a GemPlugin wrapper to the Mailchimp API.
4
+
5
+ h2. Installation
6
+
7
+ There are a few options for installing Hominid. Please note that Hominid expects to find a configuration file at @/config/hominid.yml@. If you are using Hominid as a GemPlugin, you will need to be sure and create this file. If you are using Hominid as a normal Rails plugin, this file will be created automatically when the plugin is installed.
8
+
9
+ Install as a Rails plugin:
10
+
11
+ <pre><code>script/plugin install git://github.com/bgetting/hominid.git</code></pre>
12
+
13
+ Clone from the Github repository:
14
+
15
+ <pre><code>git clone git://github.com/bgetting/hominid.git</code></pre>
16
+
17
+ Use the GemPlugin:
18
+
19
+ <pre><code>config.gem "bgetting-hominid", :version => '~> 0.0.0', :lib => 'hominid', :source => "http://gems.github.com"</code></pre>
20
+
21
+ h2. Example
22
+
23
+ To interact with the Mailchimp API, simply create a new Hominid object:
24
+
25
+ <pre><code>@hominid = Hominid.new</code></pre>
26
+
27
+ First, locate the mailing list that you are going to be working with. If you know the mailing list ID, then you can use that directly. Or you can find a particular list by name using:
28
+
29
+ <pre><code>
30
+ def find_list_id(list_name)
31
+ mailing_lists = @hominid.lists
32
+ unless mailing_lists.nil?
33
+ @list_id = mailing_lists.find {|list| list["name"] == list_name}["id"]
34
+ end
35
+ end
36
+ </code></pre>
37
+
38
+ For example, to subscribe someone to a mailing list at Mailchimp:
39
+
40
+ <pre><code>@hominid.subscribe(@list_id, "email@example.com", {:FNAME => 'Bob', :LNAME => 'Smith'}, 'html')</code></pre>
41
+
42
+ To unsubscribe someone:
43
+
44
+ <pre><code>@hominid.unsubscribe(@list_id, "email@example.com")</code></pre>
45
+
46
+ To update a list member:
47
+
48
+ <pre><code>@hominid.update_member(@list_id, "email@example.com", {:FNAME => 'Robert', :EMAIL => 'another@example.com'})</code></pre>
49
+
50
+ Campaign methods are also supported. You can get all the campaigns for a particular list by:
51
+
52
+ <pre><code>@hominid.campaigns(@list_id)</code></pre>
53
+
54
+ Leave the @@list_id@ out and it will return all the campaigns for your Mailchimp account.
55
+
56
+
57
+ h2. Other Stuff
58
+
59
+ For the most part, this whole thing was an attempt to optimize the acts_as_mailchimp plugin, and incorporates all the great work from "C.G. Brown":http://www.projectlocker.com/ and "Kelly Mahan":http://digimedia.com/, as well as "Matthew Carlson":http://mandarinsoda.com/, whose plugin inspired nearly all of this work.
60
+
61
+ The Acts_As_Mailchimp plugin will be updated to utilize Hominid, so that there is still a way to add simple "acts_as" methods to a model. However, since Hominid is such a thin wrapper I encourage people to start experimenting with using the other methods to integrate newsletter marketing natively into their apps. Mailchimp put a lot of work into their API, so take advantage of it and make something cool!
62
+
63
+ Copyright (c) 2009 Brian Getting, released under the MIT license.
data/lib/hominid.rb ADDED
@@ -0,0 +1,317 @@
1
+ require 'xmlrpc/client'
2
+
3
+ class Hominid
4
+
5
+ # MailChimp API Documentation: http://www.mailchimp.com/api/1.1/
6
+
7
+ def initialize(autologin = true)
8
+ load_monkey_brains
9
+ login if autologin
10
+ return self
11
+ end
12
+
13
+ def load_monkey_brains
14
+ config = YAML.load(File.open("#{RAILS_ROOT}/config/hominid.yml"))[RAILS_ENV].symbolize_keys
15
+ @chimpUsername = config[:username].to_s
16
+ @chimpPassword = config[:password].to_s
17
+ @send_goodbye = config[:send_goodbye]
18
+ @send_notify = config[:send_notify]
19
+ @double_opt = config[:double_opt]
20
+ end
21
+
22
+ def login
23
+ begin
24
+ @chimpApi ||= XMLRPC::Client.new2("http://api.mailchimp.com/1.1/")
25
+ @api_key = @chimpApi.call("login", @chimpUsername, @chimpPassword)
26
+ rescue
27
+ puts "***** Mail Chimp Error *****"
28
+ puts "Connection: #{@chimpApi}"
29
+ puts "Login: #{@chimpUsername}"
30
+ puts "Password: #{@chimpPassword}"
31
+ end
32
+ end
33
+
34
+ def add_api_key(key)
35
+ begin
36
+ @chimpApi ||= XMLRPC::Client.new2("http://api.mailchimp.com/1.1/")
37
+ @chimpApi.call("apikeyAdd", @chimpUsername, @chimpPassword, key)
38
+ rescue
39
+ false
40
+ end
41
+ end
42
+
43
+ def expire_api_key(key)
44
+ begin
45
+ @chimpApi ||= XMLRPC::Client.new2("http://api.mailchimp.com/1.1/")
46
+ @chimpApi.call("apikeyExpire", @chimpUsername, @chimpPassword, key)
47
+ rescue
48
+ false
49
+ end
50
+ end
51
+
52
+ def api_keys(include_expired = false)
53
+ begin
54
+ @chimpApi ||= XMLRPC::Client.new2("http://api.mailchimp.com/1.1/")
55
+ @api_keys = @chimpApi.call("apikeys", @chimpUsername, @chimpPassword, @api_key, include_expired)
56
+ rescue
57
+ return nil
58
+ end
59
+ end
60
+
61
+ ## Campaign related methods
62
+
63
+ def campaign_content(campaign_id)
64
+ # Get the content of a campaign
65
+ begin
66
+ @content = @chimpApi.call("campaignContent", @api_key, campaign_id)
67
+ rescue
68
+ return nil
69
+ end
70
+ end
71
+
72
+ def campaigns(list_id = nil)
73
+ # Get the campaigns for this account (optionally for a specific list)
74
+ begin
75
+ if list_id
76
+ @campaigns = @chimpApi.call("campaigns", @api_key, list_id)
77
+ else
78
+ @campaigns = @chimpApi.call("campaigns", @api_key)
79
+ end
80
+ rescue
81
+ return nil
82
+ end
83
+ end
84
+
85
+ def create_campaign(type = 'regular', options = {}, content = {}, segment_options = {}, type_opts = {})
86
+ # Create a new campaign
87
+ begin
88
+ @campaign = @chimpApi.call("campaignCreate", @api_key, type, options, content, segment_options, type_opts)
89
+ rescue
90
+ return nil
91
+ end
92
+ end
93
+
94
+ def delete_campaign(campaign_id)
95
+ # Delete a campaign
96
+ begin
97
+ @campaign = @chimpApi.call("campaignDelete", @api_key, campaign_id)
98
+ rescue
99
+ false
100
+ end
101
+ end
102
+
103
+ def replicate_campaign(campaign_id)
104
+ # Replicate a campaign (returns ID of new campaign)
105
+ begin
106
+ @campaign = @chimpApi.call("campaignReplicate", @api_key, campaign_id)
107
+ rescue
108
+ false
109
+ end
110
+ end
111
+
112
+ def schedule_campaign(campaign_id, time = "#{1.day.from_now}")
113
+ # Schedule a campaign
114
+ ## TODO: Add support for A/B Split scheduling
115
+ begin
116
+ @chimpApi.call("campaignSchedule", @api_key, campaign_id, time)
117
+ rescue
118
+ false
119
+ end
120
+ end
121
+
122
+ def send_now(campaign_id)
123
+ # Send a campaign
124
+ begin
125
+ @chimpApi.call("campaignSendNow", @api_key, campaign_id)
126
+ rescue
127
+ false
128
+ end
129
+ end
130
+
131
+ def send_test(campaign_id, emails = {})
132
+ # Send a test of a campaign
133
+ begin
134
+ @chimpApi.call("campaignSendTest", @api_key, campaign_id, emails)
135
+ rescue
136
+ false
137
+ end
138
+ end
139
+
140
+ def templates
141
+ # Get the templates
142
+ begin
143
+ @templates = @chimpApi.call("campaignTemplates", @api_key)
144
+ rescue
145
+ return nil
146
+ end
147
+ end
148
+
149
+ def update_campaign(campaign_id, name, value)
150
+ # Update a campaign
151
+ begin
152
+ @chimpApi.call("campaignUpdate", @api_key, campaign_id, name, value)
153
+ rescue
154
+ false
155
+ end
156
+ end
157
+
158
+ def unschedule_campaign(campaign_id)
159
+ # Unschedule a campaign
160
+ begin
161
+ @chimpApi.call("campaignUnschedule", @api_key, campaign_id)
162
+ rescue
163
+ false
164
+ end
165
+ end
166
+
167
+ ## Helper methods
168
+
169
+ def html_to_text(content)
170
+ # Convert HTML content to text
171
+ begin
172
+ @html_to_text = @chimpApi.call("generateText", @api_key, 'html', content)
173
+ rescue
174
+ return nil
175
+ end
176
+ end
177
+
178
+ def convert_css_to_inline(html, strip_css = false)
179
+ # Convert CSS styles to inline styles and (optionally) remove original styles
180
+ begin
181
+ @html_to_text = @chimpApi.call("inlineCss", @api_key, html, strip_css)
182
+ rescue
183
+ return nil
184
+ end
185
+ end
186
+
187
+ ## List related methods
188
+
189
+ def lists
190
+ # Get all of the lists for this mailchimp account
191
+ begin
192
+ @lists = @chimpApi.call("lists", @api_key)
193
+ rescue
194
+ return nil
195
+ end
196
+ end
197
+
198
+ def create_group(list_id, group)
199
+ # Add an interest group to a list
200
+ begin
201
+ @chimpApi.call("listInterestGroupAdd", @api_key, group)
202
+ rescue
203
+ false
204
+ end
205
+ end
206
+
207
+ def create_tag(list_id, tag, name, required = false)
208
+ # Add a merge tag to a list
209
+ begin
210
+ @chimpApi.call("listMergeVarAdd", @api_key, tag, name, required)
211
+ rescue
212
+ false
213
+ end
214
+ end
215
+
216
+ def delete_group(list_id, group)
217
+ # Delete an interest group for a list
218
+ begin
219
+ @chimpApi.call("listInterestGroupDel", @api_key, group)
220
+ rescue
221
+ false
222
+ end
223
+ end
224
+
225
+ def delete_tag(list_id, tag)
226
+ # Delete a merge tag and all its members
227
+ begin
228
+ @chimpApi.call("listMergeVarDel", @api_key, tag)
229
+ rescue
230
+ false
231
+ end
232
+ end
233
+
234
+ def groups(list_id)
235
+ # Get the interest groups for a list
236
+ begin
237
+ @groups = @chimpApi.call("listInterestGroups", @api_key, list_id)
238
+ rescue
239
+ return nil
240
+ end
241
+ end
242
+
243
+ def member(list_id, email)
244
+ # Get a member of a list
245
+ begin
246
+ @member = @chimpApi.call("listMemberInfo", @api_key, list_id, email)
247
+ rescue
248
+ return nil
249
+ end
250
+ end
251
+
252
+ def members(list_id, status = "subscribed")
253
+ # Get members of a list based on status
254
+ begin
255
+ @members = @chimpApi.call("listMembers", @api_key, list_id, status)
256
+ rescue
257
+ return nil
258
+ end
259
+ end
260
+
261
+ def merge_tags(list_id)
262
+ # Get the merge tags for a list
263
+ begin
264
+ @merge_tags = @chimpApi.call("listMergeVars", @api_key, list_id)
265
+ rescue
266
+ return nil
267
+ end
268
+ end
269
+
270
+ def subscribe(list_id, email, user_info = {}, email_type = "html")
271
+ # Subscribe a member
272
+ begin
273
+ @chimpApi.call("listSubscribe", @api_key, list_id, email, user_info, email_type, @double_opt)
274
+ rescue
275
+ false
276
+ end
277
+ end
278
+
279
+ def subscribe_many(list_id, subscribers)
280
+ # Subscribe a batch of members
281
+ # subscribers = {:EMAIL => 'example@email.com', :EMAIL_TYPE => 'html'}
282
+ begin
283
+ @chimpApi.call("listBatchSubscribe", @api_key, list_id, subscribers, @double_opt, true)
284
+ rescue
285
+ false
286
+ end
287
+ end
288
+
289
+ def unsubscribe(list_id, current_email)
290
+ # Unsubscribe a list member
291
+ begin
292
+ @chimpApi.call("listUnsubscribe", @api_key, list_id, current_email, true, @send_goodbye, @send_notify)
293
+ rescue
294
+ false
295
+ end
296
+ end
297
+
298
+ def unsubscribe_many(list_id, emails)
299
+ # Unsubscribe an array of email addresses
300
+ # emails = ['first@email.com', 'second@email.com']
301
+ begin
302
+ @chimpApi.call("listBatchUnsubscribe", @api_key, list_id, emails, true, @send_goodbye, @send_notify)
303
+ rescue
304
+ false
305
+ end
306
+ end
307
+
308
+ def update_member(list_id, current_email, user_info = {}, email_type = "")
309
+ # Update a member of this list
310
+ begin
311
+ @chimpApi.call("listUpdateMember", @api_key, list_id, current_email, user_info, email_type, true)
312
+ rescue
313
+ false
314
+ end
315
+ end
316
+
317
+ end
@@ -0,0 +1,11 @@
1
+ require 'test_helper'
2
+
3
+ class HominidTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+
9
+ ## TODO: Write tests to verify each method
10
+
11
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bgetting-hominid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Getting
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-24 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Hominid is a Rails GemPlugin for interacting with the Mailchimp API
17
+ email: brian@terra-firma-design.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.textile
24
+ files:
25
+ - README.textile
26
+ - lib/hominid.rb
27
+ - test/hominid_test.rb
28
+ - test/test_helper.rb
29
+ has_rdoc: true
30
+ homepage: http://github.com/bgetting/hominid
31
+ post_install_message:
32
+ rdoc_options:
33
+ - --inline-source
34
+ - --charset=UTF-8
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.2.0
53
+ signing_key:
54
+ specification_version: 2
55
+ summary: Hominid is a Rails GemPlugin for interacting with the Mailchimp API
56
+ test_files: []
57
+