Class: MadMimi

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

Defined Under Namespace

Classes: MadMimiError

Constant Summary collapse

BASE_URL =
'api.madmimi.com'
NEW_LISTS_PATH =
'/audience_lists'
AUDIENCE_MEMBERS_PATH =
'/audience_members'
AUDIENCE_LISTS_PATH =
'/audience_lists/lists.xml'
MEMBERSHIPS_PATH =
'/audience_members/%email%/lists.xml'
SUPPRESSED_SINCE_PATH =
'/audience_members/suppressed_since/%timestamp%.txt'
SUPPRESS_USER_PATH =
' /audience_members/%email%/suppress_email'
SEARCH_PATH =
'/audience_members/search.xml'
PROMOTIONS_PATH =
'/promotions.xml'
PROMOTION_SAVE_PATH =
'/promotions/save'
MAILING_STATS_PATH =
'/promotions/%promotion_id%/mailings/%mailing_id%.xml'
MAILER_PATH =
'/mailer'
MAILER_TO_LIST_PATH =
'/mailer/to_list'
MAILER_TO_ALL_PATH =
'/mailer/to_all'
MAILER_STATUS_PATH =
'/mailers/status'

Instance Method Summary collapse

Constructor Details

#initialize(username, api_key) ⇒ MadMimi

Returns a new instance of MadMimi.



59
60
61
# File 'lib/madmimi.rb', line 59

def initialize(username, api_key)
  @api_settings = { :username => username, :api_key => api_key }
end

Instance Method Details

#add_to_list(email, list_name) ⇒ Object



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

def add_to_list(email, list_name)
  do_request("#{NEW_LISTS_PATH}/#{URI.escape(list_name)}/add", :post, :email => email)
end

#add_user(options) ⇒ Object



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

def add_user(options)
  csv_data = build_csv(options)
  do_request(AUDIENCE_MEMBERS_PATH, :post, :csv_file => csv_data)
end

#add_users_to_list(list_name, arr) ⇒ Object

Not the most elegant, but it works for now. :)



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

def add_users_to_list(list_name, arr)
  arr.each do |a|
    a[:add_list] = list_name
    add_user(a)
  end
end

#api_keyObject



67
68
69
# File 'lib/madmimi.rb', line 67

def api_key
  @api_settings[:api_key]
end

#audience_search(query_string, raw = false) ⇒ Object



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

def audience_search(query_string, raw = false)
  request = do_request(SEARCH_PATH, :get, :raw => raw, :query => query_string)
  Crack::XML.parse(request)
end

#csv_import(csv_string) ⇒ Object



94
95
96
# File 'lib/madmimi.rb', line 94

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

#default_optObject



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

def default_opt
  { :username => username, :api_key => api_key }
end

#delete_list(list_name) ⇒ Object



90
91
92
# File 'lib/madmimi.rb', line 90

def delete_list(list_name)
  do_request("#{NEW_LISTS_PATH}/#{URI.escape(list_name)}", :post, :'_method' => 'delete')
end

#listsObject

Audience and lists



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

def lists
  request = do_request(AUDIENCE_LISTS_PATH, :get)
  Crack::XML.parse(request)
end

#mailing_stats(promotion_id, mailing_id) ⇒ Object

Stats



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

def mailing_stats(promotion_id, mailing_id)
  path = MAILING_STATS_PATH.gsub('%promotion_id%', promotion_id).gsub('%mailing_id%', mailing_id)
  request = do_request(path, :get)
  Crack::XML.parse(request)
end

#memberships(email) ⇒ Object



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

def memberships(email)
  request = do_request(MEMBERSHIPS_PATH.gsub('%email%', email), :get)
  Crack::XML.parse(request)
end

#new_list(list_name) ⇒ Object



86
87
88
# File 'lib/madmimi.rb', line 86

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

#promotionsObject

Promotions



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

def promotions
  request = do_request(PROMOTIONS_PATH, :get)
  Crack::XML.parse(request)
end

#remove_from_list(email, list_name) ⇒ Object



107
108
109
# File 'lib/madmimi.rb', line 107

def remove_from_list(email, list_name)
  do_request("#{NEW_LISTS_PATH}/#{URI.escape(list_name)}/remove", :post, :email => email)
end

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



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/madmimi.rb', line 138

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 PROMOTION_SAVE_PATH, :post, options
end

#send_html(opt, html) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/madmimi.rb', line 173

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(options[:to_all] ? MAILER_TO_ALL_PATH : MAILER_TO_LIST_PATH, :post, options, true)
    else
      do_request(MAILER_PATH, :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



163
164
165
166
167
168
169
170
171
# File 'lib/madmimi.rb', line 163

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(options[:to_all] ? MAILER_TO_ALL_PATH : MAILER_TO_LIST_PATH, :post, options, true)
  else
    do_request(MAILER_PATH, :post, options, true)
  end
end

#send_plaintext(opt, plaintext) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/madmimi.rb', line 190

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(options[:to_all] ? MAILER_TO_ALL_PATH : MAILER_TO_LIST_PATH, :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(MAILER_PATH, :post, options, true)
  end
end

#status(transaction_id) ⇒ Object



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

def status(transaction_id)
  do_request "#{MAILER_STATUS_PATH}/#{transaction_id}", :get, {}, true
end

#suppress_email(email) ⇒ Object



115
116
117
# File 'lib/madmimi.rb', line 115

def suppress_email(email)
  do_request(SUPPRESS_USER_PATH.gsub('%email%', email), :post)
end

#suppressed_since(timestamp) ⇒ Object



111
112
113
# File 'lib/madmimi.rb', line 111

def suppressed_since(timestamp)
  do_request(SUPPRESSED_SINCE_PATH.gsub('%timestamp%', timestamp), :get)
end

#usernameObject



63
64
65
# File 'lib/madmimi.rb', line 63

def username
  @api_settings[:username]
end