bcaccinolo-mailchimp_api 0.0.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 ADDED
@@ -0,0 +1,6 @@
1
+ Mailchimp API
2
+ ============
3
+ Author: Benoit Caccinolo
4
+
5
+
6
+
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gemspec|
6
+ gemspec.name = "mailchimp_api"
7
+ gemspec.summary = "Summarize your gem"
8
+ gemspec.description = "Describe your gem"
9
+ gemspec.email = "benoit.caccinolo@gmail.com"
10
+ gemspec.homepage = "http://ruready.tumblr.com"
11
+ gemspec.description = "TODO"
12
+ gemspec.authors = ["Benoit Caccinolo"]
13
+ end
14
+ rescue LoadError
15
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
16
+ end
17
+
18
+
19
+
20
+
21
+ task :default do
22
+ puts "default task"
23
+ end
24
+
25
+ desc "Launch unit tests."
26
+ task :test do
27
+ require 'test/unit/ui/console/testrunner'
28
+ require 'test/test_mc'
29
+ Test::Unit::UI::Console::TestRunner.run(TestMailChimp)
30
+ end
31
+
32
+
33
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.3
data/lib/mailchimp.rb ADDED
@@ -0,0 +1,142 @@
1
+ # For the API 1.1
2
+ # http://www.mailchimp.com/api/1.1/
3
+ #
4
+ require "rubygems"
5
+ require 'net/http'
6
+ require 'uri'
7
+ require "json"
8
+ require 'yaml'
9
+
10
+ class MailChimp
11
+
12
+ attr_accessor :api
13
+
14
+ # tested
15
+ def login username, password
16
+ ret = call_server 'login', {:username => username, :password => password}
17
+ @api = ret.slice(1, ret.size-2) if ret.class == String
18
+ end
19
+
20
+ # tested
21
+ def ping
22
+ ret = call_server 'ping'
23
+ return ((ret.class == String) and !(ret =~ /Everything.*Chimpy/).nil?) ? true : false
24
+ end
25
+
26
+ # tested
27
+ def campaign_create list_id, from_email, from_name, subject, content
28
+ params = {:options => {:list_id => list_id, :subject => subject,
29
+ :from_name => from_name, :from_email => from_email},
30
+ :type => "regular", :content => {"html" => content} }
31
+ ret = call_server 'campaignCreate', params
32
+ if ret.class == String
33
+ ret = ret.slice(1, ret.size-2)
34
+ end
35
+ ret
36
+ end
37
+
38
+ # tested
39
+ def campaign_update cid, name, value
40
+ params = {:cid => cid, :name => name, :value => value}
41
+ ret = call_server 'campaignUpdate', params
42
+ ret
43
+ end
44
+
45
+ # tested
46
+ def campaign_delete cid
47
+ params = {:cid => cid}
48
+ ret = call_server 'campaignDelete', params
49
+ ret
50
+ end
51
+
52
+ # not tested
53
+ def campaign_send_now cid
54
+ params = {:cid => cid}
55
+ ret = call_server 'campaignSendNow', params
56
+ ret
57
+ end
58
+
59
+ # tested
60
+ def campaign_send_test cid, emails
61
+ params = {:cid => cid, :test_emails => emails}
62
+ ret = call_server 'campaignSendTest', params
63
+ ret
64
+ end
65
+
66
+ # not tested
67
+ def list_batch_subscribe list_id, batch_emails
68
+ params = {:id => list_id, :batch => batch_emails}
69
+ ret = call_server 'listBatchSubscribe', params
70
+ ret
71
+ end
72
+
73
+ # tested
74
+ def list_batch_unsubscribe list_id, batch_emails
75
+ params = {:id => list_id, :emails => batch_emails, :delete_member => true, :send_goodbye => false }
76
+ ret = call_server 'listBatchUnsubscribe', params
77
+ ret
78
+ end
79
+
80
+ # tested
81
+ def list_subscribe list_id, email
82
+ params = {:id => list_id, :email_address => email,
83
+ :merge_vars => {'FIRST' => '', 'LAST' => ''} , :double_optin => false}
84
+ ret = call_server 'listSubscribe', params
85
+ ret
86
+ end
87
+
88
+ # tested
89
+ def list_unsubscribe list_id, email
90
+ params = {:id => list_id, :email_address => email}
91
+ ret = call_server 'listUnsubscribe', params
92
+ ret
93
+ end
94
+
95
+ # tested
96
+ def lists
97
+ params = {}
98
+ ret = call_server 'lists', params
99
+ ret
100
+ end
101
+
102
+ # not tested
103
+ def list_members list_id
104
+ params = {:id => list_id}
105
+ ret = call_server 'listMembers', params
106
+ (ret.class == Hash) ? [] : ret
107
+ end
108
+
109
+ def call_server method , params={}
110
+ url = "api.mailchimp.com"
111
+ path = "/1.1/?output=json&method=#{method}"
112
+ params[:apikey] = @api unless method == "login"
113
+ data = gen_params_list(params)
114
+ #puts "http://#{url}#{path}&#{data}"
115
+ http = Net::HTTP.new(url)
116
+ result = http.post(path, data)
117
+ begin
118
+ json_res = JSON.parse result.body
119
+ rescue JSON::ParserError
120
+ json_res = result.body
121
+ end
122
+ json_res
123
+ end
124
+
125
+ def gen_params_list params
126
+ params.map do |key, value|
127
+ if value.class == Hash
128
+ value.map do |k, v|
129
+ "#{key}[#{k}]=#{v}"
130
+ end.join '&'
131
+ elsif value.class == Array
132
+ value.map do |v|
133
+ "#{key}[]=#{v}"
134
+ end.join '&'
135
+ else
136
+ "#{key}=#{value}"
137
+ end
138
+ end.join '&'
139
+ end
140
+
141
+ end
142
+
@@ -0,0 +1,44 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mailchimp_api}
8
+ s.version = "0.0.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Benoit Caccinolo"]
12
+ s.date = %q{2009-08-11}
13
+ s.description = %q{TODO}
14
+ s.email = %q{benoit.caccinolo@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README"
17
+ ]
18
+ s.files = [
19
+ "README",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "lib/mailchimp.rb",
23
+ "mailchimp_api.gemspec",
24
+ "test/test_mc.rb"
25
+ ]
26
+ s.homepage = %q{http://ruready.tumblr.com}
27
+ s.rdoc_options = ["--charset=UTF-8"]
28
+ s.require_paths = ["lib"]
29
+ s.rubygems_version = %q{1.3.4}
30
+ s.summary = %q{Summarize your gem}
31
+ s.test_files = [
32
+ "test/test_mc.rb"
33
+ ]
34
+
35
+ if s.respond_to? :specification_version then
36
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
37
+ s.specification_version = 3
38
+
39
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
40
+ else
41
+ end
42
+ else
43
+ end
44
+ end
data/test/test_mc.rb ADDED
@@ -0,0 +1,100 @@
1
+ require "test/unit"
2
+ require "lib/mailchimp"
3
+
4
+ LOGIN=""
5
+ PWD=""
6
+
7
+ class TestMailChimp < Test::Unit::TestCase
8
+
9
+ @@done = nil
10
+ @@list_id = ""
11
+ @@campaign_id = ""
12
+
13
+ def setup
14
+ raise "You have to set the Constants LOGIN and PWD." if LOGIN == ""
15
+ if @@done.nil?
16
+ @@done = true
17
+ @@mc = MailChimp.new
18
+ @@mc.login LOGIN, PWD
19
+ test_lists
20
+ end
21
+ end
22
+
23
+ #def teardown
24
+ #end
25
+
26
+ def test_ping
27
+ ret = @mc.ping
28
+ assert ret == true
29
+ end
30
+
31
+ def test_lists
32
+ ret = @@mc.lists
33
+ list_ids = ret.map { |e| e['name'] }
34
+ result = list_ids.include?("test_list")
35
+ unless result
36
+ raise "In order to continue the tests, you have to create a list called test_list."
37
+ end
38
+ ret.each do |list|
39
+ @@list_id = list["id"] if list["name"] == "test_list"
40
+ end
41
+ assert result
42
+ end
43
+
44
+ def test_list_subscribe
45
+ assert @mc.list_subscribe(@list_id, "benoit_test@gmail.com")
46
+ end
47
+
48
+ def test_list_unsubscribe
49
+ assert @mc.list_unsubscribe(@list_id, "benoit_test@gmail.com")
50
+ end
51
+
52
+ def test_list_batch_subscribe
53
+ ret = @mc.list_batch_subscribe(@list_id, [ ["EMAIL" => "benoit_test4@gmail.com"] ])
54
+ assert(ret["success_count"] == "1", ret.to_yaml)
55
+ end
56
+
57
+ def test_list_batch_unsubscribe
58
+ @mc.list_subscribe(@list_id, "benoit_test1@gmail.com")
59
+ @mc.list_subscribe(@list_id, "benoit_test2@gmail.com")
60
+ ret = @mc.list_batch_unsubscribe(@list_id, ["benoit_test1@gmail.com", "benoit_test2@gmail.com"])
61
+ assert(ret["success_count"] == 2, ret.to_yaml)
62
+ end
63
+
64
+ def test_campaign_create_and_delete
65
+ ret = @@mc.campaign_create(@@list_id, "from_benoit@gmail.com", "Benoit Caccinolo",
66
+ "This the subject of the newsletter",
67
+ "This is the content of the newsletter")
68
+ if ret.class == String
69
+ @@campaign_id = ret
70
+ end
71
+ assert(ret.class == String)
72
+
73
+ ret = @@mc.campaign_delete @@campaign_id
74
+ assert ret
75
+ end
76
+
77
+ def test_campaign_update
78
+ ret = @@mc.campaign_create(@@list_id, "from_benoit@gmail.com", "Benoit Caccinolo",
79
+ "This the subject of the newsletter",
80
+ "This is the content of the newsletter")
81
+ campaign_id = ret
82
+ ret = @@mc.campaign_update(campaign_id, "content", {"html" => "New content"})
83
+ assert ret
84
+ @@mc.campaign_delete campaign_id
85
+ end
86
+
87
+ def test_campaign_send_test
88
+ ret = @@mc.campaign_create(@@list_id, "from_benoit@gmail.com", "Benoit Caccinolo",
89
+ "This the subject of the newsletter 2222222222222",
90
+ "This is the content of the newsletter")
91
+ campaign_id = ret
92
+
93
+ ret = @@mc.campaign_send_test(campaign_id, ["benoit.caccinolo@gmail.com"])
94
+ puts ret
95
+ assert ret
96
+ @@mc.campaign_delete campaign_id
97
+ end
98
+
99
+ end
100
+
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bcaccinolo-mailchimp_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Benoit Caccinolo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-11 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: TODO
17
+ email: benoit.caccinolo@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - README
26
+ - Rakefile
27
+ - VERSION
28
+ - lib/mailchimp.rb
29
+ - mailchimp_api.gemspec
30
+ - test/test_mc.rb
31
+ has_rdoc: false
32
+ homepage: http://ruready.tumblr.com
33
+ licenses:
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --charset=UTF-8
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.3.5
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Summarize your gem
58
+ test_files:
59
+ - test/test_mc.rb