Permalink
Browse files

add raise_exceptions options to initializer

  • Loading branch information...
1 parent 2bd0aac commit 035783a7a1fde66cc459f8496061b406af2a8969 @maximgladkov maximgladkov committed May 16, 2014
Showing with 51 additions and 58 deletions.
  1. +35 −30 README.rdoc
  2. +16 −28 lib/madmimi.rb
View
65 README.rdoc
@@ -17,69 +17,74 @@ or if you prefer to live on the edge, just clone this repository and build it fr
mimi = MadMimi.new('emailaddress', 'api_key')
mimi.lists # get all of your Mad Mimi lists returned as a hash
+You can pass +{ :raise_exceptions => true }+ to the initializer, if you wish to receive exceptions for failing requests:
+
+ MadMimi.new('emailaddress', 'api_key', { :raise_exceptions => true })
+ mimi.lists # will raise an Exception if request fails
+
=== Audience Members and Lists
mimi.memberships('email') # returns a hash of the lists that specific email address is subscribed to
-
+
mimi.new_list('New list name') # make a new list
-
+
mimi.delete_list('New list name') # delete the list I just created
-
+
mimi.csv_import("name,email\ndave,dave@example.com\n") # import from a csv string
-
+
mimi.add_to_list('dave@example.com', 'Test List') # add this email address to a specific list
-
+
mimi.remove_from_list('dave@example.com', 'Test List') # remove this email address from a specific list
-
+
mimi.suppressed_since('unix timestamp') # get a TXT of all addresses that were suppressed since this timestamp
=== Promotions
mimi.promotions # returns a hash of your promotions
-
+
mimi.save_promotion('promotion_name', 'raw_html', 'plain_text') # saves a promotion (creates the promotion if it does not exist)
-
+
mimi.mailing_stats('promotion_id', 'mailing_id') # get stats on a specific mailing
== Sending E-Mail (using the Mailer API)
=== Replacing keys in your email body text:
- options = {
- 'promotion_name' => 'Test Promotion',
- 'recipients' => 'Nicholas Young <nicholas@madmimi.com>',
- 'from' => 'MadMimi Ruby <rubygem@madmimi.com>',
- 'subject' => 'Test Subject'
+ options = {
+ 'promotion_name' => 'Test Promotion',
+ 'recipients' => 'Nicholas Young <nicholas@madmimi.com>',
+ 'from' => 'MadMimi Ruby <rubygem@madmimi.com>',
+ 'subject' => 'Test Subject'
}
- yaml_body = {
- 'greeting' => 'Hello',
- 'name' => 'Nicholas'
+ yaml_body = {
+ 'greeting' => 'Hello',
+ 'name' => 'Nicholas'
}
-
+
mimi.send_mail(options, yaml_body)
=== Sending Raw HTML (presumably generated by your app)
- options = {
- 'promotion_name' => 'Test Promotion',
- 'recipients' => 'Nicholas Young <nicholas@madmimi.com>',
- 'from' => 'MadMimi Ruby <rubygem@madmimi.com>',
- 'subject' => 'Test Subject'
+ options = {
+ 'promotion_name' => 'Test Promotion',
+ 'recipients' => 'Nicholas Young <nicholas@madmimi.com>',
+ 'from' => 'MadMimi Ruby <rubygem@madmimi.com>',
+ 'subject' => 'Test Subject'
}
raw_html = "<html><head><title>My great promotion!</title></head><body>Body stuff[[tracking_beacon]]</body></html>"
-
+
mimi.send_html(options, raw_html)
=== Sending Plain Text
- options = {
- 'promotion_name' => 'Test Promotion',
- 'recipients' => 'Nicholas Young <nicholas@madmimi.com>',
- 'from' => 'MadMimi Ruby <rubygem@madmimi.com>',
- 'subject' => 'Test Subject'
+ options = {
+ 'promotion_name' => 'Test Promotion',
+ 'recipients' => 'Nicholas Young <nicholas@madmimi.com>',
+ 'from' => 'MadMimi Ruby <rubygem@madmimi.com>',
+ 'subject' => 'Test Subject'
}
plain_text = "Plain text email contents [[unsubscribe]]"
-
+
mimi.send_plaintext(options, plain_text)
=== Getting the status of a transactional mailing
@@ -106,7 +111,7 @@ will be those for an already-existing "audience."
* 'to_all': Set to true to send a promotion, plain_text or raw_html to all your audience members
== Note on Patches/Pull Requests
-
+
* Fork the project.
* Make your feature addition or bug fix.
* Add tests for it. This is important so I don't break it in a
View
44 lib/madmimi.rb
@@ -26,6 +26,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
+require 'active_support/core_ext/string'
require 'uri'
require 'net/http'
require 'net/https'
@@ -56,8 +57,12 @@ class MadMimiError < StandardError; end
MAILER_TO_ALL_PATH = '/mailer/to_all'
MAILER_STATUS_PATH = '/mailers/status'
- def initialize(username, api_key)
- @api_settings = { :username => username, :api_key => api_key }
+ def initialize(username, api_key, options = {})
+ @api_settings = options.merge({ :username => username, :api_key => api_key })
+ end
+
+ def raise_exceptions?
+ @api_settings[:raise_exceptions]
end
def username
@@ -211,7 +216,6 @@ def status(transaction_id)
def do_request(path, req_type = :get, options = {}, transactional = false)
options = options.merge(default_opt)
form_data = options.inject({}) { |m, (k, v)| m[k.to_s] = v; m }
- resp = href = ""
if transactional == true
http = Net::HTTP.new(BASE_URL, 443)
@@ -222,32 +226,16 @@ def do_request(path, req_type = :get, options = {}, transactional = false)
http = Net::HTTP.new(BASE_URL, 80)
end
- case req_type
-
- when :get then
- begin
- http.start do |http|
- req = Net::HTTP::Get.new(path)
- req.set_form_data(form_data)
- response = http.request(req)
- resp = response.body.strip
- end
- resp
- rescue SocketError
- raise "Host unreachable."
- end
- when :post then
- begin
- http.start do |http|
- req = Net::HTTP::Post.new(path)
- req.set_form_data(form_data)
- response = http.request(req)
- resp = response.body.strip
- end
- rescue SocketError
- raise "Host unreachable."
- end
+ response = http.start do |http|
+ # Either Net::HTTP::Get or Net::HTTP::Post
+ http_class = Net::HTTP.const_get(req_type.to_s.titleize)
+ req = http_class.new(path)
+ req.set_form_data(form_data)
+ http.request(req)
end
+
+ response.value if raise_exceptions?
+ response.body.strip
end
def build_csv(hash)

0 comments on commit 035783a

Please sign in to comment.