Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality for events and get/delete messages #24

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/mailgun.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require "rest-client"
require "json"
require "multimap"

require "mailgun/mailgun_error"
require "mailgun/base"
Expand All @@ -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"
Expand All @@ -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
8 changes: 6 additions & 2 deletions lib/mailgun/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions lib/mailgun/event.rb
Original file line number Diff line number Diff line change
@@ -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
46 changes: 43 additions & 3 deletions lib/mailgun/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
# 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
12 changes: 9 additions & 3 deletions lib/mailgun/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Route
def initialize(mailgun)
@mailgun = mailgun
end

def list(options={})
Mailgun.submit(:get, route_url, options)["items"] || []
end
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions mailgun.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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<rest-client>, [">= 0"])
gem.add_dependency(%q<multimap>, [">= 0"])
gem.version = "0.8"

gem.add_dependency(%q<rest-client>, ["~> 2.0.0.rc2"])

gem.add_development_dependency(%q<rspec>, [">= 2"])
gem.add_development_dependency(%q<debugger>, [">= 0"])
Expand Down
67 changes: 67 additions & 0 deletions spec/event_spec.rb
Original file line number Diff line number Diff line change
@@ -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 = <<EOF
{
"items": [
{
"campaigns":[

],
"user-variables":{

},
"flags":{
"is-test-mode":false
},
"tags":[

],
"timestamp":1378335036.859382,
"message":{
"headers":{
"to":"satshabad <[email protected]>",
"message-id":"CAC8xyJxAO7Y0sr=3r-rJ4C6ULZs3cSVPPqYEXLHtarKOKaOCKw@mail.gmail.com",
"from":"Someone <[email protected]>",
"subject":"Re: A TEST"
},
"attachments":[

],
"recipients":[
"[email protected]"
],
"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
38 changes: 36 additions & 2 deletions spec/list/message_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,6 +10,8 @@
:name => "test",
:domain => "sample.mailgun.org"
}

@sample_message_key = "WyI3MDhjODgwZTZlIiwgIjF6"
end

describe "send email" do
Expand All @@ -30,11 +32,43 @@
:from => "[email protected]"
}
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 = <<EOF
{
"headers"=>{
"to"=>"[email protected]",
"message-id"=>"[email protected]",
"from"=>"[email protected]",
"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