Class: CampaignMonitor
- Inherits:
-
Object
- Object
- CampaignMonitor
- Includes:
- Helpers
- Defined in:
- lib/campaign_monitor.rb,
lib/campaign_monitor/list.rb,
lib/campaign_monitor/client.rb,
lib/campaign_monitor/result.rb,
lib/campaign_monitor/helpers.rb,
lib/campaign_monitor/campaign.rb,
lib/campaign_monitor/subscriber.rb
Defined Under Namespace
Modules: Helpers Classes: Campaign, Client, List, Result, Subscriber, SubscriberBounce, SubscriberClick, SubscriberOpen, SubscriberUnsubscribe
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#api_url ⇒ Object
readonly
Returns the value of attribute api_url.
Instance Method Summary collapse
-
#add_subscriber(list_id, email, name) ⇒ Object
A quick method of adding a subscriber to a list.
-
#campaigns(client_id) ⇒ Object
Returns an array of Campaign objects associated with the specified Client ID.
-
#clients ⇒ Object
Returns an array of Client objects associated with the API Key.
-
#http_get(url) ⇒ Object
Does an HTTP GET on a given URL and returns the response body.
-
#initialize(api_key = CAMPAIGN_MONITOR_API_KEY) ⇒ CampaignMonitor
constructor
Replace this API key with your own (www.campaignmonitor.com/api/).
-
#lists(client_id) ⇒ Object
Returns an array of Subscriber Lists for the specified Client ID.
-
#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.
- #parsed_system_date ⇒ Object
-
#request(method, params) ⇒ Object
Takes a CampaignMonitor API method name and set of parameters; returns an XmlSimple object with the response.
-
#request_url(method, params = {}) ⇒ Object
Takes a CampaignMonitor API method name and set of parameters; returns the correct URL for the REST API.
- #system_date ⇒ Object
- #using_soap ⇒ Object
Methods included from Helpers
#formatted_timestamp, #handle_response, #timestamp_format
Constructor Details
#initialize(api_key = CAMPAIGN_MONITOR_API_KEY) ⇒ CampaignMonitor
Replace this API key with your own (www.campaignmonitor.com/api/)
79 80 81 82 |
# File 'lib/campaign_monitor.rb', line 79 def initialize(api_key=CAMPAIGN_MONITOR_API_KEY) @api_key = api_key @api_url = 'http://api.createsend.com/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
113 114 115 |
# File 'lib/campaign_monitor.rb', line 113 def method_missing(method_id, params = {}) request(method_id.id2name.gsub(/_/, '.'), params) end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
76 77 78 |
# File 'lib/campaign_monitor.rb', line 76 def api_key @api_key end |
#api_url ⇒ Object (readonly)
Returns the value of attribute api_url.
76 77 78 |
# File 'lib/campaign_monitor.rb', line 76 def api_url @api_url 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.succeeded?
puts "Subscriber Added to List"
end
179 180 181 |
# File 'lib/campaign_monitor.rb', line 179 def add_subscriber(list_id, email, name) Result.new(Subscriber_Add("ListID" => list_id, "Email" => email, "Name" => name)) 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
149 150 151 152 153 |
# File 'lib/campaign_monitor.rb', line 149 def campaigns(client_id) 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 |
#clients ⇒ Object
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
126 127 128 129 130 |
# File 'lib/campaign_monitor.rb', line 126 def clients handle_response(User_GetClients()) do |response| response["Client"].collect{|c| Client.new(c["ClientID"], c["Name"])} end end |
#http_get(url) ⇒ Object
Does an HTTP GET on a given URL and returns the response body
107 108 109 |
# File 'lib/campaign_monitor.rb', line 107 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
164 165 166 167 168 |
# File 'lib/campaign_monitor.rb', line 164 def lists(client_id) handle_response(Client_GetLists("ClientID" => client_id)) do |response| response["List"].collect{|l| List.new(l["ListID"], l["Name"])} end end |
#parsed_system_date ⇒ Object
136 137 138 |
# File 'lib/campaign_monitor.rb', line 136 def parsed_system_date DateTime.strptime(system_date, ) end |
#request(method, params) ⇒ Object
Takes a CampaignMonitor API method name and set of parameters; returns an XmlSimple object with the response
87 88 89 90 91 92 93 |
# File 'lib/campaign_monitor.rb', line 87 def request(method, params) response = PARSER.xml_in(http_get(request_url(method, params)), { 'keeproot' => false, 'forcearray' => %w[List Campaign Subscriber Client SubscriberOpen SubscriberUnsubscribe SubscriberClick SubscriberBounce], 'noattr' => true }) response.delete('d1p1:type') 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.
96 97 98 99 100 101 102 103 104 |
# File 'lib/campaign_monitor.rb', line 96 def request_url(method, params={}) params.merge!('ApiKey' => api_key) query = params.collect do |key, value| "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}" end.sort * '&' "#{api_url}/#{method}?#{query}" end |
#system_date ⇒ Object
132 133 134 |
# File 'lib/campaign_monitor.rb', line 132 def system_date User_GetSystemDate() end |
#using_soap ⇒ Object
183 184 185 186 187 188 189 |
# File 'lib/campaign_monitor.rb', line 183 def using_soap driver = wsdl_driver_factory.create_rpc_driver response = yield(driver) driver.reset_stream response end |