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



186
187
188
# File 'lib/madmimi.rb', line 186

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



182
183
184
# File 'lib/madmimi.rb', line 182

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) ⇒ Object



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

def list_members(list_name)
  wrap_with_array('audience', 'member') do
    do_request(path(:audience_list_members, :list => list_name), :get)
  end
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



215
216
217
# File 'lib/madmimi.rb', line 215

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



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

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



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/madmimi.rb', line 197

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



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/madmimi.rb', line 230

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



220
221
222
223
224
225
226
227
228
# File 'lib/madmimi.rb', line 220

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



247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/madmimi.rb', line 247

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



261
262
263
# File 'lib/madmimi.rb', line 261

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

#suppress_email(email) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/madmimi.rb', line 161

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)


177
178
179
180
# File 'lib/madmimi.rb', line 177

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

#suppressed_since(timestamp, show_suppression_reason = false) ⇒ Object



155
156
157
158
159
# File 'lib/madmimi.rb', line 155

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



169
170
171
172
173
174
175
# File 'lib/madmimi.rb', line 169

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