diff --git a/README.md b/README.md index 1b460c2..36fe855 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,23 @@ Supported route actions are: `:forward`, and `:stop` @mailbox.domains.delete "example.com" ``` + +#### Events +```ruby +# Initialize your Mailgun object: +@mailgun = Mailgun(:api_key => 'your-api-key') + +# Setup some paramters +parameters = { + :end => "Wed, 15 Feb 2012 13:03:31 GMT", + :event => 'stored' +} + +# Send the request +@mailgun.events.list parameters +``` + + ## Making Your Changes * Fork the project (Github has really good step-by-step directions) diff --git a/lib/mailgun.rb b/lib/mailgun.rb index e833631..68b1b01 100644 --- a/lib/mailgun.rb +++ b/lib/mailgun.rb @@ -1,6 +1,5 @@ require "rest-client" require "json" -require "multimap" require "mailgun/mailgun_error" require "mailgun/base" @@ -11,6 +10,7 @@ require "mailgun/unsubscribe" require "mailgun/complaint" require "mailgun/log" +require "mailgun/event" require "mailgun/list" require "mailgun/list/member" require "mailgun/message" @@ -19,6 +19,6 @@ def Mailgun(options={}) options[:api_key] = Mailgun.api_key if Mailgun.api_key - options[:domain] = Mailgun.domain if Mailgun.domain + #options[:domain] = Mailgun.domain if Mailgun.domain Mailgun::Base.new(options) end diff --git a/lib/mailgun/base.rb b/lib/mailgun/base.rb index efe7734..6d5f79a 100644 --- a/lib/mailgun/base.rb +++ b/lib/mailgun/base.rb @@ -8,9 +8,9 @@ class Base # * Test mode - if enabled, doesn't actually send emails (see http://documentation.mailgun.net/user_manual.html#sending-in-test-mode) # * Domain - domain to use def initialize(options) - Mailgun.mailgun_host = options.fetch(:mailgun_host) {"api.mailgun.net"} + Mailgun.mailgun_host = options.fetch(:mailgun_host) { "api.mailgun.net" } Mailgun.protocol = options.fetch(:protocol) { "https" } - Mailgun.api_version = options.fetch(:api_version) { "v2" } + Mailgun.api_version = options.fetch(:api_version) { "v3" } Mailgun.test_mode = options.fetch(:test_mode) { false } Mailgun.api_key = options.fetch(:api_key) { raise ArgumentError.new(":api_key is a required argument to initialize Mailgun") if Mailgun.api_key.nil?} Mailgun.domain = options.fetch(:domain) { nil } @@ -54,6 +54,10 @@ def log(domain=Mailgun.domain) Mailgun::Log.new(self, domain) end + def events(domain=Mailgun.domain) + Mailgun::Events.new(self, domain) + end + def lists @lists ||= Mailgun::MailingList.new(self) end diff --git a/lib/mailgun/event.rb b/lib/mailgun/event.rb new file mode 100644 index 0000000..355f8de --- /dev/null +++ b/lib/mailgun/event.rb @@ -0,0 +1,23 @@ +module Mailgun + class Events + # Used internally, called from Mailgun::Base + def initialize(mailgun, domain) + @mailgun = mailgun + @domain = domain + end + + # List all events for a given domain + # * domain the domain for which all events will listed + def list(options={}) + Mailgun.submit(:get, events_url, options)['items'] || [] + end + + private + + # Helper method to generate the proper url for Mailgun events API calls + def events_url + "#{@mailgun.base_url}/#{@domain}/events" + end + + end +end \ No newline at end of file diff --git a/lib/mailgun/message.rb b/lib/mailgun/message.rb index 68fa8ef..6201033 100644 --- a/lib/mailgun/message.rb +++ b/lib/mailgun/message.rb @@ -15,14 +15,54 @@ def send_email(parameters={}) # :in_test_mode BOOL. override the @use_test_mode setting # :tags to add tags to the email # :track BOOL - Mailgun.submit(:post, messages_url, parameters) + + # TODO: check addresses for proper format: + # display-name + # display-name should be enclosed in quotes + # to escape these characters ()<>[]:;,@\ + # and not contain any control characters + + Mailgun.submit(:post, send_messages_url, parameters) + end + + # receive email + def fetch_email( url ) + # options: + # key is the identifier returned in the events call + # for a stored email + parts = url.split( '//' ) + Mailgun.submit(:get, parts[0] + "//api:#{Mailgun.api_key}@" + parts[1]) + end + + # delete email + def delete_email( key ) + # options: + # key is the identifier returned in the events call + # for a stored email + Mailgun.submit(:delete, fetch_messages_url+"/"+key) end - #private + def fetch_email_attachment( url ) + # options: + # key is the identifier returned in the events call + # for a stored email + # fidx is the index of the attachment (from the + # original message + + # There is no JSON response here ... just the file + parts = url.split( '//' ) + RestClient.send(:get, parts[0] + "//api:#{Mailgun.api_key}@" + parts[1]) + end + + private # Helper method to generate the proper url for Mailgun message API calls - def messages_url + def send_messages_url "#{@mailgun.base_url}/#{@domain}/messages" end + + def fetch_messages_url + "#{@mailgun.base_url}/domains/#{@domain}/messages" + end end end diff --git a/lib/mailgun/route.rb b/lib/mailgun/route.rb index a819c3b..84beda2 100644 --- a/lib/mailgun/route.rb +++ b/lib/mailgun/route.rb @@ -4,7 +4,7 @@ class Route def initialize(mailgun) @mailgun = mailgun end - + def list(options={}) Mailgun.submit(:get, route_url, options)["items"] || [] end @@ -49,11 +49,11 @@ def update(route_id, params) Mailgun.submit(:put, route_url(route_id), data) end - + def destroy(route_id) Mailgun.submit(:delete, route_url(route_id))["id"] end - + private def route_url(route_id=nil) @@ -67,6 +67,12 @@ def build_actions(actions) case action.first.to_sym when :forward _actions << "forward(\"#{action.last}\")" + when :store + if action.length > 1 + _actions << "store(notify=#{action.last})" + else + _actions << "store()" + end when :stop _actions << "stop()" else diff --git a/mailgun.gemspec b/mailgun.gemspec index e33f365..12140b5 100644 --- a/mailgun.gemspec +++ b/mailgun.gemspec @@ -12,10 +12,9 @@ Gem::Specification.new do |gem| gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") gem.name = "mailgun" gem.require_paths = ["lib"] - gem.version = "0.7" - - gem.add_dependency(%q, [">= 0"]) - gem.add_dependency(%q, [">= 0"]) + gem.version = "0.8" + + gem.add_dependency(%q, ["~> 2.0.0.rc2"]) gem.add_development_dependency(%q, [">= 2"]) gem.add_development_dependency(%q, [">= 0"]) diff --git a/spec/event_spec.rb b/spec/event_spec.rb new file mode 100644 index 0000000..66d8d57 --- /dev/null +++ b/spec/event_spec.rb @@ -0,0 +1,67 @@ +require 'spec_helper' + +describe Mailgun::Events do + + before :each do + @mailgun = Mailgun({:api_key => "api-key"}) # used to get the default values + end + + describe "get events" do + it "should make a GET request with the right params" do + sample_response = <", + "message-id":"CAC8xyJxAO7Y0sr=3r-rJ4C6ULZs3cSVPPqYEXLHtarKOKaOCKw@mail.gmail.com", + "from":"Someone ", + "subject":"Re: A TEST" + }, + "attachments":[ + + ], + "recipients":[ + "satshabad@mailgun.com" + ], + "size":2566 + }, + "storage":{ + "url":"https://api.mailgun.net/v2/domains/ninomail.com/messages/WyI3MDhjODgwZTZlIiwgIjF6", + "key":"WyI3MDhjODgwZTZlIiwgIjF6" + }, + "event":"stored" + } + ] +} +EOF + + parameters = { + :end => "Wed, 15 Feb 2012 13:03:31 GMT", + :event => 'stored' + } + + Mailgun.should_receive(:submit). + with(:get, @mailgun.events.send('events_url'), parameters). + and_return(sample_response) + + @mailgun.events.list parameters + end + end + + +end \ No newline at end of file diff --git a/spec/list/message_spec.rb b/spec/list/message_spec.rb index e58641a..3117f02 100644 --- a/spec/list/message_spec.rb +++ b/spec/list/message_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Mailgun::Log do +describe Mailgun::Message do before :each do @mailgun = Mailgun({:api_key => "api-key"}) # used to get the default values @@ -10,6 +10,8 @@ :name => "test", :domain => "sample.mailgun.org" } + + @sample_message_key = "WyI3MDhjODgwZTZlIiwgIjF6" end describe "send email" do @@ -30,11 +32,43 @@ :from => "lumberg.bill@initech.mailgun.domain" } Mailgun.should_receive(:submit) \ - .with(:post, @mailgun.messages.messages_url, parameters) \ + .with(:post, @mailgun.messages.send('send_messages_url'), parameters) \ .and_return(sample_response) @mailgun.messages.send_email(parameters) end end + describe "get email" do + it "should make a GET request with the right params" do + sample_response = <{ + "to"=>"lumberg.bill@initech.mailgun.domain", + "message-id"=>"20111114174239.25659.5817@samples.mailgun.org", + "from"=>"cooldev@your.mailgun.domain", + "subject"=>"RE: missing tps reports" + } +} +EOF + + Mailgun.should_receive(:submit). + with(:get, @mailgun.messages.send('fetch_messages_url')+'/'+@sample_message_key). + and_return(sample_response) + + @mailgun.messages.fetch_email @sample_message_key + end + end + + describe "delete email" do + it "should make a DELETE request with the right params" do + + Mailgun.should_receive(:submit). + with(:delete, @mailgun.messages.send('fetch_messages_url')+'/'+@sample_message_key). + and_return({}) + + @mailgun.messages.delete_email @sample_message_key + end + end + end \ No newline at end of file