mailgun 0.5 → 0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,22 +1,17 @@
1
1
  module Mailgun
2
- class List
2
+
3
+ # Mailing List functionality
4
+ # Refer http://documentation.mailgun.net/api-mailinglists.html for optional parameters
5
+
6
+ class MailingList
3
7
  # Used internally, called from Mailgun::Base
4
8
  def initialize(mailgun)
5
9
  @mailgun = mailgun
6
10
  end
7
-
8
-
9
- ## List functionality
10
-
11
- # TODO add default domain functionality for the address names of lists
12
-
13
- # List all mailing lists
14
- def all
15
- response = Mailgun.submit :get, list_url
16
11
 
17
- if response
18
- response["items"].collect {|item| item["address"]}
19
- end
12
+ # List all mailing lists
13
+ def list(options={})
14
+ response = Mailgun.submit(:get, list_url, options)["items"] || []
20
15
  end
21
16
 
22
17
  # List a single mailing list by a given address
@@ -25,21 +20,16 @@ module Mailgun
25
20
  end
26
21
 
27
22
  # Create a mailing list with a given address
28
- # with an optional name and description
29
- def create(address, name=nil, description=nil)
23
+ def create(address, options={})
30
24
  params = {:address => address}
31
- params[:name] = name if name
32
- params[:description] = description if description
33
- Mailgun.submit :post, list_url, params
25
+ Mailgun.submit :post, list_url, params.merge(options)
34
26
  end
35
27
 
36
28
  # Update a mailing list with a given address
37
29
  # with an optional new address, name or description
38
- def update(address, new_address, name=nil, description=nil)
39
- params = {:address => new_address}
40
- params[:name] = name if name
41
- params[:description] = description if description
42
- Mailgun.submit :put, list_url(address), params
30
+ def update(address, new_address, options={})
31
+ params = {:address => new_address}
32
+ Mailgun.submit :put, list_url(address), params.merge(options)
43
33
  end
44
34
 
45
35
  # Deletes a mailing list with a given address
@@ -55,5 +45,5 @@ module Mailgun
55
45
  "#{@mailgun.base_url}/lists#{'/' + address if address}"
56
46
  end
57
47
 
58
- end
48
+ end
59
49
  end
@@ -1,56 +1,52 @@
1
1
  module Mailgun
2
- class List::Member
2
+ # List Member functionality
3
+ # Refer Mailgun docs for optional params
4
+ class MailingList::Member
5
+
3
6
  # Used internally, called from Mailgun::Base
4
- def initialize(mailgun)
7
+ def initialize(mailgun, address)
5
8
  @mailgun = mailgun
9
+ @address = address
6
10
  end
7
-
8
- ## List Member functionality
9
11
 
10
12
  # List all mailing list members
11
- # TODO add parameters: subscribed, limit, skip
12
- def list(address)
13
- response = Mailgun.submit :get, list_member_url(address)
14
-
15
- if response
16
- response["items"].collect {|item| item["address"]}
17
- end
13
+ def list(options={})
14
+ response = Mailgun.submit(:get, list_member_url, options)["items"]
18
15
  end
19
16
 
20
17
  # List a single mailing list member by a given address
21
- def find(address, member_address)
22
- Mailgun.submit :get, list_member_url(address, member_address)
18
+ def find(member_address)
19
+ Mailgun.submit :get, list_member_url(member_address)
23
20
  end
24
21
 
22
+
25
23
  # Adds a mailing list member with a given address
26
- # TODO add name, vars, subscribed, upsert
27
- def add(address, member_address, name=nil, vars={}, subscribed='yes', upsert='no')
28
- params = {:address => member_address, :subscribed => subscribed, :upsert => 'no'}
29
- params[:name] = name if name
30
- params[:vars] = vars unless vars.empty?
31
- Mailgun.submit :post, list_member_url(address), params
24
+ # NOTE Use create instead of add?
25
+ def add(member_address, options={})
26
+ params = {:address => member_address}
27
+ Mailgun.submit :post, list_member_url, params.merge(options)
32
28
  end
33
29
 
30
+ # TODO add spec?
31
+ alias_method :create, :add
32
+
34
33
  # Update a mailing list member with a given address
35
- # with an optional new member_address, name, vars and subscribed
36
- def update(address, member_address, name=nil, vars={}, subscribed='yes')
37
- params = {:address => member_address, :subscribed => subscribed}
38
- params[:name] = name if name
39
- params[:vars] = vars unless vars.empty?
40
- Mailgun.submit :put, list_member_url(address, member_address), params
34
+ def update(member_address, options={})
35
+ params = {:address => member_address}
36
+ Mailgun.submit :put, list_member_url(member_address), params.merge(options)
41
37
  end
42
38
 
43
39
  # Deletes a mailing list member with a given address
44
- def remove(address, member_address)
45
- Mailgun.submit :delete, list_member_url(address, member_address)
40
+ def remove(member_address)
41
+ Mailgun.submit :delete, list_member_url(member_address)
46
42
  end
47
43
 
48
44
 
49
45
  private
50
46
 
51
47
  # Helper method to generate the proper url for Mailgun mailbox API calls
52
- def list_member_url(address, member_address=nil)
53
- "#{@mailgun.base_url}/lists#{'/' + address}/members#{'/' + member_address if member_address}"
48
+ def list_member_url(member_address=nil)
49
+ "#{@mailgun.base_url}/lists#{'/' + @address}/members#{'/' + member_address if member_address}"
54
50
  end
55
51
 
56
52
  end
@@ -1,25 +1,22 @@
1
1
  module Mailgun
2
2
  class Log
3
3
  # Used internally, called from Mailgun::Base
4
- def initialize(mailgun)
4
+ def initialize(mailgun, domain)
5
5
  @mailgun = mailgun
6
+ @domain = domain
6
7
  end
7
8
 
8
9
  # List all logs for a given domain
9
10
  # * domain the domain for which all complaints will listed
10
- def list(domain=Mailgun.domain, limit=100, skip=0)
11
- response = Mailgun.submit :get, log_url(domain), {:limit => limit, :skip => skip}
12
-
13
- if response
14
- response["items"].collect {|item| item["message"]}
15
- end
11
+ def list(options={})
12
+ Mailgun.submit(:get, log_url, options)
16
13
  end
17
14
 
18
15
  private
19
16
 
20
17
  # Helper method to generate the proper url for Mailgun complaints API calls
21
- def log_url(domain)
22
- "#{@mailgun.base_url}/#{domain}/log"
18
+ def log_url
19
+ "#{@mailgun.base_url}/#{@domain}/log"
23
20
  end
24
21
 
25
22
  end
@@ -2,49 +2,49 @@ module Mailgun
2
2
  class Mailbox
3
3
 
4
4
  # Used internally, called from Mailgun::Base
5
- def initialize(mailgun)
5
+ def initialize(mailgun, domain)
6
6
  @mailgun = mailgun
7
+ @domain = domain
7
8
  end
8
9
 
9
10
  # List all mailboxes for a given domain
10
11
  # * domain the domain for which all mailboxes will listed
11
- def list(domain = Mailgun.domain)
12
- response = Mailgun.submit :get, mailbox_url(domain)
13
-
14
- if response
15
- response["items"].collect {|item| item["mailbox"]}
16
- end
12
+ def list(options={})
13
+ Mailgun.submit(:get, mailbox_url, options)["items"]
17
14
  end
18
15
 
19
16
 
20
17
  # Creates a mailbox on the Mailgun server with the given password
21
- def create(address, password)
22
- Mailgun.submit :post, mailbox_url(address.split("@").last), :mailbox => address,
23
- :password => password
18
+ def create(mailbox_name, password)
19
+ address = "#{mailbox_name}@#{@domain}"
20
+ Mailgun.submit(
21
+ :post,
22
+ mailbox_url,
23
+ {
24
+ :mailbox => address,
25
+ :password => password
26
+ }
27
+ )
24
28
  end
25
29
 
26
30
 
27
31
  # Sets the password for a mailbox
28
- def update_password(address, password)
29
- mailbox_name, domain = address.split("@")
30
-
31
- Mailgun.submit :put, mailbox_url(domain, mailbox_name), :password => password
32
+ def update_password(mailbox_name, password)
33
+ Mailgun.submit :put, mailbox_url(mailbox_name), :password => password
32
34
  end
33
35
 
34
36
 
35
37
  # Destroys the mailbox
36
- def destroy(address)
37
- mailbox_name, domain = address.split("@")
38
-
39
- Mailgun.submit :delete, mailbox_url(domain, mailbox_name)
38
+ def destroy(mailbox_name)
39
+ Mailgun.submit :delete, mailbox_url(mailbox_name)
40
40
  end
41
41
 
42
42
 
43
43
  private
44
44
 
45
45
  # Helper method to generate the proper url for Mailgun mailbox API calls
46
- def mailbox_url(domain, mailbox_name=nil)
47
- "#{@mailgun.base_url}/#{domain}/mailboxes#{'/' + mailbox_name if mailbox_name}"
46
+ def mailbox_url(mailbox_name=nil)
47
+ "#{@mailgun.base_url}/#{@domain}/mailboxes#{'/' + mailbox_name if mailbox_name}"
48
48
  end
49
49
 
50
50
  end
@@ -1,13 +1,13 @@
1
1
  module Mailgun
2
- class Mail
3
-
4
- def initialize(mailgun)
2
+ class Message
3
+ def initialize(mailgun, domain)
5
4
  @mailgun = mailgun
5
+ @domain = domain
6
6
  end
7
7
 
8
8
  # send email
9
- def send_email()
10
- # TODO with the following options
9
+ def send_email(parameters={})
10
+ # options:
11
11
  # :from, :to, :cc, :bcc, :subject, :text, :html
12
12
  # :with_attachment
13
13
  # :with_attachments
@@ -15,6 +15,14 @@ module Mailgun
15
15
  # :in_test_mode BOOL. override the @use_test_mode setting
16
16
  # :tags to add tags to the email
17
17
  # :track BOOL
18
+ Mailgun.submit(:post, messages_url, parameters)
19
+ end
20
+
21
+ #private
22
+
23
+ # Helper method to generate the proper url for Mailgun message API calls
24
+ def messages_url
25
+ "#{@mailgun.base_url}/#{@domain}/messages"
18
26
  end
19
27
  end
20
28
  end
@@ -5,8 +5,8 @@ module Mailgun
5
5
  @mailgun = mailgun
6
6
  end
7
7
 
8
- def list(limit=100, skip=0)
9
- Mailgun.submit(:get, route_url, :limit => limit, :skip => skip)["items"] || []
8
+ def list(options={})
9
+ Mailgun.submit(:get, route_url, options)["items"] || []
10
10
  end
11
11
 
12
12
  def find(route_id)
@@ -1,37 +1,33 @@
1
1
  module Mailgun
2
2
  class Unsubscribe
3
3
  # Used internally, called from Mailgun::Base
4
- def initialize(mailgun)
4
+ def initialize(mailgun, domain)
5
5
  @mailgun = mailgun
6
+ @domain = domain
6
7
  end
7
8
 
8
- # List all unsubscribes for a given domain
9
- # * domain the domain for which all unsubscribes will listed
10
- def list(domain = Mailgun.domain)
11
- response = Mailgun.submit :get, unsubscribe_url(domain)
12
-
13
- if response
14
- response["items"].collect {|item| item["address"]}
15
- end
9
+ # List all unsubscribes for the domain
10
+ def list(options={})
11
+ Mailgun.submit(:get, unsubscribe_url, options)["items"]
16
12
  end
17
13
 
18
- def find(domain = Mailgun.domain, email)
19
- Mailgun.submit :get, unsubscribe_url(domain, email)
14
+ def find(email)
15
+ Mailgun.submit :get, unsubscribe_url(email)
20
16
  end
21
17
 
22
- def add(email, domain=Mailgun.domain, tag='*')
23
- Mailgun.submit :post, unsubscribe_url(domain), {:address => email, :tag => tag}
18
+ def add(email, tag='*')
19
+ Mailgun.submit :post, unsubscribe_url, {:address => email, :tag => tag}
24
20
  end
25
21
 
26
- def remove(domain = Mailgun.domain, email)
27
- Mailgun.submit :delete, unsubscribe_url(domain, email)
22
+ def remove(email)
23
+ Mailgun.submit :delete, unsubscribe_url(email)
28
24
  end
29
25
 
30
26
  private
31
27
 
32
28
  # Helper method to generate the proper url for Mailgun unsubscribe API calls
33
- def unsubscribe_url(domain, address=nil)
34
- "#{@mailgun.base_url}/#{domain}/unsubscribes#{'/' + address if address}"
29
+ def unsubscribe_url(address=nil)
30
+ "#{@mailgun.base_url}/#{@domain}/unsubscribes#{'/' + address if address}"
35
31
  end
36
32
 
37
33
  end
@@ -12,8 +12,11 @@ Gem::Specification.new do |gem|
12
12
  gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
13
  gem.name = "mailgun"
14
14
  gem.require_paths = ["lib"]
15
- gem.version = "0.5"
15
+ gem.version = "0.6"
16
16
 
17
17
  gem.add_dependency(%q<rest-client>, [">= 0"])
18
18
  gem.add_dependency(%q<multimap>, [">= 0"])
19
+
20
+ gem.add_development_dependency(%q<rspec>, [">= 2"])
21
+ gem.add_development_dependency(%q<debugger>, [">= 0"])
19
22
  end
@@ -5,7 +5,7 @@ describe Mailgun::Bounce do
5
5
  before :each do
6
6
  @mailgun = Mailgun({:api_key => "api-key"}) # used to get the default values
7
7
 
8
- @bounce_options = {
8
+ @sample = {
9
9
  :email => "test@sample.mailgun.org",
10
10
  :name => "test",
11
11
  :domain => "sample.mailgun.org"
@@ -15,32 +15,39 @@ describe Mailgun::Bounce do
15
15
  describe "list bounces" do
16
16
  it "should make a GET request with the right params" do
17
17
  sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.mailgun.org\" } ]}"
18
- RestClient.should_receive(:get).with("#{@mailgun.bounces.send(:bounce_url, @bounce_options[:domain])}", {}).and_return(sample_response)
18
+ bounces_url = @mailgun.bounces(@sample[:domain]).send(:bounce_url)
19
+
20
+ Mailgun.should_receive(:submit).
21
+ with(:get, bounces_url, {}).
22
+ and_return(sample_response)
19
23
 
20
- @mailgun.bounces.list @bounce_options[:domain]
24
+ @mailgun.bounces(@sample[:domain]).list
21
25
  end
22
26
  end
23
27
 
24
28
  describe "find bounces" do
25
29
  it "should make a GET request with correct params to find given email address" do
26
30
  sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.mailgun.org\" } ]}"
27
- RestClient.should_receive(:get)
28
- .with("#{@mailgun.bounces.send(:bounce_url, @bounce_options[:domain], @bounce_options[:email])}", {})
29
- .and_return(sample_response)
31
+ bounces_url = @mailgun.bounces(@sample[:domain]).send(:bounce_url, @sample[:email])
32
+
33
+ Mailgun.should_receive(:submit).
34
+ with(:get, bounces_url).
35
+ and_return(sample_response)
30
36
 
31
- @mailgun.bounces.find(@bounce_options[:domain], @bounce_options[:email])
37
+ @mailgun.bounces(@sample[:domain]).find(@sample[:email])
32
38
  end
33
39
  end
34
40
 
35
41
  describe "add bounces" do
36
42
  it "should make a POST request with correct params to add a given email address" do
37
- #sample_response = "{\"message\"=>\"Address has been added to the bounces table\", \"address\"=>\"#{@bounce_options[:email]}\"}"
38
43
  sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.mailgun.org\" } ]}"
39
- RestClient.should_receive(:post)
40
- .with("#{@mailgun.bounces.send(:bounce_url, @bounce_options[:domain])}", {:address => @bounce_options[:email]})
41
- .and_return(sample_response)
44
+ bounces_url = @mailgun.bounces(@sample[:domain]).send(:bounce_url)
45
+
46
+ Mailgun.should_receive(:submit).
47
+ with(:post, bounces_url, {:address => @sample[:email]} ).
48
+ and_return(sample_response)
42
49
 
43
- @mailgun.bounces.add(@bounce_options[:domain], @bounce_options[:email])
50
+ @mailgun.bounces(@sample[:domain]).add(@sample[:email])
44
51
  end
45
52
  end
46
53
 
@@ -3,9 +3,9 @@ require 'spec_helper'
3
3
  describe Mailgun::Complaint do
4
4
 
5
5
  before :each do
6
- @mailgun = Mailgun({:api_key => "api-key"}) # used to get the default values
6
+ @mailgun = Mailgun({:api_key => "api-key"})
7
7
 
8
- @complaint_options = {
8
+ @sample = {
9
9
  :email => "test@sample.mailgun.org",
10
10
  :name => "test",
11
11
  :domain => "sample.mailgun.org"
@@ -14,42 +14,89 @@ describe Mailgun::Complaint do
14
14
 
15
15
  describe "list complaints" do
16
16
  it "should make a GET request with the right params" do
17
- sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.mailgun.org\" } ]}"
18
- RestClient.should_receive(:get).with("#{@mailgun.complaints.send(:complaint_url, @complaint_options[:domain])}", {}).and_return(sample_response)
17
+ sample_response = <<EOF
18
+ {
19
+ "total_count": 1,
20
+ "items": [
21
+ {
22
+ "count": 2,
23
+ "created_at": "Tue, 15 Nov 2011 08:25:11 GMT",
24
+ "address": "romanto@profista.com"
25
+ }
26
+ ]
27
+ }
28
+ EOF
19
29
 
20
- @mailgun.complaints.list @complaint_options[:domain]
30
+ complaints_url = @mailgun.complaints(@sample[:domain]).send(:complaint_url)
31
+
32
+ Mailgun.should_receive(:submit).
33
+ with(:get, complaints_url, {}).
34
+ and_return(sample_response)
35
+
36
+ @mailgun.complaints(@sample[:domain]).list
37
+ end
38
+ end
39
+
40
+
41
+ describe "add complaint" do
42
+ it "should make a POST request with correct params to add a given email address to complaint from a tag" do
43
+ sample_response = <<EOF
44
+ {
45
+ "message": "Address has been added to the complaints table",
46
+ "address": "#{@sample[:email]}"
47
+ }
48
+ EOF
49
+
50
+ complaints_url = @mailgun.complaints(@sample[:domain]).send(:complaint_url)
51
+
52
+ Mailgun.should_receive(:submit)
53
+ .with(:post, complaints_url, {:address => @sample[:email]})
54
+ .and_return(sample_response)
55
+
56
+ @mailgun.complaints(@sample[:domain]).add(@sample[:email])
21
57
  end
22
58
  end
23
59
 
60
+
24
61
  describe "find complaint" do
25
62
  it "should make a GET request with the right params to find given email address" do
26
- sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.mailgun.org\" } ]}"
27
- RestClient.should_receive(:get)
28
- .with("#{@mailgun.complaints.send(:complaint_url, @complaint_options[:domain], @complaint_options[:email])}", {})
29
- .and_return(sample_response)
63
+ sample_response = <<EOF
64
+ {
65
+ "complaint": {
66
+ "count": 2,
67
+ "created_at": "Tue, 15 Nov 2011 08:25:11 GMT",
68
+ "address": "romanto@profista.com"
69
+ }
70
+ }
71
+ EOF
72
+
73
+ complaints_url = @mailgun.complaints(@sample[:domain]).send(:complaint_url, @sample[:email])
74
+
75
+ Mailgun.should_receive(:submit)
76
+ .with(:get, complaints_url)
77
+ .and_return(sample_response)
30
78
 
31
- @mailgun.complaints.find(@complaint_options[:domain], @complaint_options[:email])
79
+ @mailgun.complaints(@sample[:domain]).find(@sample[:email])
32
80
  end
33
81
  end
34
82
 
83
+
35
84
  describe "delete complaint" do
36
85
  it "should make a DELETE request with correct params to remove a given email address" do
37
- response_message = "{\"message\"=>\"Complaint event has been removed\", \"address\"=>\"#{@complaint_options[:email]}\"}"
38
- Mailgun.should_receive(:submit)
39
- .with(:delete, "#{@mailgun.complaints.send(:complaint_url, @complaint_options[:domain], @complaint_options[:email])}")
40
- .and_return(response_message)
86
+ sample_response = <<EOF
87
+ {
88
+ "message": "Complaint event has been removed",
89
+ "address": "#{@sample[:email]}"}"
90
+ }
91
+ EOF
41
92
 
42
- @mailgun.complaints.remove(@complaint_options[:domain], @complaint_options[:email])
43
- end
44
- end
93
+ complaints_url = @mailgun.complaints(@sample[:domain]).send(:complaint_url, @sample[:email])
45
94
 
46
- describe "add complaint" do
47
- it "should make a POST request with correct params to add a given email address to complaint from a tag" do
48
- response_message = "{\"message\"=>\"Address has been added to the complaints table\", \"address\"=>\"#{@complaint_options[:email]}\"}"
49
95
  Mailgun.should_receive(:submit)
50
- .with(:post, "#{@mailgun.complaints.send(:complaint_url, @complaint_options[:domain])}",{:address=>@complaint_options[:email]})
51
- .and_return(response_message)
52
- @mailgun.complaints.add(@complaint_options[:domain],@complaint_options[:email])
96
+ .with(:delete, complaints_url)
97
+ .and_return(sample_response)
98
+
99
+ @mailgun.complaints(@sample[:domain]).destroy(@sample[:email])
53
100
  end
54
101
  end
55
102