Skip to content

Commit

Permalink
Fixed bug in List.find
Browse files Browse the repository at this point in the history
  • Loading branch information
kamarcum authored and Glenn Gillen committed Nov 19, 2010
1 parent 3c87328 commit 6ac971a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 33 deletions.
17 changes: 8 additions & 9 deletions lib/monkey_wrench/base.rb
Expand Up @@ -11,12 +11,12 @@ class Base < OpenStruct
@@apikey = nil
@@datacenter = nil
@@dryrun = false

class << self
def default_query_params
{ :output => "json", :apikey=> @@apikey}
end

def base_uri
"http://#{datacenter}.api.mailchimp.com/1.2/"
end
Expand All @@ -36,7 +36,7 @@ def get(params, http_options = {})
end
end
end

def post(params, http_options = {})
if @@dryrun
puts "POST #{base_uri} #{params.merge(default_query_params).inspect}"
Expand All @@ -50,10 +50,10 @@ def post(params, http_options = {})
end
end
end

def handle_errors(objects)
return objects unless objects.respond_to?(:has_key?)

if objects.has_key?("error")
objects.replace({ "error" => MonkeyWrench::Error.new(objects['error'], objects['code']) })
elsif objects.has_key?("errors")
Expand All @@ -69,12 +69,12 @@ def handle_errors(objects)
def apikey
@@apikey
end

def datacenter
@@datacenter
end
end

private
def self.robustly(http_options, &block)
retry_limit = http_options[:retry_limit] || default_retry_limit
Expand All @@ -94,10 +94,9 @@ def self.robustly(http_options, &block)
def get(*args)
self.class.get(*args)
end

def post(*args)
self.class.post(*args)
end

end
end
30 changes: 15 additions & 15 deletions lib/monkey_wrench/list.rb
Expand Up @@ -3,7 +3,7 @@
module MonkeyWrench
class List < MonkeyWrench::Base
# Finds a given list by name
#
#
# @example
# MonkeyWrench::List.find_by_name("My Example List")
#
Expand All @@ -12,7 +12,7 @@ class List < MonkeyWrench::Base
def self.find_by_name(list_name)
lists = find_all.detect{|list| list.name == list_name}
end

# Will compare another list against the current one and return true if
# they are the same (based on list ID)
#
Expand All @@ -36,15 +36,15 @@ def ==(other_list)
# @param [String] id the unique Mailchimp list ID
# @return [MonkeyWrench::List] the list
def self.find(id)
new(:id => id)
find_all.find{|e| e.id == id}
end

# Finds all lists
#
# @example
# MonkeyWrench::List.find_all
#
# @return [Array<MonkeyWrench::List>]
# @return [Array<MonkeyWrench::List>]
def self.find_all
@@lists ||= post({ :method => "lists" }).map do |list|
List.new(list)
Expand All @@ -54,7 +54,7 @@ def self.find_all
class << self
alias :all :find_all
end

# Returns all members for this list
#
# @example Find all members that have unsubscribed in the last 24 hours:
Expand Down Expand Up @@ -83,7 +83,7 @@ def members(options = {})
MonkeyWrench::Member.new(response_user)
end
end
end
end

# Enumerates over each member and executes the provided block. Will
# automatically page and batch requests for members.
Expand All @@ -96,7 +96,7 @@ def members(options = {})
# end
#
# @param [Proc] &block code to execute for each member
def each_member(&block)
def each_member(&block)
page = 0
loop do
batch = members(:start => page, :limit => 15000)
Expand All @@ -107,7 +107,7 @@ def each_member(&block)
page += 1
end
end

# Updates details of list members
#
# @example Update a single member's email address
Expand Down Expand Up @@ -140,7 +140,7 @@ def update_members(members, options = {})
post(options.merge(mailchimp_args))
end
end

# Find a member in this list with the given email address
#
# @example
Expand Down Expand Up @@ -193,7 +193,7 @@ def subscribe(contact_details, opts = {})
return { :success => 1, :errors => []}
end
end

# Unsubscribes a person (or list of people) from the list
#
# @example Unsubscribe a single user
Expand Down Expand Up @@ -222,7 +222,7 @@ def unsubscribe(emails, opts = {})
return { :success => response["success_count"],
:errors => response["errors"] }
end

# Will flag the email(s) as opted-out for all future mailing for this list
#
# @example Opt-out a single user
Expand All @@ -236,17 +236,17 @@ def unsubscribe(emails, opts = {})
# @param [String, Array<String>] email address(es) of people to opt-out.
# @return [Hash] contains 2 keys. :success contains the number of successful actions, :error a list of all errors.
def opt_out(emails)
emails = [*emails]
emails = [*emails]
subscribe(emails.map{|email| { :email => email }})
unsubscribe(emails, :send_goodbye => false, :send_notify => false)
end

private
def self.reserved_keys
[:email, :type, :double_optin, :update_existing, :replace_interests,
:send_welcome, :emails, :send_notify, :send_goodbye, :delete_member]
end

def subscribe_many(subscribers, opts = {})
if opts[:send_welcome]
subscribe_one_at_a_time(subscribers, opts)
Expand Down
34 changes: 26 additions & 8 deletions test/monkey_wrench/list_test.rb
Expand Up @@ -2,13 +2,31 @@
require "test_helper"

class MonkeyWrench::ListTest < Test::Unit::TestCase
context "finding a list" do
setup do
setup_config
end
context "finding a list by id" do
should "find a list by id" do
mock_chimp_post(:lists)
list = MonkeyWrench::List.find("my-list-id")
expected = MonkeyWrench::List.new(:id => "my-list-id")
assert_equal expected, list
end
should "return nil if the list doesn't exist" do
mock_chimp_post(:lists)
list = MonkeyWrench::List.find("imaginary-list-id")
assert_equal nil, list
end
end
end
context "subscribing to a list" do
setup do
setup_config
mock_chimp_post(:lists)
@list = MonkeyWrench::List.find_by_name("A test list")
end

context "multiple subscribers at once" do
should "subscribe users" do
form_params = {
Expand All @@ -18,11 +36,11 @@ class MonkeyWrench::ListTest < Test::Unit::TestCase
:id => "my-list-id"}
mock_chimp_post(:listBatchSubscribe, form_params)

subscribers = [{:email => "mail@chimp.com", :type => :html}]
expected = {:success => 1, :errors => []}
subscribers = [{:email => "mail@chimp.com", :type => :html}]
expected = {:success => 1, :errors => []}
assert_equal expected, @list.subscribe(subscribers)
end

should "split more than one thousand subscribers into batches" do
subscribers = (1..1004).map do |i|
{:email => "mail#{i}@chimp.com", :type => :html}
Expand All @@ -35,18 +53,18 @@ class MonkeyWrench::ListTest < Test::Unit::TestCase
expected = {:success => 1004, :errors => []}
assert_equal expected, @list.subscribe(subscribers)
end

should "send welcome email" do
form_params = {:merge_vars => {"FOO" => "bar"}, :id => "my-list-id",
:email_address => "mail@chimp.com", :type => "html",
:send_welcome => "true"}
mock_chimp_post(:listSubscribe, form_params)

subscribers = [{:email => "mail@chimp.com", :type => :html, :foo => "bar"}]
expected = {:success => 1, :errors => []}
subscribers = [{:email => "mail@chimp.com", :type => :html, :foo => "bar"}]
expected = {:success => 1, :errors => []}
assert_equal expected, @list.subscribe(subscribers, :send_welcome => true)
end

should "opt-out from list" do
end

Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Expand Up @@ -30,7 +30,7 @@ def uri_for_remote_method(remote_method)
"http://my-dc.api.mailchimp.com/1.2/?#{query_string}"
end

def mock_chimp_posts(remote_method, sequence)
def mock_chimp_posts(remote_method, sequence)
uri = uri_for_remote_method(remote_method)
sequence.each do |response|
response_body = canned_response(fixture_filename(response[:fixture] || remote_method, response[:is_success]))
Expand Down

0 comments on commit 6ac971a

Please sign in to comment.