From f93d54083b3a078152845f219d9a238dba85420b Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Fri, 20 Feb 2009 03:32:06 -0500 Subject: [PATCH] new list api and tests --- lib/campaign_monitor.rb | 2 +- lib/campaign_monitor/client.rb | 2 +- lib/campaign_monitor/list.rb | 65 ++++++++++++++++++------- test/campaign_monitor_test.rb | 48 +++---------------- test/list_test.rb | 88 ++++++++++++++++++++++++++++++++++ 5 files changed, 146 insertions(+), 59 deletions(-) create mode 100644 test/list_test.rb diff --git a/lib/campaign_monitor.rb b/lib/campaign_monitor.rb index 893eee3..901d504 100644 --- a/lib/campaign_monitor.rb +++ b/lib/campaign_monitor.rb @@ -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 diff --git a/lib/campaign_monitor/client.rb b/lib/campaign_monitor/client.rb index aa5ae4c..118221f 100644 --- a/lib/campaign_monitor/client.rb +++ b/lib/campaign_monitor/client.rb @@ -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 diff --git a/lib/campaign_monitor/list.rb b/lib/campaign_monitor/list.rb index bf59222..758ea6d 100644 --- a/lib/campaign_monitor/list.rb +++ b/lib/campaign_monitor/list.rb @@ -6,24 +6,47 @@ 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 @@ -31,11 +54,21 @@ 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 @@ -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 diff --git a/test/campaign_monitor_test.rb b/test/campaign_monitor_test.rb index 35013c5..6586a6d 100644 --- a/test/campaign_monitor_test.rb +++ b/test/campaign_monitor_test.rb @@ -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 @@ -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 @@ -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 diff --git a/test/list_test.rb b/test/list_test.rb new file mode 100644 index 0000000..fc858e4 --- /dev/null +++ b/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 \ No newline at end of file