Skip to content
This repository has been archived by the owner on Aug 9, 2022. It is now read-only.

Commit

Permalink
new list api and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed Feb 20, 2009
1 parent 60765aa commit f93d540
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 59 deletions.
2 changes: 1 addition & 1 deletion lib/campaign_monitor.rb
Expand Up @@ -166,7 +166,7 @@ def campaigns(client_id)
# end
def lists(client_id)
handle_response(Client_GetLists("ClientID" => client_id)) do |response|
response["List"].collect{|l| List.new(l["ListID"], l["Name"])}
response["List"].collect{|l| List.new(l)}
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/campaign_monitor/client.rb
Expand Up @@ -14,7 +14,7 @@ def initialize(id, name=nil)
end

def new_list
List.new(nil,nil,:ClientID => id)
List.new(:ClientID => id)
end

# Example
Expand Down
65 changes: 49 additions & 16 deletions lib/campaign_monitor/list.rb
Expand Up @@ -6,36 +6,69 @@ class CampaignMonitor
class List < Base
include CampaignMonitor::Helpers

attr_reader :id, :cm_client, :result
attr_reader :cm_client, :result

# Example
# @list = new List(12345)
def initialize(id=nil, name=nil,attrs={})
defaults={"ConfirmOptIn" => "false",
"UnsubscribePage" => "",
"ConfirmationSuccessPage" => ""}
def initialize(attrs={})
super
@id = id
@name = name
@attributes=defaults.merge(attrs)
self["Title"]=name if name # override for now
@attributes=attrs
end

# compatible with previous API
def name
self["Title"]
self["Title"] || self["Name"]
end

def id
self["ListID"]
end

# Example
#
# @list = @client.new_list.defaults

def defaults
defaults={"ConfirmOptIn" => "false",
"UnsubscribePage" => "",
"ConfirmationSuccessPage" => ""}
@attributes=defaults.merge(@attributes)
self
end

def []=(k,v)
if %w{Title Name}.include?(k)
super("Title", v)
super("Name", v)
else
super(k,v)
end
end

def id=(v)
self["ListID"]=v
end

# AR like
def save
id ? Update : Create
end

def GetDetail(overwrite=false)
raw=cm_client.List_GetDetail("ListID" => id)
@attributes=raw.merge(@attributes)
@attributes.merge!(raw) if overwrite
end

def Update
# TODO: need to make sure we've loaded the full record with List_GetDetail
# so that we have the full component of attributes to write back to the API
@attributes["ListID"] ||= id
@result=Result.new(List_Update(@attributes))
# if we're dealing with a half baked object that Client#lists has given
# us then we need to popular all the fields before we can attempt an update
unless @fully_baked
self.GetDetail
@fully_baked=true
end
@result=Result.new(cm_client.List_Update(@attributes))
@result.success?
end

def Delete
Expand All @@ -46,8 +79,8 @@ def Delete
def Create
raw=cm_client.List_Create(@attributes)
@result=Result.new(raw)
@id = raw["__content__"] if raw["__content__"]
@id ? true : false
self.id = raw["__content__"] if raw["__content__"]
id ? true : false
end

# Example
Expand Down
48 changes: 7 additions & 41 deletions test/campaign_monitor_test.rb
Expand Up @@ -12,15 +12,9 @@ def setup
@cm = CampaignMonitor.new(ENV["API_KEY"] || CAMPAIGN_MONITOR_API_KEY)
# find an existing client
@client=find_test_client
assert_not_nil @client, "Please create a '#{CLIENT_NAME}' client so tests can run."
# create one list for that client
response = @cm.List_Create(build_new_list("ClientID" => @client.id))
@list_id=response["__content__"]
assert_not_nil @client, "Please create a '#{CLIENT_NAME}' (company name) client so tests can run."
end

def teardown
response = @cm.List_Delete("ListID" => @list_id)
end

# def test_create_and_delete_client
# before=@cm.clients.size
Expand All @@ -39,36 +33,13 @@ def test_find_existing_client_by_name
assert clients.map {|c| c.name}.include?(CLIENT_NAME), "could not find client named: #{CLIENT_NAME}"
end

def test_create_list
list = @client.new_list
list["Title"]="This a new list"
assert list.Create
assert_success list.result
assert_not_nil list.id
assert_equal 0, list.active_subscribers(Date.new(2005,1,1)).size
assert list.Delete
assert_success list.result
end

def test_lists
lists = @client.lists
assert lists.size > 0

assert lists.map {|l| l.name}.include?(LIST_NAME), "could not find list named: #{LIST_NAME}"
end

def test_list_add_subscriber
list = find_test_list
assert_equal 0, list.active_subscribers(Date.new(2005,1,1)).size
assert_success list.add_and_resubscribe('a@test.com', 'Test A')
assert_equal 1, list.active_subscribers(Date.new(2005,1,1)).size
assert_success list.remove_subscriber('a@test.com')
end
# campaigns

def test_campaigns
client = find_test_client
assert client.campaigns.size > 0, "should have one campaign"
end
# def test_campaigns
# client = find_test_client
# assert client.campaigns.size > 0, "should have one campaign"
# end


protected
Expand All @@ -79,12 +50,7 @@ def build_new_client(options={})
}.merge(options)
end

def build_new_list(options={})
{"Title" => "List #1", "ConfirmOptIn" => "false",
"UnsubscribePage" => "",
"ConfirmationSuccessPage" => ""}.merge(options)
end


def assert_success(result)
assert result.succeeded?, result.message
end
Expand Down
88 changes: 88 additions & 0 deletions test/list_test.rb
@@ -0,0 +1,88 @@
require 'rubygems'
require 'campaign_monitor'
require 'test/unit'

CAMPAIGN_MONITOR_API_KEY = 'Your API Key'
CLIENT_NAME = 'Spacely Space Sprockets'
LIST_NAME = 'List #1'

class CampaignMonitorTest < Test::Unit::TestCase

def setup
@cm = CampaignMonitor.new(ENV["API_KEY"] || CAMPAIGN_MONITOR_API_KEY)
# find an existing client
@client=find_test_client
assert_not_nil @client, "Please create a '#{CLIENT_NAME}' (company name) client so tests can run."

@list = @client.new_list.defaults
@list["Title"]="List #1"
assert @list.Create
end

def teardown
@list.Delete
end

def test_create_and_delete_list
list = @client.new_list.defaults
list["Title"]="This is a new list"
assert list.Create
assert_success list.result
assert_not_nil list.id
assert_equal 0, list.active_subscribers(Date.new(2005,1,1)).size
saved_id=list.id
# find it all over again
list=@client.lists.detect { |x| x.name == "This is a new list" }
assert_equal saved_id, list.id
assert list.Delete
assert_success list.result
# should be gone now
assert_nil @client.lists.detect { |x| x.name == "This is a new list" }
end

def test_update_list
list=@client.lists.first
assert_equal "List #1", list.name
list["Name"]="Just another list"
list.Update
list=@client.lists.first
assert_equal "Just another list", list.name
end

def test_getinfo_list
list=@client.lists.first
assert_equal "List #1", list.name
assert_nil list["ConfirmOptIn"]
list.GetDetail
assert_equal "false", list["ConfirmOptIn"]
end

def test_list_add_subscriber
list=@client.lists.first
assert_equal 0, list.active_subscribers(Date.new(2005,1,1)).size
assert_success list.add_and_resubscribe('a@test.com', 'Test A')
assert_equal 1, list.active_subscribers(Date.new(2005,1,1)).size
assert_success list.remove_subscriber('a@test.com')
end


protected

def build_new_list(options={})
{"Title" => "List #1", "ConfirmOptIn" => "false",
"UnsubscribePage" => "",
"ConfirmationSuccessPage" => ""}.merge(options)
end

def assert_success(result)
assert result.succeeded?, result.message
end

def find_test_client(clients=@cm.clients)
clients.detect { |c| c.name == CLIENT_NAME }
end

def find_test_list(lists=find_test_client.lists)
lists.detect { |l| l.name == LIST_NAME }
end
end

0 comments on commit f93d540

Please sign in to comment.