netguru-hominid 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.textile ADDED
@@ -0,0 +1,71 @@
1
+ h1. Hominid
2
+
3
+ Hominid is a GemPlugin wrapper to the "Mailchimp API":http://www.mailchimp.com/api/1.2/.
4
+
5
+ h2. Installation
6
+
7
+ There are a few options for installing Hominid.
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", :lib => 'hominid', :source => "http://gems.github.com"</code></pre>
20
+
21
+ h2. Configuration
22
+
23
+ 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. You will need to create a Mailchimp account and put your API key (available at "http://admin.mailchimp.com/account/api/":http://admin.mailchimp.com/account/api/) into the configuration file.
24
+
25
+ h2. Example
26
+
27
+ To interact with the Mailchimp API, simply create a new Hominid object:
28
+
29
+ <pre><code>@hominid = Hominid.new</code></pre>
30
+
31
+ 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:
32
+
33
+ <pre><code>
34
+ def find_list_id(list_name)
35
+ mailing_lists = @hominid.lists
36
+ unless mailing_lists.nil?
37
+ @list_id = mailing_lists.find {|list| list["name"] == list_name}["id"]
38
+ end
39
+ end
40
+ </code></pre>
41
+
42
+ For example, to subscribe someone to a mailing list at Mailchimp:
43
+
44
+ <pre><code>@hominid.subscribe(@list_id, "email@example.com", {:FNAME => 'Bob', :LNAME => 'Smith'}, 'html')</code></pre>
45
+
46
+ To unsubscribe someone:
47
+
48
+ <pre><code>@hominid.unsubscribe(@list_id, "email@example.com")</code></pre>
49
+
50
+ To update a list member:
51
+
52
+ <pre><code>@hominid.subscribe(@list_id, "email@example.com", {:FNAME => 'Robert', :EMAIL => 'another@example.com'}, 'html', true)</code></pre>
53
+
54
+ _or_
55
+
56
+ <pre><code>@hominid.update_member(@list_id, "email@example.com", {:FNAME => 'Robert', :EMAIL => 'another@example.com'})</code></pre>
57
+
58
+ Campaign methods are also supported. You can get all the campaigns for a particular list by:
59
+
60
+ <pre><code>@hominid.campaigns(@list_id)</code></pre>
61
+
62
+ Leave the @@list_id@ out and it will return all the campaigns for your Mailchimp account.
63
+
64
+
65
+ h2. Other Stuff
66
+
67
+ 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.
68
+
69
+ 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!
70
+
71
+ Copyright (c) 2009 Brian Getting, released under the MIT license.
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 1
3
+ :minor: 1
4
+ :patch: 2
data/lib/hominid.rb ADDED
@@ -0,0 +1,327 @@
1
+ require 'xmlrpc/client'
2
+
3
+ class Hominid
4
+
5
+ # MailChimp API Documentation: http://www.mailchimp.com/api/1.2/
6
+ MAILCHIMP_API = "http://api.mailchimp.com/1.2/"
7
+
8
+ def initialize
9
+ load_monkey_brains
10
+ @chimpApi ||= XMLRPC::Client.new2(MAILCHIMP_API)
11
+ return self
12
+ end
13
+
14
+ def load_monkey_brains
15
+ config = YAML.load(File.open("#{RAILS_ROOT}/config/hominid.yml"))[RAILS_ENV].symbolize_keys
16
+ @chimpUsername = config[:username].to_s
17
+ @chimpPassword = config[:password].to_s
18
+ @api_key = config[:api_key]
19
+ @send_goodbye = config[:send_goodbye]
20
+ @send_notify = config[:send_notify]
21
+ @double_opt = config[:double_opt]
22
+ end
23
+
24
+ ## Security related methods
25
+
26
+ def add_api_key
27
+ begin
28
+ @chimpApi ||= XMLRPC::Client.new2(MAILCHIMP_API)
29
+ @chimpApi.call("apikeyAdd", @chimpUsername, @chimpPassword, @api_key)
30
+ rescue
31
+ false
32
+ end
33
+ end
34
+
35
+ def expire_api_key
36
+ begin
37
+ @chimpApi ||= XMLRPC::Client.new2(MAILCHIMP_API)
38
+ @chimpApi.call("apikeyExpire", @chimpUsername, @chimpPassword, @api_key)
39
+ rescue
40
+ false
41
+ end
42
+ end
43
+
44
+ def api_keys(include_expired = false)
45
+ begin
46
+ @chimpApi ||= XMLRPC::Client.new2(MAILCHIMP_API)
47
+ @api_keys = @chimpApi.call("apikeys", @chimpUsername, @chimpPassword, @api_key, include_expired)
48
+ rescue
49
+ return nil
50
+ end
51
+ end
52
+
53
+ ## Campaign related methods
54
+
55
+ def campaign_content(campaign_id)
56
+ # Get the content of a campaign
57
+ begin
58
+ @content = @chimpApi.call("campaignContent", @api_key, campaign_id)
59
+ rescue
60
+ return nil
61
+ end
62
+ end
63
+
64
+ def campaigns(filters = {}, start = 0, limit = 50)
65
+ # Get the campaigns for this account
66
+ # API Version 1.2 requires that filters be sent as a hash
67
+ # Available options for the filters hash are:
68
+ #
69
+ # :campaign_id = (string) The ID of the campaign you wish to return.
70
+ # :list_id = (string) Show only campaigns with this list_id.
71
+ # :folder_id = (integer) Show only campaigns from this folder.
72
+ # :from_name = (string) Show only campaigns with this from_name.
73
+ # :from_email = (string) Show only campaigns with this from_email.
74
+ # :title = (string) Show only campaigns with this title.
75
+ # :subject = (string) Show only campaigns with this subject.
76
+ # :sedtime_start = (string) Show campaigns sent after YYYY-MM-DD HH:mm:ss.
77
+ # :sendtime_end = (string) Show campaigns sent before YYYY-MM-DD HH:mm:ss.
78
+ # :subject = (boolean) Filter by exact values, or search within content for filter values.
79
+ begin
80
+ @campaigns = @chimpApi.call("campaigns", @api_key, filters, start, limit)
81
+ rescue
82
+ return nil
83
+ end
84
+ end
85
+
86
+ def create_campaign(type = 'regular', options = {}, content = {}, segment_options = {}, type_opts = {})
87
+ # Create a new campaign
88
+ begin
89
+ @campaign = @chimpApi.call("campaignCreate", @api_key, type, options, content, segment_options, type_opts)
90
+ rescue
91
+ return nil
92
+ end
93
+ end
94
+
95
+ def delete_campaign(campaign_id)
96
+ # Delete a campaign
97
+ begin
98
+ @campaign = @chimpApi.call("campaignDelete", @api_key, campaign_id)
99
+ rescue
100
+ false
101
+ end
102
+ end
103
+
104
+ def replicate_campaign(campaign_id)
105
+ # Replicate a campaign (returns ID of new campaign)
106
+ begin
107
+ @campaign = @chimpApi.call("campaignReplicate", @api_key, campaign_id)
108
+ rescue
109
+ false
110
+ end
111
+ end
112
+
113
+ def schedule_campaign(campaign_id, time = "#{1.day.from_now}")
114
+ # Schedule a campaign
115
+ ## TODO: Add support for A/B Split scheduling
116
+ begin
117
+ @chimpApi.call("campaignSchedule", @api_key, campaign_id, time)
118
+ rescue
119
+ false
120
+ end
121
+ end
122
+
123
+ def send_now(campaign_id)
124
+ # Send a campaign
125
+ begin
126
+ @chimpApi.call("campaignSendNow", @api_key, campaign_id)
127
+ rescue
128
+ false
129
+ end
130
+ end
131
+
132
+ def send_test(campaign_id, emails = {})
133
+ # Send a test of a campaign
134
+ begin
135
+ @chimpApi.call("campaignSendTest", @api_key, campaign_id, emails)
136
+ rescue
137
+ false
138
+ end
139
+ end
140
+
141
+ def templates
142
+ # Get the templates
143
+ begin
144
+ @templates = @chimpApi.call("campaignTemplates", @api_key)
145
+ rescue
146
+ return nil
147
+ end
148
+ end
149
+
150
+ def update_campaign(campaign_id, name, value)
151
+ # Update a campaign
152
+ begin
153
+ @chimpApi.call("campaignUpdate", @api_key, campaign_id, name, value)
154
+ rescue
155
+ false
156
+ end
157
+ end
158
+
159
+ def unschedule_campaign(campaign_id)
160
+ # Unschedule a campaign
161
+ begin
162
+ @chimpApi.call("campaignUnschedule", @api_key, campaign_id)
163
+ rescue
164
+ false
165
+ end
166
+ end
167
+
168
+ ## Helper methods
169
+
170
+ def html_to_text(content)
171
+ # Convert HTML content to text
172
+ begin
173
+ @html_to_text = @chimpApi.call("generateText", @api_key, 'html', content)
174
+ rescue
175
+ return nil
176
+ end
177
+ end
178
+
179
+ def convert_css_to_inline(html, strip_css = false)
180
+ # Convert CSS styles to inline styles and (optionally) remove original styles
181
+ begin
182
+ @html_to_text = @chimpApi.call("inlineCss", @api_key, html, strip_css)
183
+ rescue
184
+ return nil
185
+ end
186
+ end
187
+
188
+ ## List related methods
189
+
190
+ def lists
191
+ # Get all of the lists for this mailchimp account
192
+ begin
193
+ @lists = @chimpApi.call("lists", @api_key)
194
+ rescue
195
+ return nil
196
+ end
197
+ end
198
+
199
+ def create_group(list_id, group)
200
+ # Add an interest group to a list
201
+ begin
202
+ @chimpApi.call("listInterestGroupAdd", @api_key, list_id, group)
203
+ rescue
204
+ false
205
+ end
206
+ end
207
+
208
+ def create_tag(list_id, tag, name, required = false)
209
+ # Add a merge tag to a list
210
+ begin
211
+ @chimpApi.call("listMergeVarAdd", @api_key, list_id, tag, name, required)
212
+ rescue
213
+ false
214
+ end
215
+ end
216
+
217
+ def delete_group(list_id, group)
218
+ # Delete an interest group for a list
219
+ begin
220
+ @chimpApi.call("listInterestGroupDel", @api_key, list_id, group)
221
+ rescue
222
+ false
223
+ end
224
+ end
225
+
226
+ def delete_tag(list_id, tag)
227
+ # Delete a merge tag and all its members
228
+ begin
229
+ @chimpApi.call("listMergeVarDel", @api_key, list_id, tag)
230
+ rescue
231
+ false
232
+ end
233
+ end
234
+
235
+ def groups(list_id)
236
+ # Get the interest groups for a list
237
+ begin
238
+ @groups = @chimpApi.call("listInterestGroups", @api_key, list_id)
239
+ rescue
240
+ return nil
241
+ end
242
+ end
243
+
244
+ def member(list_id, email)
245
+ # Get a member of a list
246
+ begin
247
+ @member = @chimpApi.call("listMemberInfo", @api_key, list_id, email)
248
+ rescue
249
+ return nil
250
+ end
251
+ end
252
+
253
+ def members(list_id, status = "subscribed", since = "2000-01-01 00:00:00", start = 0, limit = 100)
254
+ # Get members of a list based on status
255
+ # Select members based on one of the following statuses:
256
+ # 'subscribed'
257
+ # 'unsubscribed'
258
+ # 'cleaned'
259
+ # 'updated'
260
+ #
261
+ # Select members that have updated their status or profile by providing
262
+ # a "since" date in the format of YYYY-MM-DD HH:MM:SS
263
+ #
264
+ begin
265
+ @members = @chimpApi.call("listMembers", @api_key, list_id, status, since, start, limit)
266
+ rescue
267
+ return nil
268
+ end
269
+ end
270
+
271
+ def merge_tags(list_id)
272
+ # Get the merge tags for a list
273
+ begin
274
+ @merge_tags = @chimpApi.call("listMergeVars", @api_key, list_id)
275
+ rescue
276
+ return nil
277
+ end
278
+ end
279
+
280
+ def subscribe(list_id, email, user_info = {}, email_type = "html", update_existing = true, replace_interests = true, double_opt_in = nil)
281
+ # Subscribe a member
282
+ begin
283
+ @chimpApi.call("listSubscribe", @api_key, list_id, email, user_info, email_type, double_opt_in || @double_opt, update_existing, replace_interests)
284
+ rescue
285
+ false
286
+ end
287
+ end
288
+
289
+ def subscribe_many(list_id, subscribers)
290
+ # Subscribe a batch of members
291
+ # subscribers = {:EMAIL => 'example@email.com', :EMAIL_TYPE => 'html'}
292
+ begin
293
+ @chimpApi.call("listBatchSubscribe", @api_key, list_id, subscribers, @double_opt, true)
294
+ rescue
295
+ false
296
+ end
297
+ end
298
+
299
+ def unsubscribe(list_id, current_email)
300
+ # Unsubscribe a list member
301
+ begin
302
+ @chimpApi.call("listUnsubscribe", @api_key, list_id, current_email, true, @send_goodbye, @send_notify)
303
+ rescue
304
+ false
305
+ end
306
+ end
307
+
308
+ def unsubscribe_many(list_id, emails)
309
+ # Unsubscribe an array of email addresses
310
+ # emails = ['first@email.com', 'second@email.com']
311
+ begin
312
+ @chimpApi.call("listBatchUnsubscribe", @api_key, list_id, emails, true, @send_goodbye, @send_notify)
313
+ rescue
314
+ false
315
+ end
316
+ end
317
+
318
+ def update_member(list_id, current_email, user_info = {}, email_type = "")
319
+ # Update a member of this list
320
+ begin
321
+ @chimpApi.call("listUpdateMember", @api_key, list_id, current_email, user_info, email_type, true)
322
+ rescue
323
+ false
324
+ end
325
+ end
326
+
327
+ 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,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: netguru-hominid
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Brian Getting
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-28 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
+ - VERSION.yml
27
+ - lib/hominid.rb
28
+ - test/hominid_test.rb
29
+ - test/test_helper.rb
30
+ has_rdoc: true
31
+ homepage: http://github.com/bgetting/hominid
32
+ post_install_message:
33
+ rdoc_options:
34
+ - --inline-source
35
+ - --charset=UTF-8
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.2.0
54
+ signing_key:
55
+ specification_version: 2
56
+ summary: Hominid is a Rails GemPlugin for interacting with the Mailchimp API
57
+ test_files: []
58
+