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

Commit

Permalink
* move to more of an AR like attributes model where we can set and ac…
Browse files Browse the repository at this point in the history
…cess 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
  • Loading branch information
joshgoebel committed Feb 20, 2009
1 parent be5caf4 commit 60765aa
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 9 deletions.
9 changes: 9 additions & 0 deletions lib/campaign_monitor/base.rb
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/campaign_monitor/client.rb
Expand Up @@ -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
Expand Down
40 changes: 37 additions & 3 deletions lib/campaign_monitor/list.rb
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion lib/campaign_monitor/result.rb
Expand Up @@ -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
Expand Down
15 changes: 10 additions & 5 deletions test/campaign_monitor_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 60765aa

Please sign in to comment.