Skip to content

Commit

Permalink
Some refactoring to DRY up Client class. Added some content to readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyw committed Jan 7, 2009
1 parent d79a74c commit 3245628
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 27 deletions.
31 changes: 24 additions & 7 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
= campaign_monitor

This library provides access to the Campaign Monitor API (http://www.campaignmonitor.com/api)
This RubyGem provides access to the Campaign Monitor API (http://www.campaignmonitor.com/api).

== Pre-requisites

An account with Campaign Monitor and the API Key. Accounts are free and can be obtained from
http://www.campaignmonitor.com
An account with Campaign Monitor and the API Key. Accounts are free and can be created at
http://www.campaignmonitor.com.

== Resources

Install
=== Install
+gem install patientslikeme-campaign_monitor+

* gem install patientslikeme-campaign_monitor
=== Git Repository
http://github.com/patientslikeme/campaign_monitor

Git Repository

* http://github.com/patientslikeme/campaign_monitor
== Usage

cm = CampaignMonitor.new # assumes you've set CAMPAIGN_MONITOR_API_KEY in your project

for client in cm.clients
for list in client.lists
# modify a subscriber list
list.add_subscriber(email, name, custom_fields_hash)
list.remove_subscriber(email)
list.add_and_resubscribe(email, name, custom_fields_hash)

# get subscriber list details
subscribers = list.active_subscribers(since_time)
unsubscribed = list.unsubscribed(since_time)
bounced = list.bounced(since_time)
end
end
14 changes: 4 additions & 10 deletions lib/campaign_monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,13 @@ def method_missing(method_id, params = {})
# puts client.name
# end
def clients
response = User_GetClients()
handle_response(response) do
handle_response(User_GetClients()) do |response|
response["Client"].collect{|c| Client.new(c["ClientID"], c["Name"])}
end
end

def system_date
response = User_GetSystemDate()
handle_response(response) do
response
end
User_GetSystemDate()
end

def parsed_system_date
Expand All @@ -143,8 +139,7 @@ def parsed_system_date
# puts campaign.subject
# end
def campaigns(client_id)
response = Client_GetCampaigns("ClientID" => client_id)
handle_response(response) do
handle_response(Client_GetCampaigns("ClientID" => client_id)) do |response|
response["Campaign"].collect{|c| Campaign.new(c["CampaignID"], c["Subject"], c["SentDate"], c["TotalRecipients"].to_i)}
end
end
Expand All @@ -159,8 +154,7 @@ def campaigns(client_id)
# puts list.name
# end
def lists(client_id)
response = Client_GetLists("ClientID" => client_id)
handle_response(response) do
handle_response(Client_GetLists("ClientID" => client_id)) do |response|
response["List"].collect{|l| List.new(l["ListID"], l["Name"])}
end
end
Expand Down
8 changes: 2 additions & 6 deletions lib/campaign_monitor/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ def initialize(id, name=nil)
# puts list.name
# end
def lists
handle_response(cm_client.Client_GetLists("ClientID" => self.id)) do |response|
response["List"].collect{|l| List.new(l["ListID"], l["Name"])}
end
cm_client.lists(self.id)
end

# Example
Expand All @@ -34,9 +32,7 @@ def lists
# puts campaign.subject
# end
def campaigns
handle_response(cm_client.Client_GetCampaigns("ClientID" => self.id)) do |response|
response["Campaign"].collect{|c| Campaign.new(c["CampaignID"], c["Subject"], c["SentDate"], c["TotalRecipients"].to_i)}
end
cm_client.campaigns(self.id)
end
end
end
9 changes: 5 additions & 4 deletions lib/campaign_monitor/subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Subscriber
include Helpers

attr_accessor :email_address, :name, :date_subscribed
attr_reader :cm_client

def initialize(email_address, name=nil, date=nil)
@email_address = email_address
Expand All @@ -16,25 +17,25 @@ def initialize(email_address, name=nil, date=nil)
# @subscriber = Subscriber.new("ralph.wiggum@simpsons.net")
# @subscriber.add(12345)
def add(list_id)
Result.new(@cm_client.Subscriber_Add("ListID" => list_id, "Email" => @email_address, "Name" => @name))
Result.new(cm_client.Subscriber_Add("ListID" => list_id, "Email" => @email_address, "Name" => @name))
end

# Example
# @subscriber = Subscriber.new("ralph.wiggum@simpsons.net")
# @subscriber.add_and_resubscribe(12345)
def add_and_resubscribe(list_id)
Result.new(@cm_client.Subscriber_AddAndResubscribe("ListID" => list_id, "Email" => @email_address, "Name" => @name))
Result.new(cm_client.Subscriber_AddAndResubscribe("ListID" => list_id, "Email" => @email_address, "Name" => @name))
end

# Example
# @subscriber = Subscriber.new("ralph.wiggum@simpsons.net")
# @subscriber.unsubscribe(12345)
def unsubscribe(list_id)
Result.new(@cm_client.Subscriber_Unsubscribe("ListID" => list_id, "Email" => @email_address))
Result.new(cm_client.Subscriber_Unsubscribe("ListID" => list_id, "Email" => @email_address))
end

def is_subscribed?(list_id)
result = @cm_client.Subscribers_GetIsSubscribed("ListID" => list_id, "Email" => @email_address)
result = cm_client.Subscribers_GetIsSubscribed("ListID" => list_id, "Email" => @email_address)
return true if result == 'True'
return false if result == 'False'
raise "Invalid value for is_subscribed?: #{result}"
Expand Down

0 comments on commit 3245628

Please sign in to comment.