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'
PROMOTIONS_PATH =
'/promotions.xml'
MAILING_STATS_PATH =
'/promotions/%promotion_id%/mailings/%mailing_id%.xml'
SEARCH_PATH =
'/audience_members/search.xml'
MAILER_PATH =
'/mailer'
MAILER_TO_LIST_PATH =
'/mailer/to_list'
MAILER_STATUS_PATH =
'/mailers/status'

Instance Method Summary collapse

Constructor Details

#initialize(username, api_key) ⇒ MadMimi

Returns a new instance of MadMimi.



53
54
55
# File 'lib/madmimi.rb', line 53

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

Instance Method Details

#add_to_list(email, list_name) ⇒ Object



96
97
98
# File 'lib/madmimi.rb', line 96

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



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

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. :)



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

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

#api_keyObject



61
62
63
# File 'lib/madmimi.rb', line 61

def api_key
  @api_settings[:api_key]
end

#audience_search(query_string, raw = false) ⇒ Object



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

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



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

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

#default_optObject



65
66
67
# File 'lib/madmimi.rb', line 65

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

#delete_list(list_name) ⇒ Object



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

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

#listsObject



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

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

#mailing_stats(promotion_id, mailing_id) ⇒ Object



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

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



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

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

#new_list(list_name) ⇒ Object



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

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

#promotionsObject



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

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

#remove_from_list(email, list_name) ⇒ Object



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

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

#send_html(opt, html) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/madmimi.rb', line 146

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?
      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(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



128
129
130
131
132
133
134
135
136
# File 'lib/madmimi.rb', line 128

def send_mail(opt, yaml_body)
  options = opt.dup
  options[:body] = yaml_body.to_yaml
  if !options[:list_name].nil?
    do_request(MAILER_TO_LIST_PATH, :post, options, true)
  else
    do_request(MAILER_PATH, :post, options, true)
  end
end

#send_plaintext(opt, plaintext) ⇒ Object



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

def send_plaintext(opt, plaintext)
  options = opt.dup
  options[:raw_plain_text] = plaintext
  if !options[:list_name].nil?
    if plaintext.include?('[[unsubscribe]]') || plaintext.include?('[[opt_out]]')
      do_request(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



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

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

#suppress_email(email) ⇒ Object



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

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

#suppressed_since(timestamp) ⇒ Object



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

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

#usernameObject



57
58
59
# File 'lib/madmimi.rb', line 57

def username
  @api_settings[:username]
end