rubymail 0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +64 -0
  3. data/Gemfile +7 -0
  4. data/LICENSE +201 -0
  5. data/README.md +2 -0
  6. data/Rakefile +1 -0
  7. data/lib/multimap/.gitignore +4 -0
  8. data/lib/multimap/LICENSE +20 -0
  9. data/lib/multimap/README.rdoc +16 -0
  10. data/lib/multimap/Rakefile +34 -0
  11. data/lib/multimap/benchmarks/bm_nested_multimap_construction.rb +60 -0
  12. data/lib/multimap/benchmarks/bm_nested_multimap_lookup.rb +33 -0
  13. data/lib/multimap/ext/extconf.rb +6 -0
  14. data/lib/multimap/ext/nested_multimap_ext.c +24 -0
  15. data/lib/multimap/extras/graphing.rb +83 -0
  16. data/lib/multimap/lib/multimap.rb +569 -0
  17. data/lib/multimap/lib/multiset.rb +185 -0
  18. data/lib/multimap/lib/nested_multimap.rb +158 -0
  19. data/lib/multimap/spec/enumerable_examples.rb +50 -0
  20. data/lib/multimap/spec/hash_examples.rb +264 -0
  21. data/lib/multimap/spec/multimap_spec.rb +45 -0
  22. data/lib/multimap/spec/multiset_spec.rb +184 -0
  23. data/lib/multimap/spec/nested_multimap_spec.rb +202 -0
  24. data/lib/multimap/spec/set_examples.rb +301 -0
  25. data/lib/multimap/spec/spec_helper.rb +67 -0
  26. data/lib/rubymail/address.rb +17 -0
  27. data/lib/rubymail/base.rb +118 -0
  28. data/lib/rubymail/bounce.rb +31 -0
  29. data/lib/rubymail/client.rb +87 -0
  30. data/lib/rubymail/complaint.rb +31 -0
  31. data/lib/rubymail/domain.rb +34 -0
  32. data/lib/rubymail/list.rb +37 -0
  33. data/lib/rubymail/log.rb +19 -0
  34. data/lib/rubymail/mailbox.rb +41 -0
  35. data/lib/rubymail/message.rb +16 -0
  36. data/lib/rubymail/route.rb +99 -0
  37. data/lib/rubymail/rubymail_error.rb +53 -0
  38. data/lib/rubymail/secure.rb +19 -0
  39. data/lib/rubymail/unsubscribe.rb +31 -0
  40. data/lib/rubymail/webhook.rb +43 -0
  41. data/lib/rubymail.rb +31 -0
  42. data/rubymail.gemspec +18 -0
  43. data/spec/address_spec.rb +27 -0
  44. data/spec/base_spec.rb +132 -0
  45. data/spec/bounce_spec.rb +66 -0
  46. data/spec/client_spec.rb +118 -0
  47. data/spec/complaint_spec.rb +103 -0
  48. data/spec/domain_spec.rb +80 -0
  49. data/spec/helpers/rubymail_helper.rb +9 -0
  50. data/spec/list/member_spec.rb +82 -0
  51. data/spec/list/message_spec.rb +40 -0
  52. data/spec/list_spec.rb +70 -0
  53. data/spec/log_spec.rb +27 -0
  54. data/spec/mailbox_spec.rb +63 -0
  55. data/spec/route_spec.rb +100 -0
  56. data/spec/secure_spec.rb +54 -0
  57. data/spec/spec_helper.rb +10 -0
  58. data/spec/unsubscribe_spec.rb +82 -0
  59. data/spec/webhook_spec.rb +115 -0
  60. metadata +159 -0
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rubymail::MailingList::Member do
4
+
5
+ before :each do
6
+ @rubymail = Rubymail({:api_key => "api-key"})
7
+
8
+ @sample = {
9
+ :email => "test@sample.rubymail.org",
10
+ :list_email => "test_list@sample.rubymail.org",
11
+ :name => "test",
12
+ :domain => "sample.rubymail.org"
13
+ }
14
+ end
15
+
16
+ describe "list members" do
17
+ it "should make a GET request with the right params" do
18
+ sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.rubymail.org\" } ]}"
19
+ mailing_list_members_url = @rubymail.list_members(@sample[:list_email]).send(:list_member_url)
20
+
21
+ expect(Rubymail).to receive(:submit).
22
+ with(:get, mailing_list_members_url, {}).
23
+ and_return(sample_response)
24
+
25
+ @rubymail.list_members(@sample[:list_email]).list
26
+ end
27
+ end
28
+
29
+ describe "find member in list" do
30
+ it "should make a GET request with correct params to find given email address" do
31
+ sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.rubymail.org\" } ]}"
32
+ mailing_list_members_url = @rubymail.list_members(@sample[:list_email]).send(:list_member_url, @sample[:email])
33
+
34
+ expect(Rubymail).to receive(:submit).
35
+ with(:get, mailing_list_members_url).
36
+ and_return(sample_response)
37
+
38
+ @rubymail.list_members(@sample[:list_email]).find(@sample[:email])
39
+ end
40
+ end
41
+
42
+ describe "add member to list" do
43
+ it "should make a POST request with correct params to add a given email address" do
44
+ sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.rubymail.org\" } ]}"
45
+ mailing_list_members_url = @rubymail.list_members(@sample[:list_email]).send(:list_member_url)
46
+
47
+ expect(Rubymail).to receive(:submit).
48
+ with(:post, mailing_list_members_url, {
49
+ :address => @sample[:email]
50
+ }).
51
+ and_return(sample_response)
52
+
53
+ @rubymail.list_members(@sample[:list_email]).add(@sample[:email])
54
+ end
55
+ end
56
+
57
+ describe "update member in list" do
58
+ it "should make a PUT request with correct params" do
59
+ sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.rubymail.org\" } ]}"
60
+ expect(Rubymail).to receive(:submit).
61
+ with(:put, "#{@rubymail.list_members(@sample[:list_email]).send(:list_member_url, @sample[:email])}", {
62
+ :address => @sample[:email]
63
+ }).
64
+ and_return(sample_response)
65
+
66
+ @rubymail.list_members(@sample[:list_email]).update(@sample[:email])
67
+ end
68
+ end
69
+
70
+ describe "delete member from list" do
71
+ it "should make a DELETE request with correct params" do
72
+ sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.rubymail.org\" } ]}"
73
+ mailing_list_members_url = @rubymail.list_members(@sample[:list_email]).send(:list_member_url, @sample[:email])
74
+ expect(Rubymail).to receive(:submit).
75
+ with(:delete, mailing_list_members_url).
76
+ and_return(sample_response)
77
+
78
+ @rubymail.list_members(@sample[:list_email]).remove(@sample[:email])
79
+ end
80
+ end
81
+
82
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rubymail::Log do
4
+
5
+ before :each do
6
+ @rubymail = Rubymail({:api_key => "api-key"})
7
+
8
+ @sample = {
9
+ :email => "test@sample.rubymail.org",
10
+ :name => "test",
11
+ :domain => "sample.rubymail.org"
12
+ }
13
+ end
14
+
15
+ describe "send email" do
16
+ it "should make a POST request to send an email" do
17
+
18
+ sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.rubymail.org\" } ]}"
19
+ expect(Rubymail).to receive(:submit)
20
+ .with(:get, "#{@rubymail.lists.send(:list_url, @sample[:list_email])}")
21
+ .and_return(sample_response)
22
+
23
+ @rubymail.lists.find(@sample[:list_email])
24
+
25
+ sample_response = "{\"message\": \"Queued. Thank you.\",\"id\": \"<20111114174239.25659.5817@samples.rubymail.org>\"}"
26
+ parameters = {
27
+ :to => "cooldev@your.rubymail.domain",
28
+ :subject => "missing tps reports",
29
+ :text => "yeah, we're gonna need you to come in on friday...yeah.",
30
+ :from => "lumberg.bill@initech.rubymail.domain"
31
+ }
32
+ expect(Rubymail).to receive(:submit) \
33
+ .with(:post, @rubymail.messages.messages_url, parameters) \
34
+ .and_return(sample_response)
35
+
36
+ @rubymail.messages.send_email(parameters)
37
+ end
38
+ end
39
+
40
+ end
data/spec/list_spec.rb ADDED
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rubymail::MailingList do
4
+
5
+ before :each do
6
+ @rubymail = Rubymail({:api_key => "api-key"})
7
+
8
+ @sample = {
9
+ :email => "test@sample.rubymail.org",
10
+ :list_email => "dev@samples.rubymail.org",
11
+ :name => "test",
12
+ :domain => "sample.rubymail.org"
13
+ }
14
+ end
15
+
16
+ describe "list mailing lists" do
17
+ it "should make a GET request with the right params" do
18
+ sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.rubymail.org\" } ]}"
19
+ expect(Rubymail).to receive(:submit)
20
+ .with(:get, "#{@rubymail.lists.send(:list_url)}", {}).and_return(sample_response)
21
+
22
+ @rubymail.lists.list
23
+ end
24
+ end
25
+
26
+ describe "find list adress" do
27
+ it "should make a GET request with correct params to find given email address" do
28
+ sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.rubymail.org\" } ]}"
29
+ expect(Rubymail).to receive(:submit)
30
+ .with(:get, "#{@rubymail.lists.send(:list_url, @sample[:list_email])}")
31
+ .and_return(sample_response)
32
+
33
+ @rubymail.lists.find(@sample[:list_email])
34
+ end
35
+ end
36
+
37
+ describe "create list" do
38
+ it "should make a POST request with correct params to add a given email address" do
39
+ sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.rubymail.org\" } ]}"
40
+ expect(Rubymail).to receive(:submit)
41
+ .with(:post, "#{@rubymail.lists.send(:list_url)}", {:address => @sample[:list_email]})
42
+ .and_return(sample_response)
43
+
44
+ @rubymail.lists.create(@sample[:list_email])
45
+ end
46
+ end
47
+
48
+ describe "update list" do
49
+ it "should make a PUT request with correct params" do
50
+ sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.rubymail.org\" } ]}"
51
+ expect(Rubymail).to receive(:submit)
52
+ .with(:put, "#{@rubymail.lists.send(:list_url, @sample[:list_email])}", {:address => @sample[:email]})
53
+ .and_return(sample_response)
54
+
55
+ @rubymail.lists.update(@sample[:list_email], @sample[:email])
56
+ end
57
+ end
58
+
59
+ describe "delete list" do
60
+ it "should make a DELETE request with correct params" do
61
+ sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.rubymail.org\" } ]}"
62
+ expect(Rubymail).to receive(:submit)
63
+ .with(:delete, "#{@rubymail.lists.send(:list_url, @sample[:list_email])}")
64
+ .and_return(sample_response)
65
+
66
+ @rubymail.lists.delete(@sample[:list_email])
67
+ end
68
+ end
69
+
70
+ end
data/spec/log_spec.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rubymail::Log do
4
+
5
+ before :each do
6
+ @rubymail = Rubymail({:api_key => "api-key"})
7
+
8
+ @sample = {
9
+ :email => "test@sample.rubymail.org",
10
+ :name => "test",
11
+ :domain => "sample.rubymail.org"
12
+ }
13
+ end
14
+
15
+ describe "list log" do
16
+ it "should make a GET request with the right params" do
17
+ sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.rubymail.org\" } ]}"
18
+ log_url = @rubymail.log(@sample[:domain]).send(:log_url)
19
+ expect(Rubymail).to receive(:submit).
20
+ with(:get, log_url, {}).
21
+ and_return(sample_response)
22
+
23
+ @rubymail.log(@sample[:domain]).list
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rubymail::Mailbox do
4
+
5
+ before :each do
6
+ @rubymail = Rubymail({:api_key => "api-key"})
7
+
8
+ @sample = {
9
+ :email => "test@sample.rubymail.org",
10
+ :mailbox_name => "test",
11
+ :domain => "sample.rubymail.org"
12
+ }
13
+ end
14
+
15
+ describe "list mailboxes" do
16
+ it "should make a GET request with the right params" do
17
+ sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.rubymail.org\" } ]}"
18
+ mailboxes_url = @rubymail.mailboxes(@sample[:domain]).send(:mailbox_url)
19
+
20
+ expect(Rubymail).to receive(:submit).
21
+ with(:get,mailboxes_url, {}).
22
+ and_return(sample_response)
23
+
24
+ @rubymail.mailboxes(@sample[:domain]).list
25
+ end
26
+ end
27
+
28
+ describe "create mailbox" do
29
+ it "should make a POST request with the right params" do
30
+ mailboxes_url = @rubymail.mailboxes(@sample[:domain]).send(:mailbox_url)
31
+ expect(Rubymail).to receive(:submit)
32
+ .with(:post, mailboxes_url,
33
+ :mailbox => @sample[:email],
34
+ :password => @sample[:password])
35
+ .and_return({})
36
+
37
+ @rubymail.mailboxes(@sample[:domain]).create(@sample[:mailbox_name], @sample[:password])
38
+ end
39
+ end
40
+
41
+ describe "update mailbox" do
42
+ it "should make a PUT request with the right params" do
43
+ mailboxes_url = @rubymail.mailboxes(@sample[:domain]).send(:mailbox_url, @sample[:mailbox_name])
44
+ expect(Rubymail).to receive(:submit)
45
+ .with(:put, mailboxes_url, :password => @sample[:password])
46
+ .and_return({})
47
+
48
+ @rubymail.mailboxes(@sample[:domain]).
49
+ update_password(@sample[:mailbox_name], @sample[:password])
50
+ end
51
+ end
52
+
53
+ describe "destroy mailbox" do
54
+ it "should make a DELETE request with the right params" do
55
+ mailboxes_url = @rubymail.mailboxes(@sample[:domain]).send(:mailbox_url, @sample[:name])
56
+ expect(Rubymail).to receive(:submit)
57
+ .with(:delete, mailboxes_url)
58
+ .and_return({})
59
+
60
+ @rubymail.mailboxes(@sample[:domain]).destroy(@sample[:name])
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,100 @@
1
+ require "spec_helper"
2
+
3
+ describe Rubymail::Route do
4
+
5
+ before :each do
6
+ @rubymail = Rubymail({:api_key => "api-key"})
7
+ @sample_route_id = "a45cd"
8
+ end
9
+
10
+ describe "list routes" do
11
+ before :each do
12
+ sample_response = <<EOF
13
+ {
14
+ "total_count": 0,
15
+ "items": []
16
+ }
17
+ EOF.to_json
18
+
19
+ expect(Rubymail).to receive(:submit).
20
+ with(:get, "#{@rubymail.routes.send(:route_url)}", {}).
21
+ and_return(sample_response)
22
+ end
23
+
24
+ it "should make a GET request with the right params" do
25
+ @rubymail.routes.list
26
+ end
27
+
28
+ it "should respond with an Array" do
29
+ expect(@rubymail.routes.list).to be_kind_of(Array)
30
+ end
31
+ end
32
+
33
+ describe "get route" do
34
+ it "should make a GET request with the right params" do
35
+ sample_response = <<EOF
36
+ {
37
+ "route": {
38
+ "description": "Sample route",
39
+ "created_at": "Wed, 15 Feb 2012 13:03:31 GMT",
40
+ "actions": [
41
+ "forward(\"http://myhost.com/messages/\")",
42
+ "stop()"
43
+ ],
44
+ "priority": 1,
45
+ "expression": "match_recipient(\".*@samples.rubymail.org\")",
46
+ "id": "4f3bad2335335426750048c6"
47
+ }
48
+ }
49
+ EOF
50
+
51
+ expect(Rubymail).to receive(:submit).
52
+ with(:get, "#{@rubymail.routes.send(:route_url, @sample_route_id)}").
53
+ and_return(sample_response)
54
+
55
+ @rubymail.routes.find @sample_route_id
56
+ end
57
+ end
58
+
59
+ describe "create route" do
60
+ it "should make a POST request with the right params" do
61
+ options = {}
62
+
63
+ options[:description] = "test_route"
64
+ options[:priority] = 1
65
+ options[:expression] = [:match_recipient, "sample.rubymail.org"]
66
+ options[:action] = [[:forward, "http://test-site.com"], [:stop]]
67
+
68
+ expect(Rubymail).to receive(:submit)
69
+ .with(:post, @rubymail.routes.send(:route_url), instance_of(Hash))
70
+ .and_return("{\"route\": {\"id\": \"@sample_route_id\"}}")
71
+
72
+ @rubymail.routes.create(
73
+ options[:description],
74
+ options[:priority],
75
+ options[:expression],
76
+ options[:action],
77
+ )
78
+ end
79
+ end
80
+
81
+ describe "update route" do
82
+ it "should make a PUT request with the right params" do
83
+ options = { description: "test_route" }
84
+
85
+ expect(Rubymail).to receive(:submit)
86
+ .with(:put, "#{@rubymail.routes.send(:route_url, @sample_route_id)}", { 'description' => ["test_route"] })
87
+ .and_return("{\"id\": \"#{@sample_route_id}\"}")
88
+ @rubymail.routes.update @sample_route_id, options
89
+ end
90
+ end
91
+
92
+ describe "delete route" do
93
+ it "should make a DELETE request with the right params" do
94
+ expect(Rubymail).to receive(:submit).
95
+ with(:delete, "#{@rubymail.routes.send(:route_url, @sample_route_id)}").
96
+ and_return("{\"id\": \"#{@sample_route_id}\"}")
97
+ @rubymail.routes.destroy @sample_route_id
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+ require './spec/helpers/rubymail_helper.rb'
3
+
4
+ RSpec.configure do |c|
5
+ c.include RubymailHelper
6
+ end
7
+
8
+ describe Rubymail::Secure do
9
+
10
+ before :each do
11
+ @rubymail = Rubymail({:api_key => "some-api-key"}) # used to get the default values
12
+ end
13
+
14
+ it "generate_request_auth helper should generate a timestamp, a token and a signature" do
15
+ timestamp, token, signature = generate_request_auth("some-api-key")
16
+
17
+ expect(timestamp).to_not be_nil
18
+ expect(token.length).to eq 50
19
+ expect(signature.length).to eq 64
20
+ end
21
+
22
+ it "check_request_auth should return true for a recently generated authentication" do
23
+ timestamp, token, signature = generate_request_auth("some-api-key")
24
+
25
+ result = @rubymail.secure.check_request_auth(timestamp, token, signature)
26
+
27
+ expect(result).to be true
28
+ end
29
+
30
+ it "check_request_auth should return false for an authentication generated more than 5 minutes ago" do
31
+ timestamp, token, signature = generate_request_auth("some-api-key", -6)
32
+
33
+ result = @rubymail.secure.check_request_auth(timestamp, token, signature)
34
+
35
+ expect(result).to be false
36
+ end
37
+
38
+ it "check_request_auth should return true for an authentication generated any time when the check offset is 0" do
39
+ timestamp, token, signature = generate_request_auth("some-api-key", -6)
40
+
41
+ result = @rubymail.secure.check_request_auth(timestamp, token, signature, 0)
42
+
43
+ expect(result).to be true
44
+ end
45
+
46
+ it "check_request_auth should return false for a different api key, token or signature" do
47
+ timestamp, token, signature = generate_request_auth("some-different-api-key")
48
+
49
+ result = @rubymail.secure.check_request_auth(timestamp, token, signature)
50
+
51
+ expect(result).to be false
52
+ end
53
+
54
+ end
@@ -0,0 +1,10 @@
1
+ $:.unshift File.expand_path("../lib", File.dirname(__FILE__))
2
+ require 'simplecov'
3
+ SimpleCov.start
4
+
5
+ require "rspec"
6
+ require "pry"
7
+ require "rubymail"
8
+
9
+ RSpec.configure do |config|
10
+ end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rubymail::Unsubscribe do
4
+
5
+ before :each do
6
+ @rubymail = Rubymail({:api_key => "api-key"})
7
+
8
+ @sample = {
9
+ :email => "test@sample.rubymail.org",
10
+ :name => "test",
11
+ :domain => "sample.rubymail.org",
12
+ :tag => 'tag1'
13
+ }
14
+ end
15
+
16
+ describe "list unsubscribes" do
17
+ it "should make a GET request with the right params" do
18
+ sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.rubymail.org\" } ]}"
19
+ unsubscribes_url = @rubymail.unsubscribes(@sample[:domain]).send(:unsubscribe_url)
20
+ expect(Rubymail).to receive(:submit).
21
+ with(:get, unsubscribes_url, {}).
22
+ and_return(sample_response)
23
+
24
+ @rubymail.unsubscribes(@sample[:domain]).list
25
+ end
26
+ end
27
+
28
+ describe "find unsubscribe" do
29
+ it "should make a GET request with the right params to find given email address" do
30
+ sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.rubymail.org\" } ]}"
31
+ unsubscribes_url = @rubymail.unsubscribes(@sample[:domain]).send(:unsubscribe_url, @sample[:email])
32
+
33
+ expect(Rubymail).to receive(:submit)
34
+ .with(:get, unsubscribes_url)
35
+ .and_return(sample_response)
36
+
37
+ @rubymail.unsubscribes(@sample[:domain]).find(@sample[:email])
38
+ end
39
+ end
40
+
41
+ describe "delete unsubscribe" do
42
+ it "should make a DELETE request with correct params to remove a given email address" do
43
+ response_message = "{\"message\"=>\"Unsubscribe event has been removed\", \"address\"=>\"#{@sample[:email]}\"}"
44
+ unsubscribes_url = @rubymail.unsubscribes(@sample[:domain]).send(:unsubscribe_url, @sample[:email])
45
+
46
+ expect(Rubymail).to receive(:submit)
47
+ .with(:delete, unsubscribes_url)
48
+ .and_return(response_message)
49
+
50
+ @rubymail.unsubscribes(@sample[:domain]).remove(@sample[:email])
51
+ end
52
+ end
53
+
54
+ describe "add unsubscribe" do
55
+ context "to tag" do
56
+ it "should make a POST request with correct params to add a given email address to unsubscribe from a tag" do
57
+ response_message = "{\"message\"=>\"Address has been added to the unsubscribes table\", \"address\"=>\"#{@sample[:email]}\"}"
58
+ expect(Rubymail).to receive(:submit)
59
+ .with(:post, "#{@rubymail.unsubscribes(@sample[:domain]).send(:unsubscribe_url)}",{:address=>@sample[:email], :tag=>@sample[:tag]})
60
+ .and_return(response_message)
61
+
62
+ @rubymail.unsubscribes(@sample[:domain]).add(@sample[:email], @sample[:tag])
63
+ end
64
+ end
65
+
66
+ context "on all" do
67
+ it "should make a POST request with correct params to add a given email address to unsubscribe from all tags" do
68
+ sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.rubymail.org\" } ]}"
69
+ unsubscribes_url = @rubymail.unsubscribes(@sample[:domain]).send(:unsubscribe_url)
70
+
71
+ expect(Rubymail).to receive(:submit)
72
+ .with(:post, unsubscribes_url, {
73
+ :address => @sample[:email], :tag => '*'
74
+ })
75
+ .and_return(sample_response)
76
+
77
+ @rubymail.unsubscribes(@sample[:domain]).add(@sample[:email])
78
+ end
79
+ end
80
+ end
81
+
82
+ end
@@ -0,0 +1,115 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rubymail::Webhook do
4
+
5
+ before :each do
6
+ @rubymail = Rubymail({:api_key => "api-key", :webhook_url => "http://postbin.heroku.com/860bcd65"})
7
+
8
+ @sample = {
9
+ :id => "click",
10
+ :url => "http://postbin.heroku.com/860bcd65"
11
+ }
12
+ end
13
+
14
+ describe "list avabilable webhook ids" do
15
+ it "should return the correct ids" do
16
+ expect(@rubymail.webhooks.available_ids).to match_array %i(bounce deliver drop spam unsubscribe click open)
17
+ end
18
+ end
19
+
20
+ describe "list webhooks" do
21
+ it "should make a GET request with the correct params" do
22
+
23
+ sample_response = "{\"total_count\": 1, \"items\": [{\"webhooks\":{\"open\":{\"url\":\"http://postbin.heroku.com/860bcd65\"},\"click\":{\"url\":\"http://postbin.heroku.com/860bcd65\"}}}]}"
24
+ webhooks_url = @rubymail.webhooks.send(:webhook_url)
25
+
26
+ expect(Rubymail).to receive(:submit).
27
+ with(:get, webhooks_url).
28
+ and_return(sample_response)
29
+
30
+ @rubymail.webhooks.list
31
+ end
32
+ end
33
+
34
+ describe "find a webhook" do
35
+ it "should make a GET request with correct params to find a given webhook" do
36
+ sample_response = "{\"webhook\": {\"url\":\"http://postbin.heroku.com/860bcd65\"}"
37
+ webhooks_url = @rubymail.webhooks.send(:webhook_url, @sample[:id])
38
+
39
+ expect(Rubymail).to receive(:submit).
40
+ with(:get, webhooks_url).
41
+ and_return(sample_response)
42
+
43
+ @rubymail.webhooks.find(@sample[:id])
44
+ end
45
+ end
46
+
47
+ describe "add a webhook" do
48
+ context "using the default webhook url" do
49
+ it "should make a POST request with correct params to add a webhook" do
50
+ sample_response = "{\"message\":\"Webhook has been created\",\"webhook\":{\"url\":\"http://postbin.heroku.com/860bcd65\"}}"
51
+ webhooks_url = @rubymail.webhooks.send(:webhook_url)
52
+
53
+ expect(Rubymail).to receive(:submit).
54
+ with(:post, webhooks_url, {:id => @sample[:id], :url => @sample[:url]}).
55
+ and_return(sample_response)
56
+
57
+ @rubymail.webhooks.create(@sample[:id])
58
+ end
59
+ end
60
+ context "overwriting the default webhook url" do
61
+ it "should make a POST request with correct params to add a webhook" do
62
+ sample_response = "{\"message\":\"Webhook has been created\",\"webhook\":{\"url\":\"http://postbin.heroku.com/860bcd65\"}}"
63
+ webhooks_url = @rubymail.webhooks.send(:webhook_url)
64
+ overwritten_url = 'http://rubymail.net/webhook'
65
+
66
+ expect(Rubymail).to receive(:submit).
67
+ with(:post, webhooks_url, {:id => @sample[:id], :url => overwritten_url}).
68
+ and_return(sample_response)
69
+
70
+ @rubymail.webhooks.create(@sample[:id], overwritten_url)
71
+ end
72
+ end
73
+ end
74
+
75
+ describe "update a webhook" do
76
+ context "using the default webhook url" do
77
+ it "should make a POST request with correct params to add a webhook" do
78
+ sample_response = "{\"message\":\"Webhook has been updated\",\"webhook\":{\"url\":\"http://postbin.heroku.com/860bcd65\"}}"
79
+ webhooks_url = @rubymail.webhooks.send(:webhook_url, @sample[:id])
80
+
81
+ expect(Rubymail).to receive(:submit).
82
+ with(:put, webhooks_url, {:url => @sample[:url]}).
83
+ and_return(sample_response)
84
+
85
+ @rubymail.webhooks.update(@sample[:id])
86
+ end
87
+ end
88
+ context "overwriting the default webhook url" do
89
+ it "should make a POST request with correct params to add a webhook" do
90
+ sample_response = "{\"message\":\"Webhook has been updated\",\"webhook\":{\"url\":\"http://postbin.heroku.com/860bcd65\"}}"
91
+ webhooks_url = @rubymail.webhooks.send(:webhook_url, @sample[:id])
92
+ overwritten_url = 'http://rubymail.net/webhook'
93
+
94
+ expect(Rubymail).to receive(:submit).
95
+ with(:put, webhooks_url, {:url => overwritten_url}).
96
+ and_return(sample_response)
97
+
98
+ @rubymail.webhooks.update(@sample[:id], overwritten_url)
99
+ end
100
+ end
101
+ end
102
+
103
+ describe "delete a webhook" do
104
+ it "should make a DELETE request with correct params" do
105
+ sample_response = "{\"message\":\"Webhook has been deleted\",\"webhook\":{\"url\":\"http://postbin.heroku.com/860bcd65\"}}"
106
+ webhooks_url = @rubymail.webhooks.send(:webhook_url, @sample[:id])
107
+
108
+ expect(Rubymail).to receive(:submit).
109
+ with(:delete, webhooks_url).
110
+ and_return(sample_response)
111
+
112
+ @rubymail.webhooks.delete(@sample[:id])
113
+ end
114
+ end
115
+ end