Class: Hominid::Campaign

Inherits:
Base
  • Object
show all
Defined in:
lib/hominid/campaign.rb

Constant Summary

Constants inherited from Base

Base::MAILCHIMP_API_VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#add_api_key, #api_keys, #apply_defaults_to, #call, #clean_merge_tags, #expire_api_key

Constructor Details

#initialize(*args) ⇒ Campaign

Returns a new instance of Campaign.

Raises:



11
12
13
14
15
16
17
# File 'lib/hominid/campaign.rb', line 11

def initialize(*args)
  options = args.last.is_a?(Hash) ? args.last : {}
  raise StandardError.new('Please provide a Campaign ID.') unless options[:id]
  @campaign_id = options.delete(:id)
  @attributes = options.delete(:attributes)
  super(options)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



9
10
11
# File 'lib/hominid/campaign.rb', line 9

def attributes
  @attributes
end

#campaign_idObject (readonly)

Campaign related methods




8
9
10
# File 'lib/hominid/campaign.rb', line 8

def campaign_id
  @campaign_id
end

Class Method Details

.allObject



19
20
21
22
# File 'lib/hominid/campaign.rb', line 19

def self.all
  # Get all campaigns for this mailchimp account
  new(:id => 0).call("campaigns").to_a.collect { |c| Campaign.new(:id => c.delete('id'), :attributes => c) }
end

.create(type = 'regular', options = {}, content = {}, segment_options = {}, type_opts = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hominid/campaign.rb', line 67

def self.create(type = 'regular', options = {}, content = {}, segment_options = {}, type_opts = {})
  # Create a new campaign
  # The options hash should be structured as follows:
  #
  #   :list_id        = (string)  The ID of the list to send this campaign to.
  #   :subject        = (string)  The subject of the campaign.
  #   :from_email     = (string)  The email address this campaign will come from.
  #   :from_name      = (string)  The name that this campaign will come from.
  #   :to_email       = (string)  The To: name recipients will see.
  #   :template_id    = (integer) The ID of the template to use for this campaign (optional).
  #   :folder_id      = (integer) The ID of the folder to file this campaign in (optional).
  #   :tracking       = (array)   What to track for this campaign (optional).
  #   :title          = (string)  Internal title for this campaign (optional).
  #   :authenticate   = (boolean) Set to true to authenticate campaign (optional).
  #   :analytics      = (array)   Google analytics tags (optional).
  #   :auto_footer    = (boolean) Auto-generate the footer (optional)?
  #   :inline_css     = (boolean) Inline the CSS styles (optional)?
  #   :generate_text  = (boolean) Auto-generate text from HTML email (optional)?
  #
  # Visit http://www.mailchimp.com/api/1.2/campaigncreate.func.php for more information about creating
  # campaigns via the API.
  #
  new(:id => 0).call("campaignCreate", type, options, content, segment_options, type_opts)
  ## TODO: Return the new campaign with the ID returned from the API
end

.find(id_or_web_id) ⇒ Object



60
61
62
63
64
65
# File 'lib/hominid/campaign.rb', line 60

def self.find(id_or_web_id)
  # Campaign finder method
  all = self.all
  campaign = self.find_by_id(id_or_web_id.to_s).to_a + self.find_by_web_id(id_or_web_id.to_i).to_a
  return campaign.blank? ? nil : campaign.first
end

.find_by_id(id) ⇒ Object



55
56
57
58
# File 'lib/hominid/campaign.rb', line 55

def self.find_by_id(id)
  # Find campaign by id
  all.find { |campaign| (campaign.campaign_id == id.to_s) }
end

.find_by_list_id(list_id) ⇒ Object



28
29
30
31
# File 'lib/hominid/campaign.rb', line 28

def self.find_by_list_id(list_id)
  # Find all campaigns for the given list
  new(:id => 0).call("campaigns", {:list_id => list_id}).to_a.collect { |c| Campaign.new(:id=> c.delete('id'), :attributes => c) }
end

.find_by_list_name(list_name) ⇒ Object



24
25
26
# File 'lib/hominid/campaign.rb', line 24

def self.find_by_list_name(list_name)
  new(:id => 0).call("campaigns", {:list_id => List.find_by_name(list_name).list_id}).to_a.collect { |c| Campaign.new(:id=> c.delete('id'), :attributes => c) }
end

.find_by_title(title) ⇒ Object



33
34
35
36
# File 'lib/hominid/campaign.rb', line 33

def self.find_by_title(title)
  # Find campaign by title
  all.find { |c| c.attributes['title'] =~ /#{title}/ }
end

.find_by_type(type) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hominid/campaign.rb', line 38

def self.find_by_type(type)
  # Find campaign by type. Possible choices are:
  #   'regular'
  #   'plaintext'
  #   'absplit'
  #   'rss'
  #   'inspection'
  #   'trans'
  #   'auto'
  all.find { |campaign| campaign.attributes['type'] =~ /#{type}/ }
end

.find_by_web_id(web_id) ⇒ Object



50
51
52
53
# File 'lib/hominid/campaign.rb', line 50

def self.find_by_web_id(web_id)
  # Find campaigns by web_id
  all.find { |campaign| campaign.attributes['web_id'] == web_id.to_i }
end

.templatesObject



93
94
95
96
# File 'lib/hominid/campaign.rb', line 93

def self.templates
  # Get the templates for this account
  new(:id => 0).call("campaignTemplates")
end

Instance Method Details

#add_order(order) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/hominid/campaign.rb', line 98

def add_order(order)
  # Attach Ecommerce Order Information to a campaign.
  # The order hash should be structured as follows:
  #
  #   :id             = (string)  the order id
  #   :email_id       = (string)  email id of the subscriber (mc_eid query string)
  #   :total          = (double)  Show only campaigns with this from_name.
  #   :shipping       = (string)  *optional - the total paid for shipping fees.
  #   :tax            = (string)  *optional - the total tax paid.
  #   :store_id       = (string)  a unique id for the store sending the order in
  #   :store_name     = (string)  *optional - A readable name for the store, typicaly the hostname.
  #   :plugin_id      = (string)  the MailChimp-assigned Plugin Id. Using 1214 for the moment.
  #   :items          = (array)   the individual line items for an order, using the following keys:
  #
  #     :line_num      = (integer) *optional - line number of the item on the order
  #     :product_id    = (integer) internal product id
  #     :product_name  = (string)  the name for the product_id associated with the item
  #     :category_id   = (integer) internal id for the (main) category associated with product
  #     :category_name = (string)  the category name for the category id
  #     :qty           = (double)  the quantity of items ordered
  #     :cost          = (double)  the cost of a single item (i.e., not the extended cost of the line)
  order = order.merge(:campaign_id => @campaign_id)
  call("campaignEcommAddOrder", order)
end

#campaign_content(for_archive = true) ⇒ Object

Get the HTML & text content for a campaign :for_archive = (boolean) default true, true returns the content as it would appear in the archive, false returns the raw HTML/text



130
131
132
133
# File 'lib/hominid/campaign.rb', line 130

def campaign_content(for_archive = true)
  # Get the content of a campaign
  call("campaignContent", @campaign_id, for_archive)
end

#campaign_statsObject



123
124
125
126
# File 'lib/hominid/campaign.rb', line 123

def campaign_stats()
  # Get the stats of a campaign
  call("campaignStats", @campaign_id)
end

#delete_campaignObject



135
136
137
138
# File 'lib/hominid/campaign.rb', line 135

def delete_campaign()
  # Delete a campaign
  call("campaignDelete", @campaign_id)
end

#replicate_campaignObject



140
141
142
143
# File 'lib/hominid/campaign.rb', line 140

def replicate_campaign()
  # Replicate a campaign (returns ID of new campaign)
  call("campaignReplicate", @campaign_id)
end

#schedule_campaign(time = "#{1.day.from_now}") ⇒ Object



145
146
147
148
149
# File 'lib/hominid/campaign.rb', line 145

def schedule_campaign(time = "#{1.day.from_now}")
  # Schedule a campaign
  ## TODO: Add support for A/B Split scheduling
  call("campaignSchedule", @campaign_id, time)
end

#send_nowObject



151
152
153
154
# File 'lib/hominid/campaign.rb', line 151

def send_now()
  # Send a campaign
  call("campaignSendNow", @campaign_id)
end

#send_test(emails = {}) ⇒ Object

Send a test of a campaign



157
158
159
# File 'lib/hominid/campaign.rb', line 157

def send_test(emails = {})
  call("campaignSendTest", @campaign_id, emails)
end

#unscheduleObject



166
167
168
169
# File 'lib/hominid/campaign.rb', line 166

def unschedule()
  # Unschedule a campaign
  call("campaignUnschedule", @campaign_id)
end

#update(name, value) ⇒ Object



161
162
163
164
# File 'lib/hominid/campaign.rb', line 161

def update(name, value)
  # Update a campaign
  call("campaignUpdate", @campaign_id, name, value)
end