Class: MadMimi

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/madmimi.rb

Constant Summary collapse

MadMimiError =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(username, api_key, options = {}) ⇒ MadMimi

Returns a new instance of MadMimi.



63
64
65
66
67
68
69
70
# File 'lib/madmimi.rb', line 63

def initialize(username, api_key, options = {})
  @api_settings = options.reverse_merge({
    :verify_ssl => true
  }).merge({
    :username   => username,
    :api_key    => api_key
  })
end

Instance Method Details

#add_to_list(email, list_name, options = {}) ⇒ Object



127
128
129
# File 'lib/madmimi.rb', line 127

def add_to_list(email, list_name, options={})
  do_request(path(:add_to_list, :list => list_name), :post, options.merge(:email => email))
end

#add_user(hash_or_array) ⇒ Object Also known as: add_users



121
122
123
# File 'lib/madmimi.rb', line 121

def add_user(hash_or_array)
  csv_import(build_csv(hash_or_array))
end

#add_users_to_list(list_name, arr) ⇒ Object



197
198
199
# File 'lib/madmimi.rb', line 197

def add_users_to_list(list_name, arr)
  add_users(arr.map{ |a| a[:add_list] = list_name; a })
end

#api_keyObject



76
77
78
# File 'lib/madmimi.rb', line 76

def api_key
  @api_settings[:api_key]
end

#audience_search(query_string, raw = false) ⇒ Object



193
194
195
# File 'lib/madmimi.rb', line 193

def audience_search(query_string, raw = false)
  do_request(path(:search), :get, :raw => raw, :query => query_string)
end

#csv_import(csv_string) ⇒ Object



117
118
119
# File 'lib/madmimi.rb', line 117

def csv_import(csv_string)
  do_request(path(:audience_members), :post, :csv_file => csv_string)
end

#delete_list(list_name) ⇒ Object



113
114
115
# File 'lib/madmimi.rb', line 113

def delete_list(list_name)
  do_request(path(:destroy_list, :list => list_name), :delete)
end

#list_members(list_name, page = 1, per_page = 30) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/madmimi.rb', line 149

def list_members(list_name, page = 1, per_page = 30)
  wrap_with_array('audience', 'member') do
    do_request(path(:audience_list_members, :list => list_name), :get, {
      :page     => page,
      :per_page => per_page
    })
  end
end

#list_size(list_name) ⇒ Object



158
159
160
# File 'lib/madmimi.rb', line 158

def list_size(list_name)
    do_request(path(:audience_list_size, :list => list_name), :get)
end

#list_size_since(list_name, date) ⇒ Object



162
163
164
# File 'lib/madmimi.rb', line 162

def list_size_since(list_name, date)
  do_request(path(:audience_list_size, :list => list_name, :date => date), :get)
end

#listsObject

Audience and lists



97
98
99
100
101
# File 'lib/madmimi.rb', line 97

def lists
  wrap_with_array('lists', 'list') do
    do_request(path(:audience_lists), :get, :format => :xml)
  end
end

#mailing_stats(promotion_id, mailing_id) ⇒ Object

Stats



226
227
228
# File 'lib/madmimi.rb', line 226

def mailing_stats(promotion_id, mailing_id)
  do_request(path(:mailing_stats, :promotion_id => promotion_id, :mailing_id => mailing_id), :get)
end

#membersObject



143
144
145
146
147
# File 'lib/madmimi.rb', line 143

def members
  wrap_with_array('audience', 'member') do
    do_request(path(:get_audience_members), :get)
  end
end

#memberships(email) ⇒ Object



103
104
105
106
107
# File 'lib/madmimi.rb', line 103

def memberships(email)
  wrap_with_array('lists', 'list') do
    do_request(path(:memberships, :email => email), :get)
  end
end

#new_list(list_name) ⇒ Object



109
110
111
# File 'lib/madmimi.rb', line 109

def new_list(list_name)
  do_request(path(:create_list), :post, :name => list_name)
end

#promotionsObject

Promotions



202
203
204
205
206
# File 'lib/madmimi.rb', line 202

def promotions
  wrap_with_array('promotions', 'promotion') do
    do_request(path(:promotions), :get)
  end
end

#raise_exceptions=(raise_exceptions) ⇒ Object



84
85
86
# File 'lib/madmimi.rb', line 84

def raise_exceptions=(raise_exceptions)
  @api_settings[:raise_exceptions] = raise_exceptions
end

#raise_exceptions?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/madmimi.rb', line 80

def raise_exceptions?
  @api_settings[:raise_exceptions]
end

#remove_from_all_lists(email) ⇒ Object



135
136
137
# File 'lib/madmimi.rb', line 135

def remove_from_all_lists(email)
  do_request(path(:remove_from_all_lists), :post, :email => email)
end

#remove_from_list(email, list_name) ⇒ Object



131
132
133
# File 'lib/madmimi.rb', line 131

def remove_from_list(email, list_name)
  do_request(path(:remove_from_list, :list => list_name), :post, :email => email)
end

#save_promotion(promotion_name, raw_html, plain_text = nil) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/madmimi.rb', line 208

def save_promotion(promotion_name, raw_html, plain_text = nil)
  options = { :promotion_name => promotion_name }

  unless raw_html.nil?
    check_for_tracking_beacon raw_html
    check_for_opt_out raw_html
    options[:raw_html] = raw_html
  end

  unless plain_text.nil?
    check_for_opt_out plain_text
    options[:raw_plain_text] = plain_text
  end

  do_request(path(:promotion_save), :post, options)
end

#send_html(opt, html) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/madmimi.rb', line 241

def send_html(opt, html)
  options = opt.dup
  if html.include?('[[tracking_beacon]]') || html.include?('[[peek_image]]')
    options[:raw_html] = html
    if !options[:list_name].nil? || options[:to_all]
      unless html.include?('[[unsubscribe]]') || html.include?('[[opt_out]]')
        raise MadMimiError, "When specifying list_name, include the [[unsubscribe]] or [[opt_out]] macro in your HTML before sending."
      end
      do_request(path(options[:to_all] ? :mailer_to_all : :mailer_to_list), :post, options, true)
    else
      do_request(path(:mailer), :post, options, true)
    end
  else
    raise MadMimiError, "You'll need to include either the [[tracking_beacon]] or [[peek_image]] macro in your HTML before sending."
  end
end

#send_mail(opt, yaml_body) ⇒ Object

Mailer API



231
232
233
234
235
236
237
238
239
# File 'lib/madmimi.rb', line 231

def send_mail(opt, yaml_body)
  options = opt.dup
  options[:body] = yaml_body.to_yaml
  if !options[:list_name].nil? || options[:to_all]
    do_request(path(options[:to_all] ? :mailer_to_all : :mailer_to_list), :post, options, true)
  else
    do_request(path(:mailer), :post, options, true)
  end
end

#send_plaintext(opt, plaintext) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/madmimi.rb', line 258

def send_plaintext(opt, plaintext)
  options = opt.dup
  options[:raw_plain_text] = plaintext
  if !options[:list_name].nil? || options[:to_all]
    if plaintext.include?('[[unsubscribe]]') || plaintext.include?('[[opt_out]]')
      do_request(path(options[:to_all] ? :mailer_to_all : :mailer_to_list), :post, options, true)
    else
      raise MadMimiError, "You'll need to include either the [[unsubscribe]] or [[opt_out]] macro in your text before sending."
    end
  else
    do_request(path(:mailer), :post, options, true)
  end
end

#status(transaction_id) ⇒ Object



272
273
274
# File 'lib/madmimi.rb', line 272

def status(transaction_id)
  do_request(path(:mailer_status, :transaction_id => transaction_id), :get, {}, true)
end

#suppress_email(email) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/madmimi.rb', line 172

def suppress_email(email)
  return '' if suppressed?(email)

  process_json_response do
    do_request(path(:suppress_user), :post, :audience_member_id => email, :format => :json)
  end
end

#suppressed?(email) ⇒ Boolean

Returns:

  • (Boolean)


188
189
190
191
# File 'lib/madmimi.rb', line 188

def suppressed?(email)
  response = do_request(path(:is_suppressed, :email => email), :get)
  response == 'true'
end

#suppressed_since(timestamp, show_suppression_reason = false) ⇒ Object



166
167
168
169
170
# File 'lib/madmimi.rb', line 166

def suppressed_since(timestamp, show_suppression_reason = false)
  do_request(path(:suppressed_since, :timestamp => timestamp), :get, {
    :show_suppression_reason => show_suppression_reason
  })
end

#unsuppress_email(email) ⇒ Object



180
181
182
183
184
185
186
# File 'lib/madmimi.rb', line 180

def unsuppress_email(email)
  return '' unless suppressed?(email)

  process_json_response do
    do_request(path(:unsuppress_user, :email => email), :delete, :format => :json)
  end
end

#update_email(existing_email, new_email) ⇒ Object



139
140
141
# File 'lib/madmimi.rb', line 139

def update_email(existing_email, new_email)
  do_request(path(:update_user_email, :email => existing_email), :post, :email => existing_email, :new_email => new_email)
end

#usernameObject



72
73
74
# File 'lib/madmimi.rb', line 72

def username
  @api_settings[:username]
end

#verify_ssl=(verify_ssl) ⇒ Object



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

def verify_ssl=(verify_ssl)
  @api_settings[:verify_ssl] = verify_ssl
end

#verify_ssl?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/madmimi.rb', line 88

def verify_ssl?
  @api_settings[:verify_ssl]
end