lib/campaign.rb in createsend-0.0.1 vs lib/campaign.rb in createsend-0.0.2

- old
+ new

@@ -1,77 +1,121 @@ require 'createsend' require 'json' +# Represents a campaign and provides associated funtionality. class Campaign attr_reader :campaign_id def initialize(campaign_id) @campaign_id = campaign_id end + # Creates a new campaign for a client. def self.create(client_id, subject, name, from_name, from_email, reply_to, html_url, - text_url, list_ids, segments) + text_url, list_ids, segment_ids) options = { :body => { :Subject => subject, :Name => name, :FromName => from_name, :FromEmail => from_email, :ReplyTo => reply_to, :HtmlUrl => html_url, :TextUrl => text_url, - :ListIDs => list_ids , - :Segments => segments }.to_json } + :ListIDs => list_ids, + :SegmentIDs => segment_ids }.to_json } response = CreateSend.post "/campaigns/#{client_id}.json", options response.parsed_response end - + + # Sends a preview of this campaign. + def send_preview(recipients, personalize="fallback") + options = { :body => { + :PreviewRecipients => recipients.kind_of?(String) ? [ recipients ] : recipients, + :Personalize => personalize }.to_json } + response = post "sendpreview", options + end + + # Sends this campaign. def send(confirmation_email, send_date="immediately") options = { :body => { :ConfirmationEmail => confirmation_email, :SendDate => send_date }.to_json } response = post "send", options end + # Deletes this campaign. def delete response = CreateSend.delete "/campaigns/#{campaign_id}.json", {} end - + + # Gets a summary of this campaign def summary response = get "summary", {} Hashie::Mash.new(response) end - - def lists - response = get "lists", {} - response.map{|item| Hashie::Mash.new(item)} + + # Retrieves the lists and segments to which this campaaign will be (or was) sent. + def lists_and_segments + response = get "listsandsegments", {} + Hashie::Mash.new(response) end - - def segments - # TODO: This needs to be implemented - [] + + # Retrieves the recipients of this campaign. + def recipients(page=1, page_size=1000, order_field="email", order_direction="asc") + options = { :query => { + :page => page, + :pagesize => page_size, + :orderfield => order_field, + :orderdirection => order_direction } } + response = get 'recipients', options + Hashie::Mash.new(response) end - - def opens(date) - options = { :query => { :date => date } } + + # 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 - response.map{|item| Hashie::Mash.new(item)} + Hashie::Mash.new(response) end - - def clicks(date) - options = { :query => { :date => date } } + + # 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 - response.map{|item| Hashie::Mash.new(item)} + Hashie::Mash.new(response) end - - def unsubscribes(date) - options = { :query => { :date => date } } + + # 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 - response.map{|item| Hashie::Mash.new(item)} + Hashie::Mash.new(response) end - - def bounces - response = get "bounces", {} - response.map{|item| Hashie::Mash.new(item)} + + # Retrieves the bounces for this campaign. + def bounces(page=1, page_size=1000, order_field="date", order_direction="asc") + options = { :query => { + :page => page, + :pagesize => page_size, + :orderfield => order_field, + :orderdirection => order_direction } } + response = get "bounces", options + Hashie::Mash.new(response) end private def get(action, options = {})