mailgun 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,10 @@
1
1
  source 'http://rubygems.org'
2
2
 
3
+ gem "rest-client"
4
+
5
+ group :development do
6
+ gem "rspec"
7
+ end
8
+
3
9
  # Specify your gem's dependencies in mailgun.gemspec
4
10
  gemspec
data/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ The MIT License (MIT)
2
+ Copyright (c) 2011 Bushido Inc.
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ Mailgun
2
+ =========
3
+ This gem allows for idiomatic Mailgun usage from within ruby. Mailgun is a kickass email-as-a-service that lets you use email as if it made sense. Check it out at http://mailgun.net/
4
+
5
+ The official gem repo is at https://github.com/Bushido/mailgun
6
+
7
+ Mailgun exposes the following resources:
8
+
9
+ * Routes
10
+ * Log
11
+ * Stats
12
+ * Messages
13
+ * Mailboxes
14
+
15
+ Currently the gem only exposes the Mailbox API, but patches are welcome (and easy!).
16
+
17
+ Usage
18
+ =====
19
+ We mimic the ActiveRecord-style interface.
20
+
21
+ # Initialize your Mailgun object:
22
+ @mailgun = Mailgun(:api_key => 'your-api-key')
23
+
24
+ # Create a mailbox
25
+ @mailgun.mailbox.create "new-mailbox@your-domain.com", "password"
26
+
27
+ # List all mailboxes that belong to a domain
28
+ @mailgun.mailboxes.list domain.com"
29
+
30
+ # Destroy a mailbox (queue bond-villian laughter)
31
+ # "I'm sorry Bond, it seems your mailbox will be... destroyed!"
32
+ @mailbox.mailboxes.destroy "bond@mi5.co.uk"
33
+
34
+ Making Your Changes
35
+ ===================
36
+
37
+ * Fork the project (Github has really good step-by-step directions)
38
+ * Start a feature/bugfix branch
39
+ * Commit and push until you are happy with your contribution
40
+ * Make sure to add tests for it. This is important so we don't break it in a future version unintentionally.
41
+ * After making your changes, be sure to run the Mailgun RSpec specs to make sure everything works.
42
+ * Submit your change as a Pull Request and update the GitHub issue to let us know it is ready for review.
43
+
44
+ Authors
45
+ =======
46
+
47
+ * Akash Manohar J (akash@akash.im)
48
+ * Sean Grove (sean@gobushido.com)
49
+
50
+ Thanks
51
+ ======
52
+ Huge thanks to the Mailgun guys for such an amazing service! No time spent on mailservers == way more time spent drinking!
53
+
54
+ License & Copyright
55
+ ===================
56
+ Released under the MIT license. See LICENSE for more details.
57
+
58
+ All copyright Bushido Inc. 2011
@@ -0,0 +1,34 @@
1
+ module Mailgun
2
+ class Base
3
+ attr_accessor :api_key,
4
+ :api_version,
5
+ :protocol,
6
+ :mailgun_host,
7
+ :use_test_mode
8
+
9
+ # Options taken from
10
+ # http://documentation.mailgun.net/quickstart.html#authentication
11
+ # * Mailgun host - location of mailgun api servers
12
+ # * Procotol - http or https [default to https]
13
+ # * API key and version
14
+ # * Test mode - if enabled, doesn't actually send emails (see http://documentation.mailgun.net/user_manual.html#sending-in-test-mode)
15
+ def initialize(options)
16
+ @mailgun_host = options.fetch(:mailgun_host) {"api.mailgun.net"}
17
+ @protocol = options.fetch(:protocol) { "https" }
18
+ @api_version = options.fetch(:api_version) { "v2" }
19
+ @test_mode = options.fetch(:test_mode) { false }
20
+
21
+ @api_key = options.fetch(:api_key) { raise ArgumentError(":api_key is a required argument to initialize Mailgun") }
22
+ end
23
+
24
+ # Returns the base url used in all Mailgun API calls
25
+ def base_url
26
+ "#{@protocol}://api:#{api_key}@#{mailgun_host}/#{api_version}"
27
+ end
28
+
29
+ # Returns an instance of Mailgun::Mailbox configured for the current API user
30
+ def mailboxes
31
+ @mailboxes ||= Mailgun::Mailbox.new(self)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ module Mailgun
2
+ class Mail
3
+
4
+ def initialize(mailgun)
5
+ @mailgun = mailgun
6
+ end
7
+
8
+ # send email
9
+ def send_email()
10
+ # TODO with the following options
11
+ # :from, :to, :cc, :bcc, :subject, :text, :html
12
+ # :with_attachment
13
+ # :with_attachments
14
+ # :at for delayed delivery time option
15
+ # :in_test_mode BOOL. override the @use_test_mode setting
16
+ # :tags to add tags to the email
17
+ # :track BOOL
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,52 @@
1
+ module Mailgun
2
+ class Mailbox
3
+
4
+ # Used internally, called from Mailgun::Base
5
+ def initialize(mailgun)
6
+ @mailgun = mailgun
7
+ end
8
+
9
+ # List all mailboxes for a given domain
10
+ # * domain the domain for which all mailboxes will listed
11
+ def list(domain)
12
+ submit :get, mailbox_url(domain)
13
+ end
14
+
15
+
16
+ # Creates a mailbox on the Mailgun server with the given password
17
+ def create(address, password)
18
+ submit :post, mailbox_url(address.split("@").last), :mailbox => address,
19
+ :password => password
20
+ end
21
+
22
+
23
+ # Sets the password for a mailbox
24
+ def update_password(address, password)
25
+ mailbox_name, domain = address.split("@")
26
+
27
+ submit :put, mailbox_url(domain, mailbox_name), :password => password
28
+ end
29
+
30
+
31
+ # Destroys the mailbox
32
+ def destroy(address)
33
+ mailbox_name, domain = address.split("@")
34
+
35
+ submit :delete, mailbox_url(domain, mailbox_name)
36
+ end
37
+
38
+
39
+ private
40
+
41
+ # Helper method to generate the proper url for Mailgun mailbox API calls
42
+ def mailbox_url(domain, mailbox_name=nil)
43
+ "#{@mailgun.base_url}/#{domain}/mailboxes#{'/' + mailbox_name if mailbox_name}"
44
+ end
45
+
46
+
47
+ # Submits the API call to the Mailgun server
48
+ def submit(method, url, parameters={})
49
+ JSON(RestClient.send(method, url, parameters))
50
+ end
51
+ end
52
+ end
data/lib/mailgun.rb CHANGED
@@ -1,5 +1,9 @@
1
- require "mailgun/version"
1
+ require "rest-client"
2
+ require "json"
2
3
 
3
- module Mailgun
4
- # Your code goes here...
4
+ require 'mailgun/base'
5
+ require 'mailgun/mailbox'
6
+
7
+ def Mailgun(options)
8
+ Mailgun::Base.new(options)
5
9
  end
data/mailgun.gemspec CHANGED
@@ -1,11 +1,10 @@
1
1
  # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/mailgun/version', __FILE__)
3
2
 
4
3
  Gem::Specification.new do |gem|
5
- gem.authors = ["Akash Manohar J"]
6
- gem.email = ["akash@akash.im"]
4
+ gem.authors = ["Akash Manohar J", "Sean Grove"]
5
+ gem.email = ["akash@akash.im", "s@bushi.do"]
7
6
  gem.description = %q{Mailgun library for Ruby}
8
- gem.summary = %q{The gem itself is a todo for now}
7
+ gem.summary = %q{Idiomatic library for using the mailgun API from within ruby}
9
8
  gem.homepage = ""
10
9
 
11
10
  gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
@@ -13,5 +12,5 @@ Gem::Specification.new do |gem|
13
12
  gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
13
  gem.name = "mailgun"
15
14
  gem.require_paths = ["lib"]
16
- gem.version = Mailgun::VERSION
15
+ gem.version = "0.0.2"
17
16
  end
data/spec/base_spec.rb ADDED
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mailgun::Base do
4
+
5
+ before :each do
6
+ @mailgun = Mailgun({:api_key => "some-junk-string"}) # used to get the default values
7
+
8
+ @sample_options = {
9
+ :api_key => @mailgun.api_key,
10
+ :api_version => @mailgun.api_version,
11
+ :protocol => @mailgun.protocol,
12
+ :mailgun_host => @mailgun.mailgun_host
13
+ }
14
+ end
15
+
16
+ describe "new" do
17
+ it "Mailgun() method should return a new Mailgun object" do
18
+ @mailgun.should be_kind_of(Mailgun::Base)
19
+ end
20
+
21
+ it "should use https by default" do
22
+ @mailgun.protocol.should == "https"
23
+ end
24
+ end
25
+
26
+ describe "base_url" do
27
+ it "should return https url if use_https is true" do
28
+ mailgun = Mailgun(@sample_options)
29
+ mailgun.send(:base_url).should == "https://api:#{mailgun.api_key}@#{mailgun.mailgun_host}/#{mailgun.api_version}"
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mailgun::Mailbox do
4
+
5
+ before :each do
6
+ @mailgun = Mailgun({:api_key => "api-key"}) # used to get the default values
7
+
8
+ @mailbox_options = {
9
+ :email => "test@sample.mailgun.com",
10
+ :name => "test",
11
+ :domain => "sample.mailgun.com"
12
+ }
13
+ end
14
+
15
+ describe "list_mailboxes" do
16
+ it "should make a get request with the right params" do
17
+ RestClient.should_receive(:get)
18
+ .with("#{@mailgun.send(:base_url)}/#{@mailbox_options[:domain]}/mailboxes", {}).and_return("{}")
19
+
20
+ @mailgun.mailboxes.list @mailbox_options[:domain]
21
+ end
22
+ end
23
+
24
+ describe "create mailbox" do
25
+ it "should make a post request with the right params" do
26
+ RestClient.should_receive(:post)
27
+ .with("#{@mailgun.send(:base_url)}/#{@mailbox_options[:domain]}/mailboxes",
28
+ :mailbox => @mailbox_options[:email],
29
+ :password => @mailbox_options[:password])
30
+ .and_return({})
31
+
32
+ @mailgun.mailboxes.create(@mailbox_options[:email], @mailbox_options[:password])
33
+ end
34
+ end
35
+
36
+ describe "update mailbox password" do
37
+ it "should make a put request with the right params" do
38
+ RestClient.should_receive(:put)
39
+ .with("#{@mailgun.send(:base_url)}/#{@mailbox_options[:domain]}/mailboxes/#{@mailbox_options[:name]}",
40
+ :password => @mailbox_options[:password])
41
+ .and_return({})
42
+
43
+ @mailgun.mailboxes.update_password @mailbox_options[:email],
44
+ @mailbox_options[:password]
45
+ end
46
+ end
47
+
48
+ describe "destroy mailbox" do
49
+ it "should make a put request with the right params" do
50
+ RestClient.should_receive(:delete)
51
+ .with("#{@mailgun.send(:base_url)}/#{@mailbox_options[:domain]}/mailboxes/#{@mailbox_options[:name]}", {})
52
+ .and_return({})
53
+
54
+ @mailgun.mailboxes.destroy @mailbox_options[:email]
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,7 @@
1
+ $:.unshift File.expand_path("../lib", File.dirname(__FILE__))
2
+
3
+ require "rspec"
4
+ require "mailgun"
5
+
6
+ RSpec.configure do |config|
7
+ end
metadata CHANGED
@@ -1,11 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailgun
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Akash Manohar J
9
+ - Sean Grove
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
@@ -14,16 +15,24 @@ dependencies: []
14
15
  description: Mailgun library for Ruby
15
16
  email:
16
17
  - akash@akash.im
18
+ - s@bushi.do
17
19
  executables: []
18
20
  extensions: []
19
21
  extra_rdoc_files: []
20
22
  files:
21
23
  - .gitignore
22
24
  - Gemfile
25
+ - LICENSE
26
+ - README.md
23
27
  - Rakefile
24
28
  - lib/mailgun.rb
25
- - lib/mailgun/version.rb
29
+ - lib/mailgun/base.rb
30
+ - lib/mailgun/mail.rb
31
+ - lib/mailgun/mailbox.rb
26
32
  - mailgun.gemspec
33
+ - spec/base_spec.rb
34
+ - spec/mailbox_spec.rb
35
+ - spec/spec_helper.rb
27
36
  homepage: ''
28
37
  licenses: []
29
38
  post_install_message:
@@ -47,5 +56,8 @@ rubyforge_project:
47
56
  rubygems_version: 1.8.10
48
57
  signing_key:
49
58
  specification_version: 3
50
- summary: The gem itself is a todo for now
51
- test_files: []
59
+ summary: Idiomatic library for using the mailgun API from within ruby
60
+ test_files:
61
+ - spec/base_spec.rb
62
+ - spec/mailbox_spec.rb
63
+ - spec/spec_helper.rb
@@ -1,3 +0,0 @@
1
- module Mailgun
2
- VERSION = "0.0.1"
3
- end