From 60765aa3fcfa5184727d7d8d7cae9b1da311858e Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Fri, 20 Feb 2009 02:43:45 -0500 Subject: [PATCH] * move to more of an AR like attributes model where we can set and access attributes with [] * more the core API methods down into the list class (as a trial) so you can do things like: @client.new_list and then later @list.Create @list.Delete @list.Update @list.GetDetail --- lib/campaign_monitor/base.rb | 9 ++++++++ lib/campaign_monitor/client.rb | 4 ++++ lib/campaign_monitor/list.rb | 40 +++++++++++++++++++++++++++++++--- lib/campaign_monitor/result.rb | 4 +++- test/campaign_monitor_test.rb | 15 ++++++++----- 5 files changed, 63 insertions(+), 9 deletions(-) diff --git a/lib/campaign_monitor/base.rb b/lib/campaign_monitor/base.rb index 67e3278..a15a48f 100644 --- a/lib/campaign_monitor/base.rb +++ b/lib/campaign_monitor/base.rb @@ -12,7 +12,16 @@ def self.client=(a) @@client=a end + def [](k) + @attributes[k] + end + + def []=(k,v) + @attributes[k]=v + end + def initialize(*args) + @attributes={} @cm_client=@@client end end diff --git a/lib/campaign_monitor/client.rb b/lib/campaign_monitor/client.rb index aa80faf..aa5ae4c 100644 --- a/lib/campaign_monitor/client.rb +++ b/lib/campaign_monitor/client.rb @@ -13,6 +13,10 @@ def initialize(id, name=nil) super end + def new_list + List.new(nil,nil,:ClientID => id) + end + # Example # @client = new Client(12345) # @lists = @client.lists diff --git a/lib/campaign_monitor/list.rb b/lib/campaign_monitor/list.rb index 2c63032..bf59222 100644 --- a/lib/campaign_monitor/list.rb +++ b/lib/campaign_monitor/list.rb @@ -6,15 +6,49 @@ class CampaignMonitor class List < Base include CampaignMonitor::Helpers - attr_reader :id, :name, :cm_client + attr_reader :id, :cm_client, :result # Example # @list = new List(12345) - def initialize(id=nil, name=nil) + def initialize(id=nil, name=nil,attrs={}) + defaults={"ConfirmOptIn" => "false", + "UnsubscribePage" => "", + "ConfirmationSuccessPage" => ""} + super @id = id @name = name - super + @attributes=defaults.merge(attrs) + self["Title"]=name if name # override for now + end + + # compatible with previous API + def name + self["Title"] + end + + # AR like + def save + id ? Update : Create 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)) + end + + def Delete + @result=Result.new(cm_client.List_Delete("ListID" => id)) + @result.success? + end + + def Create + raw=cm_client.List_Create(@attributes) + @result=Result.new(raw) + @id = raw["__content__"] if raw["__content__"] + @id ? true : false + end # Example # @list = new List(12345) diff --git a/lib/campaign_monitor/result.rb b/lib/campaign_monitor/result.rb index 7da9810..1226d41 100644 --- a/lib/campaign_monitor/result.rb +++ b/lib/campaign_monitor/result.rb @@ -8,10 +8,12 @@ def initialize(response) @code = response["Code"].to_i end - def succeeded? + def success? code == 0 end + alias :succeeded? :success? + def failed? !succeeded? end diff --git a/test/campaign_monitor_test.rb b/test/campaign_monitor_test.rb index 15c98a2..35013c5 100644 --- a/test/campaign_monitor_test.rb +++ b/test/campaign_monitor_test.rb @@ -39,11 +39,16 @@ 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_success list.create - # 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