Class: MadMimi
- Inherits:
-
Object
- Object
- MadMimi
- 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
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #add_to_list(email, list_name) ⇒ Object
- #add_user(options) ⇒ Object
-
#add_users_to_list(list_name, arr) ⇒ Object
Not the most elegant, but it works for now.
- #api_key ⇒ Object
- #audience_search(query_string, raw = false) ⇒ Object
- #csv_import(csv_string) ⇒ Object
- #default_opt ⇒ Object
- #delete_list(list_name) ⇒ Object
-
#initialize(username, api_key, options = {}) ⇒ MadMimi
constructor
A new instance of MadMimi.
- #lists ⇒ Object
- #mailing_stats(promotion_id, mailing_id) ⇒ Object
- #memberships(email) ⇒ Object
- #new_list(list_name) ⇒ Object
- #promotions ⇒ Object
- #raise_exceptions ⇒ Object
- #remove_from_list(email, list_name) ⇒ Object
- #send_html(opt, html) ⇒ Object
- #send_mail(opt, yaml_body) ⇒ Object
- #send_plaintext(opt, plaintext) ⇒ Object
- #suppress_email(email) ⇒ Object
- #suppressed_since(timestamp) ⇒ Object
- #username ⇒ Object
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, = {}) @api_settings = .merge({ :username => username, :api_key => api_key }) end |
Instance Attribute Details
#response ⇒ Object (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() csv_data = build_csv() 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_key ⇒ Object
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_opt ⇒ Object
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 |
#lists ⇒ Object
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 |
#promotions ⇒ Object
117 118 119 120 |
# File 'lib/madmimi.rb', line 117 def promotions request = do_request(PROMOTIONS_PATH, :get) Crack::XML.parse(request) end |
#raise_exceptions ⇒ Object
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) = opt.dup if html.include?('[[tracking_beacon]]') || html.include?('[[peek_image]]') [:raw_html] = html if ![: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, , true) else do_request(MAILER_PATH, :post, , 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) = opt.dup [:body] = yaml_body.to_yaml if ![:list_name].nil? do_request(MAILER_TO_LIST_PATH, :post, , true) else do_request(MAILER_PATH, :post, , 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) = opt.dup [:raw_plain_text] = plaintext if ![:list_name].nil? if plaintext.include?('[[unsubscribe]]') || plaintext.include?('[[opt_out]]') do_request(MAILER_TO_LIST_PATH, :post, , 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, , 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() do_request(SUPPRESSED_SINCE_PATH.gsub('%timestamp%', ), :get) end |
#username ⇒ Object
62 63 64 |
# File 'lib/madmimi.rb', line 62 def username @api_settings[:username] end |