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'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of MadMimi.



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

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

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

Instance Method Details

#add_to_list(email, list_name) ⇒ Object



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

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



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

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



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

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

#api_keyObject



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

def api_key
  @api_settings[:api_key]
end

#audience_search(query_string, raw = false) ⇒ Object



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

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



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

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

#default_optObject



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

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

#delete_list(list_name) ⇒ Object



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

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

#listsObject



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

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

#mailing_stats(promotion_id, mailing_id) ⇒ Object



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

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



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

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

#new_list(list_name) ⇒ Object



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

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

#promotionsObject



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

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

#raise_exceptionsObject



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

def raise_exceptions
  @api_settings[:raise_exceptions]
end

#remove_from_list(email, list_name) ⇒ Object



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

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



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/madmimi.rb', line 151

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



133
134
135
136
137
138
139
140
141
# File 'lib/madmimi.rb', line 133

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



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/madmimi.rb', line 168

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

#suppress_email(email) ⇒ Object



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

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

#suppressed_since(timestamp) ⇒ Object



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

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

#usernameObject



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

def username
  @api_settings[:username]
end