Class: CampaignMonitor

Inherits:
Object
  • Object
show all
Defined in:
lib/campaign_monitor.rb

Defined Under Namespace

Classes: Campaign, Client, List, Result, Subscriber, SubscriberBounce, SubscriberClick, SubscriberOpen, SubscriberUnsubscribe

Instance Method Summary collapse

Constructor Details

#initialize(api_key = CAMPAIGN_MONITOR_API_KEY) ⇒ CampaignMonitor

Replace this API key with your own (www.campaignmonitor.com/api/)



65
66
67
68
69
# File 'lib/campaign_monitor.rb', line 65

def initialize(api_key=CAMPAIGN_MONITOR_API_KEY)
 @api_key = api_key
 @host = 'http://app.campaignmonitor.com'
 @api = '/api/api.asmx/'
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *params) ⇒ Object

By overriding the method_missing method, it is possible to easily support all of the methods available in the API



92
93
94
# File 'lib/campaign_monitor.rb', line 92

def method_missing(method_id, *params)
  request(method_id.id2name.gsub(/_/, '.'), params[0])
end

Instance Method Details

#add_subscriber(list_id, email, name) ⇒ Object

A quick method of adding a subscriber to a list. Returns a Result object

Example

@cm = CampaignMonitor.new()
result = @cm.add_subscriber(12345, "[email protected]", "Ralph Wiggum")

if result.code == 0
  puts "Subscriber Added to List"
end


159
160
161
162
# File 'lib/campaign_monitor.rb', line 159

def add_subscriber(list_id, email, name)
  response = Subscriber_Add("ListID" => list_id, "Email" => email, "Name" => name)
  Result.new(response["Message"], response["Code"].to_i)
end

#campaigns(client_id) ⇒ Object

Returns an array of Campaign objects associated with the specified Client ID

Example

@cm = CampaignMonitor.new()
@campaigns = @cm.campaigns(12345)

for campaign in @campaigns
  puts campaign.subject
end


123
124
125
126
127
128
129
130
# File 'lib/campaign_monitor.rb', line 123

def campaigns(client_id)
  response = Client_GetCampaigns("ClientID" => client_id)
  unless response["Code"].to_i != 0 
    response["Campaign"].collect{|c| Campaign.new(c["CampaignID"].to_i, c["Subject"], c["SentDate"], c["TotalRecipients"].to_i)}
  else
    raise response["Code"] + " - " + response["Message"]
  end
end

#clientsObject

Returns an array of Client objects associated with the API Key

Example

@cm = CampaignMonitor.new()
@clients = @cm.clients

for client in @clients
  puts client.name
end


105
106
107
108
109
110
111
112
# File 'lib/campaign_monitor.rb', line 105

def clients
  response = User_GetClients()
  unless response["Code"].to_i != 0 
    response["Client"].collect{|c| Client.new(c["ClientID"].to_i, c["Name"])}
  else
    raise response["Code"] + " - " + response["Message"]
  end
end

#http_get(url) ⇒ Object

Does an HTTP GET on a given URL and returns the response body



86
87
88
# File 'lib/campaign_monitor.rb', line 86

def http_get(url)
  Net::HTTP.get_response(URI.parse(url)).body.to_s
end

#lists(client_id) ⇒ Object

Returns an array of Subscriber Lists for the specified Client ID

Example

@cm = CampaignMonitor.new()
@lists = @cm.lists(12345)

for list in @lists
  puts list.name
end


141
142
143
144
145
146
147
148
# File 'lib/campaign_monitor.rb', line 141

def lists(client_id)
  response = Client_GetLists("ClientID" => client_id)
  unless response["Code"].to_i != 0 
    response["List"].collect{|l| List.new(l["ListID"].to_i, l["Name"])}
  else
    raise response["Code"] + " - " + response["Message"]
  end
end

#request(method, *params) ⇒ Object

Takes a CampaignMonitor API method name and set of parameters; returns an XmlSimple object with the response



73
74
75
76
# File 'lib/campaign_monitor.rb', line 73

def request(method, *params)
  response = XmlSimple.xml_in(http_get(request_url(method, params)), { 'ForceArray' => false, 'ForceArray' => %r(List$|Campaign$|Subscriber$|Client$|SubscriberOpen$|SubscriberUnsubscribe$|SubscriberClick$|SubscriberBounce$), 'NoAttr' => true })
  response
end

#request_url(method, *params) ⇒ Object

Takes a CampaignMonitor API method name and set of parameters; returns the correct URL for the REST API.



79
80
81
82
83
# File 'lib/campaign_monitor.rb', line 79

def request_url(method, *params)
  url = "#{@host}#{@api}/#{method}?ApiKey=#{@api_key}"
  params[0][0].each_key do |key| url += "&#{key}=" + CGI::escape(params[0][0][key].to_s) end if params[0][0]
  url
end