lib/createsend/campaign.rb in createsend-2.5.1 vs lib/createsend/campaign.rb in createsend-3.0.0

- old
+ new

@@ -1,15 +1,13 @@ -require 'createsend' -require 'json' - module CreateSend # Represents a campaign and provides associated funtionality. - class Campaign + class Campaign < CreateSend attr_reader :campaign_id - def initialize(campaign_id) + def initialize(auth, campaign_id) @campaign_id = campaign_id + super end # Creates a new campaign for a client. # client_id - String representing the ID of the client for whom the # campaign will be created. @@ -24,11 +22,11 @@ # content will be automatically generated from the HTML content. # list_ids - Array of Strings representing the IDs of the lists to # which the campaign will be sent. # segment_ids - Array of Strings representing the IDs of the segments to # which the campaign will be sent. - def self.create(client_id, subject, name, from_name, from_email, + def self.create(auth, client_id, subject, name, from_name, from_email, reply_to, html_url, text_url, list_ids, segment_ids) options = { :body => { :Subject => subject, :Name => name, :FromName => from_name, @@ -36,11 +34,12 @@ :ReplyTo => reply_to, :HtmlUrl => html_url, :TextUrl => text_url, :ListIDs => list_ids, :SegmentIDs => segment_ids }.to_json } - response = CreateSend.post "/campaigns/#{client_id}.json", options + cs = CreateSend.new auth + response = cs.post "/campaigns/#{client_id}.json", options response.parsed_response end # Creates a new campaign for a client, from a template. # client_id - String representing the ID of the client for whom the @@ -58,11 +57,11 @@ # the campaign will be based. # template_content - Hash representing the content to be used for the # editable areas of the template. See documentation at # campaignmonitor.com/api/campaigns/#creating_a_campaign_from_template # for full details of template content format. - def self.create_from_template(client_id, subject, name, from_name, + def self.create_from_template(auth, client_id, subject, name, from_name, from_email, reply_to, list_ids, segment_ids, template_id, template_content) options = { :body => { :Subject => subject, :Name => name, @@ -71,11 +70,12 @@ :ReplyTo => reply_to, :ListIDs => list_ids, :SegmentIDs => segment_ids, :TemplateID => template_id, :TemplateContent => template_content }.to_json } - response = CreateSend.post( + cs = CreateSend.new auth + response = cs.post( "/campaigns/#{client_id}/fromtemplate.json", options) response.parsed_response end # Sends a preview of this campaign. @@ -101,11 +101,11 @@ response = post "unschedule", options end # Deletes this campaign. def delete - response = CreateSend.delete "/campaigns/#{campaign_id}.json", {} + response = super "/campaigns/#{campaign_id}.json", {} end # Gets a summary of this campaign def summary response = get "summary", {} @@ -138,79 +138,61 @@ end # Retrieves the opens for this campaign. def opens(date="", page=1, page_size=1000, order_field="date", order_direction="asc") - options = { :query => { - :date => date, - :page => page, - :pagesize => page_size, - :orderfield => order_field, - :orderdirection => order_direction } } - response = get "opens", options - Hashie::Mash.new(response) + paged_result_by_date("opens", date, page, page_size, order_field, + order_direction) end # Retrieves the subscriber clicks for this campaign. def clicks(date="", page=1, page_size=1000, order_field="date", order_direction="asc") - options = { :query => { - :date => date, - :page => page, - :pagesize => page_size, - :orderfield => order_field, - :orderdirection => order_direction } } - response = get "clicks", options - Hashie::Mash.new(response) + paged_result_by_date("clicks", date, page, page_size, order_field, + order_direction) end # Retrieves the unsubscribes for this campaign. def unsubscribes(date="", page=1, page_size=1000, order_field="date", order_direction="asc") - options = { :query => { - :date => date, - :page => page, - :pagesize => page_size, - :orderfield => order_field, - :orderdirection => order_direction } } - response = get "unsubscribes", options - Hashie::Mash.new(response) + paged_result_by_date("unsubscribes", date, page, page_size, order_field, + order_direction) end # Retrieves the spam complaints for this campaign. def spam(date="", page=1, page_size=1000, order_field="date", order_direction="asc") - options = { :query => { - :date => date, - :page => page, - :pagesize => page_size, - :orderfield => order_field, - :orderdirection => order_direction } } - response = get "spam", options - Hashie::Mash.new(response) + paged_result_by_date("spam", date, page, page_size, order_field, + order_direction) end # Retrieves the bounces for this campaign. def bounces(date="", page=1, page_size=1000, order_field="date", order_direction="asc") + paged_result_by_date("bounces", date, page, page_size, order_field, + order_direction) + end + + private + + def paged_result_by_date(resource, date, page, page_size, order_field, + order_direction) options = { :query => { :date => date, :page => page, :pagesize => page_size, :orderfield => order_field, :orderdirection => order_direction } } - response = get "bounces", options + response = get resource, options Hashie::Mash.new(response) end - private - def get(action, options = {}) - CreateSend.get uri_for(action), options + super uri_for(action), options end def post(action, options = {}) - CreateSend.post uri_for(action), options + super uri_for(action), options end def uri_for(action) "/campaigns/#{campaign_id}/#{action}.json" end \ No newline at end of file