dpickett-monkey_party 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +26 -0
- data/VERSION.yml +4 -0
- data/lib/monkey_party/account.rb +48 -0
- data/lib/monkey_party/base.rb +30 -0
- data/lib/monkey_party/error.rb +9 -0
- data/lib/monkey_party/list.rb +95 -0
- data/lib/monkey_party/subscriber.rb +30 -0
- data/lib/monkey_party.rb +14 -0
- data/test/fixtures/batch_subscribe_failed.xml +28 -0
- data/test/fixtures/batch_subscribe_successful.xml +12 -0
- data/test/fixtures/batch_unsubscribe_failed.xml +19 -0
- data/test/fixtures/batch_unsubscribe_successful.xml +12 -0
- data/test/fixtures/lists_failed.xml +11 -0
- data/test/fixtures/lists_successful.xml +16 -0
- data/test/fixtures/login_failed.xml +11 -0
- data/test/fixtures/login_successful.xml +8 -0
- data/test/monkey_party/account_test.rb +91 -0
- data/test/monkey_party/error_test.rb +18 -0
- data/test/monkey_party/list_test.rb +173 -0
- data/test/monkey_party/subscriber_test.rb +37 -0
- data/test/test_helper.rb +31 -0
- metadata +108 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Dan Pickett
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
= monkey_party
|
2
|
+
|
3
|
+
An extremely young API for interacting with the MailChimp API
|
4
|
+
|
5
|
+
Uses the excellent HTTParty and HappyMapper tools and is well tested
|
6
|
+
|
7
|
+
Currently only supports
|
8
|
+
|
9
|
+
* adding api keys
|
10
|
+
* finding lists by name
|
11
|
+
* batch subscribing
|
12
|
+
* unsubscribing
|
13
|
+
|
14
|
+
== Example Usage
|
15
|
+
|
16
|
+
account = MonkeyParty::Account.login("monkey_username", "party_password")
|
17
|
+
list = MonkeyParty::List.find_by_name("list_name")
|
18
|
+
list.create_subscribers([MonkeyParty::Subscriber.new("user@example.com")])
|
19
|
+
|
20
|
+
== TODO
|
21
|
+
* Batch subscribe does not allow you to resubscribe members - implement individual subscribe method
|
22
|
+
* Clean up after cucumber features are run
|
23
|
+
|
24
|
+
== Copyright
|
25
|
+
|
26
|
+
Copyright (c) 2009 Dan Pickett. See LICENSE for details.
|
data/VERSION.yml
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
module MonkeyParty
|
2
|
+
class Account < Base
|
3
|
+
attr_accessor :user_name, :password, :keys
|
4
|
+
|
5
|
+
def initialize(attrs = {})
|
6
|
+
self.keys ||= []
|
7
|
+
self.keys << attrs.delete(:api_key) if attrs[:api_key]
|
8
|
+
super
|
9
|
+
end
|
10
|
+
|
11
|
+
def api_key
|
12
|
+
self.keys[0]
|
13
|
+
end
|
14
|
+
|
15
|
+
class << self
|
16
|
+
def login(user_name, password)
|
17
|
+
response = get("", :query => {
|
18
|
+
:method => "login",
|
19
|
+
:username => user_name,
|
20
|
+
:password => password
|
21
|
+
})["MCAPI"]
|
22
|
+
|
23
|
+
account = new
|
24
|
+
|
25
|
+
account.keys << response
|
26
|
+
account.user_name = user_name
|
27
|
+
account.password = password
|
28
|
+
|
29
|
+
#set a global api key
|
30
|
+
configatron.mailchimp.api_key = response
|
31
|
+
|
32
|
+
account
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def add_api_key
|
37
|
+
response = self.class.get("", :query => {
|
38
|
+
:method => "apikeyAdd",
|
39
|
+
:username => self.user_name,
|
40
|
+
:password => self.password,
|
41
|
+
:apikey => self.api_key
|
42
|
+
})["MCAPI"]
|
43
|
+
|
44
|
+
self.keys << response
|
45
|
+
response
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "ruby-debug"
|
2
|
+
module MonkeyParty
|
3
|
+
class Base
|
4
|
+
include HTTParty
|
5
|
+
base_uri 'http://api.mailchimp.com/1.1/'
|
6
|
+
default_params :output => "xml"
|
7
|
+
|
8
|
+
def initialize(attrs = {})
|
9
|
+
attrs.each{ |key, value| self.send("#{key}=", value) }
|
10
|
+
end
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def get(path, options = {})
|
14
|
+
#fix the format because Mail Chimp doesn't pass the proper header
|
15
|
+
options[:format] = :xml
|
16
|
+
result = super
|
17
|
+
|
18
|
+
if result.body =~ /<error /i
|
19
|
+
raise MonkeyParty::Error.parse(result.body)
|
20
|
+
end
|
21
|
+
result
|
22
|
+
end
|
23
|
+
|
24
|
+
def api_key
|
25
|
+
configatron.mailchimp.api_key
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module MonkeyParty
|
2
|
+
class List < Base
|
3
|
+
include HappyMapper
|
4
|
+
tag "struct"
|
5
|
+
|
6
|
+
element :id, String
|
7
|
+
element :web_id, String
|
8
|
+
element :name, String
|
9
|
+
element :date_created, Time
|
10
|
+
element :member_count, Integer
|
11
|
+
|
12
|
+
def create_subscribers(array_of_subscribers, options = {})
|
13
|
+
options[:double_optin] ||= true
|
14
|
+
options[:update_existing] ||= false
|
15
|
+
options[:replace_interests] ||= true
|
16
|
+
|
17
|
+
batch_hash = {}
|
18
|
+
index = 0
|
19
|
+
array_of_subscribers.each do |s|
|
20
|
+
batch_hash["batch[#{index}]"] = s.to_mailchimp_hash
|
21
|
+
index += 1
|
22
|
+
end
|
23
|
+
|
24
|
+
response = self.class.get("", :query => {
|
25
|
+
:apikey => self.class.api_key,
|
26
|
+
:id => self.id,
|
27
|
+
:method => "listBatchSubscribe",
|
28
|
+
}.merge(options).merge(batch_hash))
|
29
|
+
|
30
|
+
#response[1] is the error count
|
31
|
+
if !response["MCAPI"].nil? && response["MCAPI"][1] > 0
|
32
|
+
attach_errors_to_subscribers(array_of_subscribers, response.body)
|
33
|
+
end
|
34
|
+
|
35
|
+
array_of_subscribers
|
36
|
+
end
|
37
|
+
|
38
|
+
def destroy_subscribers(array_of_unsubscribers, options = {})
|
39
|
+
options[:delete_member] ||= false
|
40
|
+
options[:send_goodbye] ||= true
|
41
|
+
options[:send_notify] ||= false
|
42
|
+
|
43
|
+
batch_hash = {}
|
44
|
+
index = 0
|
45
|
+
array_of_unsubscribers.each do |s|
|
46
|
+
batch_hash["emails[#{index}]"] = s.email
|
47
|
+
end
|
48
|
+
|
49
|
+
response = self.class.get("", :query => {
|
50
|
+
:apikey => self.class.api_key,
|
51
|
+
:id => self.id,
|
52
|
+
:method => "listBatchUnsubscribe"
|
53
|
+
}.merge(options).merge(batch_hash))
|
54
|
+
|
55
|
+
if !response["MCAPI"].nil? && response["MCAPI"][1] > 0
|
56
|
+
attach_errors_to_subscribers(array_of_unsubscribers, response.body)
|
57
|
+
end
|
58
|
+
|
59
|
+
array_of_unsubscribers
|
60
|
+
end
|
61
|
+
|
62
|
+
class << self
|
63
|
+
def all
|
64
|
+
response = get("", :query => {
|
65
|
+
:apikey => api_key,
|
66
|
+
:method => "lists"
|
67
|
+
})
|
68
|
+
|
69
|
+
parse(response.body)
|
70
|
+
end
|
71
|
+
|
72
|
+
def find_by_name(name)
|
73
|
+
all.each do |list|
|
74
|
+
return list if list.name.downcase == name.downcase
|
75
|
+
end
|
76
|
+
|
77
|
+
return nil
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
def attach_errors_to_subscribers(subscribers, response)
|
83
|
+
#parse errors and update subscriber
|
84
|
+
error_nodes = XML::Parser.string(response).parse.root.find("/MCAPI/errors/struct")
|
85
|
+
error_nodes.each do |n|
|
86
|
+
|
87
|
+
sub_error = MonkeyParty::Error.new
|
88
|
+
sub_error.message = n.find_first("message").content
|
89
|
+
sub_error.code = n.find_first("code").content
|
90
|
+
|
91
|
+
subscribers[n.attributes["key"].to_i].error = sub_error
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module MonkeyParty
|
2
|
+
class Subscriber
|
3
|
+
attr_accessor :email, :merge_fields, :error
|
4
|
+
|
5
|
+
def initialize(email, merge_fields = {})
|
6
|
+
self.email = email
|
7
|
+
self.merge_fields = merge_fields
|
8
|
+
self.error = nil
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_h
|
12
|
+
{
|
13
|
+
:email => self.email
|
14
|
+
}.merge(self.merge_fields)
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_mailchimp_hash
|
18
|
+
chimp_hash = {}
|
19
|
+
self.to_h.each do |key, value|
|
20
|
+
chimp_hash[key.to_s.upcase] = value
|
21
|
+
end
|
22
|
+
|
23
|
+
chimp_hash
|
24
|
+
end
|
25
|
+
|
26
|
+
def valid?
|
27
|
+
self.error.nil?
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/monkey_party.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "httparty"
|
3
|
+
require "happymapper"
|
4
|
+
|
5
|
+
require "configatron"
|
6
|
+
|
7
|
+
require "monkey_party/error"
|
8
|
+
|
9
|
+
require "monkey_party/base"
|
10
|
+
|
11
|
+
require "monkey_party/subscriber"
|
12
|
+
require "monkey_party/account"
|
13
|
+
require "monkey_party/list"
|
14
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
HTTP/1.1 200 OK
|
2
|
+
Date: Wed, 04 Feb 2009 03:42:26 GMT
|
3
|
+
Server: Apache/2.2.3 (Debian) mod_python/3.2.10 Python/2.4.4 PHP/5.2.0-8+etch11
|
4
|
+
Content-Type: application/xml
|
5
|
+
Transfer-Encoding: chunked
|
6
|
+
|
7
|
+
<MCAPI type="array">
|
8
|
+
<success_count type="integer">0</success_count>
|
9
|
+
<error_count type="integer">2</error_count>
|
10
|
+
<errors type="array">
|
11
|
+
<struct key="0" type="array">
|
12
|
+
<code type="integer">214</code>
|
13
|
+
<message type="string">user@example.com is already subscribed to list Testing</message>
|
14
|
+
<row type="array">
|
15
|
+
<EMAIL type="string">user@example.com</EMAIL>
|
16
|
+
</row>
|
17
|
+
</struct>
|
18
|
+
<struct key="1" type="array">
|
19
|
+
|
20
|
+
<code type="integer">214</code>
|
21
|
+
<message type="string">user@3example.com is already subscribed to list Testing</message>
|
22
|
+
<row type="array">
|
23
|
+
<EMAIL type="string">user@3example.com</EMAIL>
|
24
|
+
</row>
|
25
|
+
</struct>
|
26
|
+
</errors>
|
27
|
+
</MCAPI>
|
28
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
HTTP/1.1 200 OK
|
2
|
+
Date: Wed, 04 Feb 2009 03:42:26 GMT
|
3
|
+
Server: Apache/2.2.3 (Debian) mod_python/3.2.10 Python/2.4.4 PHP/5.2.0-8+etch11
|
4
|
+
Content-Type: application/xml
|
5
|
+
Transfer-Encoding: chunked
|
6
|
+
|
7
|
+
<mcapi type="array">
|
8
|
+
<success_count type="integer">2</success_count>
|
9
|
+
<error_count type="integer">0</error_count>
|
10
|
+
<errors type="array">
|
11
|
+
</errors></mcapi>
|
12
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
HTTP/1.1 200 OK
|
2
|
+
Date: Wed, 04 Feb 2009 03:42:26 GMT
|
3
|
+
Server: Apache/2.2.3 (Debian) mod_python/3.2.10 Python/2.4.4 PHP/5.2.0-8+etch11
|
4
|
+
Content-Type: application/xml
|
5
|
+
Transfer-Encoding: chunked
|
6
|
+
|
7
|
+
<MCAPI type="array">
|
8
|
+
<success_count type="integer">0</success_count>
|
9
|
+
<error_count type="integer">1</error_count>
|
10
|
+
<errors type="array">
|
11
|
+
<struct key="0" type="array">
|
12
|
+
<code type="integer">232</code>
|
13
|
+
<message type="string">There is no record of "user3@example.com" in the database</message>
|
14
|
+
<email type="string">user3@example.com</email>
|
15
|
+
</struct>
|
16
|
+
|
17
|
+
</errors>
|
18
|
+
</MCAPI>
|
19
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
HTTP/1.1 200 OK
|
2
|
+
Date: Wed, 04 Feb 2009 03:42:26 GMT
|
3
|
+
Server: Apache/2.2.3 (Debian) mod_python/3.2.10 Python/2.4.4 PHP/5.2.0-8+etch11
|
4
|
+
Content-Type: application/xml
|
5
|
+
Transfer-Encoding: chunked
|
6
|
+
|
7
|
+
<mcapi type="array">
|
8
|
+
<success_count type="integer">1</success_count>
|
9
|
+
<error_count type="integer">0</error_count>
|
10
|
+
<errors type="array">
|
11
|
+
</errors></mcapi>
|
12
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
HTTP/1.1 200 OK
|
2
|
+
Date: Wed, 04 Feb 2009 03:42:26 GMT
|
3
|
+
Server: Apache/2.2.3 (Debian) mod_python/3.2.10 Python/2.4.4 PHP/5.2.0-8+etch11
|
4
|
+
Content-Type: application/xml
|
5
|
+
Transfer-Encoding: chunked
|
6
|
+
|
7
|
+
<MCAPI type="array">
|
8
|
+
<error type="string">Invalid Mailchimp API Key: fdasf</error>
|
9
|
+
<code type="integer">104</code>
|
10
|
+
</MCAPI>
|
11
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
HTTP/1.1 200 OK
|
2
|
+
Date: Wed, 04 Feb 2009 03:42:26 GMT
|
3
|
+
Server: Apache/2.2.3 (Debian) mod_python/3.2.10 Python/2.4.4 PHP/5.2.0-8+etch11
|
4
|
+
Content-Type: application/xml
|
5
|
+
Transfer-Encoding: chunked
|
6
|
+
|
7
|
+
<MCAPI type="array">
|
8
|
+
<struct key="0" type="array">
|
9
|
+
<id type="string">d40bbc3056</id>
|
10
|
+
<web_id type="integer">63816</web_id>
|
11
|
+
<name type="string">Testing</name>
|
12
|
+
<date_created type="string">Mar 27, 2009 07:00 pm</date_created>
|
13
|
+
<member_count type="double">0</member_count>
|
14
|
+
</struct>
|
15
|
+
</MCAPI>
|
16
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
HTTP/1.1 200 OK
|
2
|
+
Date: Wed, 04 Feb 2009 03:42:26 GMT
|
3
|
+
Server: Apache/2.2.3 (Debian) mod_python/3.2.10 Python/2.4.4 PHP/5.2.0-8+etch11
|
4
|
+
Content-Type: application/xml
|
5
|
+
Transfer-Encoding: chunked
|
6
|
+
|
7
|
+
<MCAPI type="array">
|
8
|
+
<error type="string">I'm sorry, that username/password could not be found</error>
|
9
|
+
<code type="integer">100</code>
|
10
|
+
</MCAPI>
|
11
|
+
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class MonkeyParty::AccountTest < Test::Unit::TestCase
|
4
|
+
context "successfully logging in" do
|
5
|
+
setup do
|
6
|
+
configatron.mailchimp.api_key
|
7
|
+
|
8
|
+
mock_login_response(true)
|
9
|
+
|
10
|
+
@account = MonkeyParty::Account.login(@user_name, @password)
|
11
|
+
end
|
12
|
+
|
13
|
+
should "return an api key" do
|
14
|
+
assert_not_nil @account.api_key
|
15
|
+
end
|
16
|
+
|
17
|
+
should "set the user_name" do
|
18
|
+
assert_not_nil @account.user_name
|
19
|
+
end
|
20
|
+
|
21
|
+
should "set the global api key" do
|
22
|
+
assert_not_nil configatron.mailchimp.api_key
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "logging in with invalid credentials" do
|
27
|
+
setup do
|
28
|
+
mock_login_response(false)
|
29
|
+
end
|
30
|
+
|
31
|
+
should "raise an error" do
|
32
|
+
assert_raises MonkeyParty::Error do
|
33
|
+
MonkeyParty::Account.login(@user_name, @password)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context "adding an api key" do
|
39
|
+
setup do
|
40
|
+
@account = MonkeyParty::Account.new(
|
41
|
+
:user_name => "test",
|
42
|
+
:password => "password",
|
43
|
+
:api_key => "api_key")
|
44
|
+
end
|
45
|
+
|
46
|
+
context "with proper credentials" do
|
47
|
+
setup do
|
48
|
+
mock_response("password=#{@account.password}&" +
|
49
|
+
"apikey=#{@account.api_key}&method=apikeyAdd&" +
|
50
|
+
"output=xml&username=#{@account.user_name}",
|
51
|
+
"login_successful")
|
52
|
+
end
|
53
|
+
|
54
|
+
should "return an api key" do
|
55
|
+
response = @account.add_api_key
|
56
|
+
assert_kind_of String, response
|
57
|
+
end
|
58
|
+
|
59
|
+
should "add the resulting api key to extra keys" do
|
60
|
+
response = @account.add_api_key
|
61
|
+
assert @account.keys.include?(response)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context "with improper credentials" do
|
66
|
+
setup do
|
67
|
+
mock_response("password=#{@account.password}&" +
|
68
|
+
"apikey=#{@account.api_key}&method=apikeyAdd&" +
|
69
|
+
"output=xml&username=#{@account.user_name}",
|
70
|
+
"login_failed")
|
71
|
+
end
|
72
|
+
|
73
|
+
should "raise an error if I don't have the right credentials" do
|
74
|
+
assert_raise MonkeyParty::Error do
|
75
|
+
@account.add_api_key
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
def mock_login_response(successful = true)
|
83
|
+
@user_name = "test_user"
|
84
|
+
@password = "password"
|
85
|
+
|
86
|
+
mock_response("method=login&username=#{@user_name}" +
|
87
|
+
"&password=#{@password}&output=xml",
|
88
|
+
successful ? "login_successful" : "login_failed")
|
89
|
+
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class MonkeyParty::ErrorTest < Test::Unit::TestCase
|
4
|
+
context "An error" do
|
5
|
+
setup do
|
6
|
+
error_body = File.read(xml_fixture_path("login_failed")).gsub(/.*<MCAPI/m, "<MCAPI")
|
7
|
+
@error = MonkeyParty::Error.parse(error_body)
|
8
|
+
end
|
9
|
+
|
10
|
+
should "have a code" do
|
11
|
+
assert_not_nil @error.code
|
12
|
+
end
|
13
|
+
|
14
|
+
should "have an error message" do
|
15
|
+
assert_not_nil @error.message
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,173 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class MonkeyParty::ListTest < Test::Unit::TestCase
|
4
|
+
context "a list" do
|
5
|
+
should "have a set of members" do
|
6
|
+
|
7
|
+
end
|
8
|
+
|
9
|
+
should "allow me to add members to the list" do
|
10
|
+
end
|
11
|
+
|
12
|
+
should "allow me to unsubscribe members from the list" do
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
should "have an id" do
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
should "have a web_id" do
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
should "have a name" do
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
should "have a date of creation" do
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
should "have a member count" do
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
context "when subscribing" do
|
39
|
+
|
40
|
+
setup do
|
41
|
+
mock_all_response
|
42
|
+
|
43
|
+
@list = MonkeyParty::List.all[0]
|
44
|
+
|
45
|
+
@subscribers = []
|
46
|
+
@subscribers << MonkeyParty::Subscriber.new("user@example.com",
|
47
|
+
{:fname => "A User"})
|
48
|
+
@subscribers << MonkeyParty::Subscriber.new("user3@example.com",
|
49
|
+
{:fname => "Another User"})
|
50
|
+
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
context "successfully" do
|
55
|
+
setup do
|
56
|
+
mock_subscription
|
57
|
+
@resultant_subscribers = @list.create_subscribers(@subscribers)
|
58
|
+
end
|
59
|
+
|
60
|
+
should "return an array of subscribers" do
|
61
|
+
assert_kind_of MonkeyParty::Subscriber, @resultant_subscribers[0]
|
62
|
+
end
|
63
|
+
|
64
|
+
should "indicate that each subscription was valid" do
|
65
|
+
@resultant_subscribers.each do |s|
|
66
|
+
assert s.valid?
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context "resulting in error(s)" do
|
72
|
+
setup do
|
73
|
+
mock_subscription(false)
|
74
|
+
@resultant_subscribers = @list.create_subscribers(@subscribers)
|
75
|
+
end
|
76
|
+
|
77
|
+
should "have invalid subscribers" do
|
78
|
+
assert !@resultant_subscribers[0].valid?
|
79
|
+
end
|
80
|
+
|
81
|
+
should "have a subscriber with an error" do
|
82
|
+
assert_not_nil @resultant_subscribers[0].error
|
83
|
+
assert_kind_of MonkeyParty::Error, @resultant_subscribers[0].error
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context "when unsubscribing" do
|
89
|
+
setup do
|
90
|
+
mock_all_response
|
91
|
+
@list = MonkeyParty::List.all[0]
|
92
|
+
@unsubscribers = [
|
93
|
+
MonkeyParty::Subscriber.new("user@example.com"),
|
94
|
+
MonkeyParty::Subscriber.new("user3@example.com")
|
95
|
+
]
|
96
|
+
end
|
97
|
+
|
98
|
+
context "successfully" do
|
99
|
+
setup do
|
100
|
+
mock_unsubscription
|
101
|
+
|
102
|
+
@resultant_unsubscribers = @list.destroy_subscribers(@unsubscribers)
|
103
|
+
end
|
104
|
+
|
105
|
+
should "have all valid subscribers" do
|
106
|
+
@resultant_unsubscribers.each do |s|
|
107
|
+
assert s.valid?
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
context "resulting in errors(s)" do
|
113
|
+
setup do
|
114
|
+
mock_unsubscription(false)
|
115
|
+
@resultant_unsubscribers = @list.destroy_subscribers(@unsubscribers)
|
116
|
+
end
|
117
|
+
|
118
|
+
should "have an invalid subscriber" do
|
119
|
+
assert !@resultant_unsubscribers[0].valid?
|
120
|
+
end
|
121
|
+
|
122
|
+
should "have a usable error for the invalid subscriber" do
|
123
|
+
assert_not_nil @resultant_unsubscribers[0].error.message
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
context "lists" do
|
129
|
+
should "retrieving all of the lists" do
|
130
|
+
mock_all_response
|
131
|
+
lists = MonkeyParty::List.all
|
132
|
+
assert_kind_of Array, lists
|
133
|
+
assert_not_nil lists[0]
|
134
|
+
assert_not_nil lists[0].name
|
135
|
+
end
|
136
|
+
|
137
|
+
should "raise an error if something goes wrong" do
|
138
|
+
mock_all_response(false)
|
139
|
+
assert_raises MonkeyParty::Error do
|
140
|
+
lists = MonkeyParty::List.all
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
should "find by name" do
|
145
|
+
mock_all_response
|
146
|
+
assert_equal "Testing", MonkeyParty::List.find_by_name("Testing").name
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
private
|
151
|
+
def mock_all_response(success = true)
|
152
|
+
mock_response("apikey=2491541245g978jkasf&method=lists&output=xml",
|
153
|
+
success ? "lists_successful" : "lists_failed")
|
154
|
+
end
|
155
|
+
|
156
|
+
def mock_subscription(success = true)
|
157
|
+
mock_response(
|
158
|
+
"double_optin=true&update_existing=false&replace_interests=true&" +
|
159
|
+
"method=listBatchSubscribe&batch[1][EMAIL]=user3%40example.com&" +
|
160
|
+
"batch[1][FNAME]=Another%20User&output=xml&" +
|
161
|
+
"batch[0][EMAIL]=user%40example.com&batch[0][FNAME]=A%20User&"+
|
162
|
+
"apikey=2491541245g978jkasf&id=d40bbc3056",
|
163
|
+
success ? "batch_subscribe_successful" : "batch_subscribe_failed")
|
164
|
+
end
|
165
|
+
|
166
|
+
def mock_unsubscription(success = true)
|
167
|
+
mock_response(
|
168
|
+
"delete_member=false&send_goodbye=true&method=listBatchUnsubscribe&" +
|
169
|
+
"send_notify=false&output=xml&apikey=2491541245g978jkasf&id=d40bbc3056&" +
|
170
|
+
"emails[0]=user3%40example.com",
|
171
|
+
success ? "batch_unsubscribe_successful" : "batch_unsubscribe_failed")
|
172
|
+
end
|
173
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class MonkeyParty::SubscriberTest < Test::Unit::TestCase
|
4
|
+
context "A subscriber" do
|
5
|
+
setup do
|
6
|
+
@merge_vars = {:merge_var => "MERGE VAR 1"}
|
7
|
+
@email = "user@example.com"
|
8
|
+
@subscriber = MonkeyParty::Subscriber.new(@email, @merge_vars)
|
9
|
+
end
|
10
|
+
|
11
|
+
should "have an email" do
|
12
|
+
assert_equal @email, @subscriber.email
|
13
|
+
end
|
14
|
+
|
15
|
+
should "have a list of merge fields" do
|
16
|
+
assert_equal @merge_vars, @subscriber.merge_fields
|
17
|
+
end
|
18
|
+
|
19
|
+
should "have a hashed version of all the attributes" do
|
20
|
+
intended_hash = {
|
21
|
+
:email => @email
|
22
|
+
}.merge(@merge_vars)
|
23
|
+
|
24
|
+
assert_equal intended_hash, @subscriber.to_h
|
25
|
+
end
|
26
|
+
|
27
|
+
should "have a mailchimp style hash of all the attributes" do
|
28
|
+
intended_hash = {}
|
29
|
+
intended_hash["EMAIL"] = @email
|
30
|
+
@merge_vars.each do |key, value|
|
31
|
+
intended_hash[key.to_s.upcase] = value
|
32
|
+
end
|
33
|
+
|
34
|
+
assert_equal intended_hash, @subscriber.to_mailchimp_hash
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require "redgreen"
|
7
|
+
rescue LoadError; end
|
8
|
+
|
9
|
+
require 'fake_web'
|
10
|
+
|
11
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
12
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
13
|
+
require 'monkey_party'
|
14
|
+
|
15
|
+
FakeWeb.allow_net_connect = false
|
16
|
+
|
17
|
+
configatron.mailchimp.api_key = "api_key"
|
18
|
+
|
19
|
+
class Test::Unit::TestCase
|
20
|
+
protected
|
21
|
+
def mock_response(path, fixture)
|
22
|
+
FakeWeb.register_uri(
|
23
|
+
"http://api.mailchimp.com/1.1?#{path}",
|
24
|
+
:response => xml_fixture_path(fixture))
|
25
|
+
end
|
26
|
+
|
27
|
+
def xml_fixture_path(fixture)
|
28
|
+
File.join(File.dirname(__FILE__), "fixtures", "#{fixture}.xml")
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dpickett-monkey_party
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dan Pickett
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-02 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: jnunemaker-httparty
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.3.1
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: jnunemaker-happymapper
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.2.2
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: configatron
|
37
|
+
type: :runtime
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 2.2.2
|
44
|
+
version:
|
45
|
+
description:
|
46
|
+
email: dpickett@enlightsolutions.com
|
47
|
+
executables: []
|
48
|
+
|
49
|
+
extensions: []
|
50
|
+
|
51
|
+
extra_rdoc_files:
|
52
|
+
- README.rdoc
|
53
|
+
- LICENSE
|
54
|
+
files:
|
55
|
+
- README.rdoc
|
56
|
+
- VERSION.yml
|
57
|
+
- lib/monkey_party
|
58
|
+
- lib/monkey_party/account.rb
|
59
|
+
- lib/monkey_party/base.rb
|
60
|
+
- lib/monkey_party/error.rb
|
61
|
+
- lib/monkey_party/list.rb
|
62
|
+
- lib/monkey_party/subscriber.rb
|
63
|
+
- lib/monkey_party.rb
|
64
|
+
- test/fixtures
|
65
|
+
- test/fixtures/batch_subscribe_failed.xml
|
66
|
+
- test/fixtures/batch_subscribe_successful.xml
|
67
|
+
- test/fixtures/batch_unsubscribe_failed.xml
|
68
|
+
- test/fixtures/batch_unsubscribe_successful.xml
|
69
|
+
- test/fixtures/lists_failed.xml
|
70
|
+
- test/fixtures/lists_successful.xml
|
71
|
+
- test/fixtures/login_failed.xml
|
72
|
+
- test/fixtures/login_successful.xml
|
73
|
+
- test/monkey_party
|
74
|
+
- test/monkey_party/account_test.rb
|
75
|
+
- test/monkey_party/error_test.rb
|
76
|
+
- test/monkey_party/list_test.rb
|
77
|
+
- test/monkey_party/subscriber_test.rb
|
78
|
+
- test/test_helper.rb
|
79
|
+
- LICENSE
|
80
|
+
has_rdoc: true
|
81
|
+
homepage: http://github.com/dpickett/monkey_party
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options:
|
84
|
+
- --inline-source
|
85
|
+
- --charset=UTF-8
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: "0"
|
93
|
+
version:
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: "0"
|
99
|
+
version:
|
100
|
+
requirements: []
|
101
|
+
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 1.2.0
|
104
|
+
signing_key:
|
105
|
+
specification_version: 2
|
106
|
+
summary: TODO
|
107
|
+
test_files: []
|
108
|
+
|