mailgun-ruby 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,38 @@
1
+ X-SBRS: 5.6
2
+ X-SenderGroup: WHITELIST
3
+ X-MailFlowPolicy: $TRUSTED
4
+ DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=trstx.com; q=dns/txt; s=mx;
5
+ t=1381182797; h=Mime-Version: Content-Type: Subject: From: To:
6
+ Message-Id: Date: Sender: Content-Transfer-Encoding;
7
+ bh=r6P1omjKL3m7gDuZC6dZEJU6trgWm1IRwAJb8h4wtfg=; b=UPFaFGiuDx2yZjCmiMEji0fIXvGMNwXUsuaX4Ss9p5EUkqE25eYjeJaJZ5w5FK+t0jsUPZqu
8
+ FIH6sftQv7GBEbSktY6sv7dgv5q2yVlY8YNG7CXvUZdWmvwfQVvfL9j1RHJo9H3QpXT9c8bZ
9
+ p9rpYmqZtZEz2ZesPj4kzUaB0lU=
10
+ DomainKey-Signature: a=rsa-sha1; c=nofws; d=trstx.com; s=mx; q=dns;
11
+ h=Mime-Version: Content-Type: Subject: From: To: Message-Id: Date:
12
+ Sender: Content-Transfer-Encoding;
13
+ b=QUYQf9OP16DphNAjeF95+G7V6DaqL4FA+m1sjIEWHdLzrGTQkiYTpzJdne3lzSypB0DwaG
14
+ AT9zOhjjN64DWFOkJD2Ce5t6vfHa/GC3FCUMq3xz/Big94g+kOciawkHyUhCrmPyh/D7kWx1
15
+ J75UIAdmE9XQ9bCSPhZ2MRaOv7b8c=
16
+ Received: by luna.mailgun.net with HTTP; Mon, 07 Oct 2013 21:53:16 +0000
17
+ Content-Type: text/plain; charset="ascii"
18
+ Subject: Test Subject
19
+ From: <joe@example.com>
20
+ To: <bob@sample.com>
21
+ Message-ID: <20131007215316.27052.7818@example.com>
22
+ X-Mailgun-Sid: WyI4MzY1OSIsICJ0cmF2aXMuc3dpZW50ZWtAcmFja3NwYWasdfewNvbSIsICJjZjQ4Il0=
23
+ Date: Mon, 7 Oct 2013 21:53:17 +0000
24
+ Sender: <joe@example.com>
25
+ Content-Transfer-Encoding: 7bit
26
+ MIME-Version: 1.0
27
+
28
+ --3a8c26e9f23c4ac193606295aa17fa6f
29
+ Content-Type: text/plain; charset="ascii"
30
+ Content-Transfer-Encoding: 7bit
31
+
32
+ Testing some Mailgun awesomness!
33
+ --3a8c26e9f23c4ac193606295aa17fa6f
34
+ Content-Type: text/html; charset="ascii"
35
+ Content-Transfer-Encoding: 7bit
36
+
37
+ <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">Test
38
+ --3a8c26e9f23c4ac193606295aa17fa6f--
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ require 'bundler/setup'
6
+ Bundler.setup(:development)
7
+ require 'mailgun'
8
+ require_relative 'unit/connection/test_client'
9
+
10
+ # INSERT YOUR API KEYS HERE
11
+ APIKEY = "key"
12
+ PUB_APIKEY = "pubkey"
13
+ APIHOST = "api.mailgun.net"
14
+ APIVERSION = "v2"
15
+ SSL= true
@@ -0,0 +1,143 @@
1
+ require 'spec_helper'
2
+ require "time"
3
+ require "json"
4
+
5
+ module Mailgun
6
+ class UnitClient
7
+
8
+ attr_reader :url, :options, :block, :body, :code
9
+
10
+ def initialize(url, options={}, backwards_compatibility=nil, &block)
11
+ @url = url
12
+ @block = block
13
+ @options = options
14
+ @body = nil
15
+ @code = nil
16
+ end
17
+
18
+ def [](resource_path, &new_block)
19
+ case
20
+ when block_given? then self.class.new(resource_path, options, &new_block)
21
+ when block then self.class.new(resource_path, options, &block)
22
+ else
23
+ self.class.new(resource_path, options)
24
+ end
25
+ end
26
+
27
+ def post(options)
28
+ begin
29
+ response = response_generator("messages")
30
+ Response.new(response)
31
+ rescue Exception => e
32
+ raise CommunicationError.new(e), e.response
33
+ end
34
+ end
35
+
36
+ def get(options, query_string = nil)
37
+ begin
38
+ if query_string
39
+
40
+ response = response_generator("bounces")
41
+ else
42
+ response = response_generator("bounces")
43
+ end
44
+ Response.new(response)
45
+ rescue Exception => e
46
+ raise CommunicationError.new(e), e.response
47
+ end
48
+ end
49
+
50
+ def put(options)
51
+ begin
52
+ response = response_generator("lists")
53
+ Response.new(response)
54
+ rescue Exception => e
55
+ raise CommunicationError.new(e), e.response
56
+ end
57
+ end
58
+
59
+ def delete()
60
+ begin
61
+ response = response_generator("campaigns")
62
+ Response.new(response)
63
+ rescue Exception => e
64
+ raise CommunicationError.new(e), e.response
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ def response_generator(resource_endpoint)
71
+ case resource_endpoint
72
+ when "messages"
73
+ t = Time.now
74
+ id = "<#{t.to_i}.#{rand(99999999)}.5817@example.com>"
75
+ @body = JSON.generate({"message" => "Queued. Thank you.", "id" => id})
76
+ when "bounces"
77
+ @body = JSON.generate({"total_count" => 1, "items" => {"created_at" => "Fri, 21 Oct 2011 11:02:55 GMT", "code" => 550, "address" => "baz@example.com", "error" => "Message was not accepted -- invalid mailbox. Local mailbox baz@example.com is unavailable: user not found"}})
78
+ when "lists"
79
+ @body = JSON.generate({"member" => {"vars" => {"age" => 26}, "name" => "Foo Bar", "subscribed" => false, "address" => "bar@example.com"}, "message" => "Mailing list member has been updated"})
80
+ when "campaigns"
81
+ @body = JSON.generate({"message" => "Campaign has been deleted", "id" => "ABC123"})
82
+ end
83
+ self
84
+ end
85
+ end
86
+
87
+ class Response
88
+
89
+ attr_accessor :body
90
+
91
+ def initialize(response)
92
+ @body = response.body
93
+ end
94
+
95
+ # Return response as Ruby Hash
96
+ #
97
+ # @return [Hash] A standard Ruby Hash containing the HTTP result.
98
+
99
+ def to_hash
100
+ begin
101
+ JSON.parse(@body)
102
+ rescue Exception => e
103
+ raise ParseError.new(e), e
104
+ end
105
+ end
106
+
107
+ # Replace @body with Ruby Hash
108
+ #
109
+ # @return [Hash] A standard Ruby Hash containing the HTTP result.
110
+
111
+ def to_hash!
112
+ begin
113
+ @body = JSON.parse(@body)
114
+ rescue Exception => e
115
+ raise ParseError.new(e), e
116
+ end
117
+ end
118
+
119
+ # Return response as Yaml
120
+ #
121
+ # @return [String] A string containing response as YAML
122
+
123
+ def to_yaml
124
+ begin
125
+ YAML::dump(JSON.parse(@body))
126
+ rescue Exception => e
127
+ raise ParseError.new(e), e
128
+ end
129
+ end
130
+
131
+ # Replace @body with YAML
132
+ #
133
+ # @return [String] A string containing response as YAML
134
+
135
+ def to_yaml!
136
+ begin
137
+ @body = YAML::dump(JSON.parse(@body))
138
+ rescue Exception => e
139
+ raise ParseError.new(e), e
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'The method generate_hash' do
4
+ before(:each) do
5
+ @mailing_list = "mylist@example.com"
6
+ @secret_app_id = "mysupersecretpassword"
7
+ @recipient_address = "bob@example.com"
8
+ @precalculated_hash = "eyJzIjoiNDc4ZjVlM2M0MWQyNDdlZGQ2NDMzYzdkZTRjOTg3MTYiLCJsIjoi%0AbXlsaXN0QGV4YW1wbGUuY29tIiwiciI6ImJvYkBleGFtcGxlLmNvbSJ9%0A"
9
+ end
10
+
11
+ it 'generates a web safe hash for the recipient wishing to subscribe' do
12
+ my_hash = Mailgun::OptInHandler.generate_hash(@mailing_list, @secret_app_id, @recipient_address)
13
+ my_hash.should eq(@precalculated_hash)
14
+ end
15
+
16
+ it 'generates a web safe hash for the recipient wishing to subscribe' do
17
+ validate_result = Mailgun::OptInHandler.validate_hash(@secret_app_id, @precalculated_hash)
18
+ validate_result.length.should eq(2)
19
+ validate_result['recipient_address'].should eq(@recipient_address)
20
+ validate_result['mailing_list'].should eq(@mailing_list)
21
+ end
22
+ end
@@ -0,0 +1,131 @@
1
+ require 'spec_helper'
2
+
3
+ module Mailgun
4
+ class Client
5
+ def initialize(api_key, api_host="api.mailgun.net", api_version="v2")
6
+ @http_client = Mailgun::UnitClient::new(api_key, api_host, api_version)
7
+ end
8
+ end
9
+ end
10
+
11
+ describe 'Mailgun instantiation' do
12
+ it 'instantiates an HttpClient object' do
13
+ expect {@mg_obj = Mailgun::Client.new("Fake-API-Key")}.not_to raise_error
14
+ end
15
+ end
16
+
17
+ describe 'The method send_message()' do
18
+ before(:each) do
19
+ @mg_obj = Mailgun::Client.new("Fake-API-Key")
20
+ @domain = "test.com"
21
+ @list_address = "mylist@test.com"
22
+ @member_address = "subscribee@test.com"
23
+ end
24
+
25
+ it 'accepts only specific data types' do
26
+ @mb_obj = Mailgun::MessageBuilder.new()
27
+ @mm_obj = Multimap.new()
28
+ @mh_obj = {}
29
+
30
+ expect {@mg_obj.send_message("test.com", "incorrect data")}.to raise_error
31
+ expect {@mg_obj.send_message("test.com", @mb_obj)}.not_to raise_error
32
+ expect {@mg_obj.send_message("test.com", @mm_obj)}.not_to raise_error
33
+ expect {@mg_obj.send_message("test.com", @mh_obj)}.not_to raise_error
34
+ end
35
+
36
+ it 'sends a message' do
37
+ data = {'from' => 'joe@test.com',
38
+ 'to' => 'bob@example.com',
39
+ 'subject' => 'Test',
40
+ 'text' => 'Test Data'}
41
+ result = @mg_obj.send_message("testdomain.com", data)
42
+
43
+ result.to_hash!
44
+ result.body.should include("message")
45
+ result.body.should include("id")
46
+ end
47
+
48
+ it 'opens the message MIME and sends the MIME message.' do
49
+ data = {'to' => 'joe@test.com',
50
+ 'message' => 'Sample Data/mime.txt'}
51
+ result = @mg_obj.send_message("testdomain.com", data)
52
+
53
+ result.to_hash!
54
+ result.body.should include("message")
55
+ result.body.should include("id")
56
+ end
57
+ end
58
+
59
+ describe 'The method post()' do
60
+ before(:each) do
61
+ @mg_obj = Mailgun::Client.new("Fake-API-Key")
62
+ @domain = "test.com"
63
+ end
64
+ it 'in this case, sends a simple message.' do
65
+ data = {'from' => 'joe@test.com',
66
+ 'to' => 'bob@example.com',
67
+ 'subject' => 'Test',
68
+ 'text' => 'Test Data'}
69
+ result = @mg_obj.post("#{@domain}/messages", data)
70
+
71
+ result.to_hash!
72
+ result.body.should include("message")
73
+ result.body.should include("id")
74
+ end
75
+ end
76
+
77
+ describe 'The method put()' do
78
+ before(:each) do
79
+ @mg_obj = Mailgun::Client.new("Fake-API-Key")
80
+ @domain = "test.com"
81
+ @list_address = "mylist@test.com"
82
+ @member_address = "subscribee@test.com"
83
+ end
84
+
85
+ it 'in this case, updates a member with the attributes in data.' do
86
+ data = {'subscribed' => false,
87
+ 'name' => 'Foo Bar'}
88
+ result = @mg_obj.put("lists/#{@list_address}/members#{@member_address}", data)
89
+
90
+ result.to_hash!
91
+ result.body.should include("member")
92
+ result.body["member"].should include("vars")
93
+ result.body["member"]["vars"].should include("age")
94
+ result.body["member"].should include("name")
95
+ result.body["member"].should include("subscribed")
96
+ result.body["member"].should include("address")
97
+ result.body.should include("message")
98
+ end
99
+
100
+ end
101
+
102
+ describe 'The method get()' do
103
+ before(:each) do
104
+ @mg_obj = Mailgun::Client.new("Fake-API-Key")
105
+ @domain = "test.com"
106
+ end
107
+ it 'in this case, obtains a list of bounces for the domain, limit of 5, skipping the first 10.' do
108
+ query_string = {'skip' => '10',
109
+ 'limit' => '5'}
110
+ result = @mg_obj.get("#{@domain}/bounces", query_string)
111
+
112
+ result.to_hash!
113
+ result.body.should include("total_count")
114
+ result.body.should include("items")
115
+ end
116
+ end
117
+
118
+ describe 'The method delete()' do
119
+ before(:each) do
120
+ @mg_obj = Mailgun::Client.new("Fake-API-Key")
121
+ @domain = "test.com"
122
+ end
123
+
124
+ it 'issues a generic delete request.' do
125
+ result = @mg_obj.delete("#{@domain}/campaigns/ABC123")
126
+
127
+ result.to_hash!
128
+ result.body.should include("message")
129
+ result.body.should include("id")
130
+ end
131
+ end
@@ -0,0 +1,130 @@
1
+ require 'spec_helper'
2
+
3
+ module Mailgun
4
+ class Client
5
+ def initialize(api_key, api_host="api.mailgun.net", api_version="v2")
6
+ @http_client = UnitClient::new(api_key, api_host, api_version)
7
+ end
8
+ end
9
+ end
10
+
11
+ describe 'BatchMessage attribute readers' do
12
+ it 'should be readable' do
13
+ @mb_client = Mailgun::Client.new("My-Fake-API-Key")
14
+ @mb_obj = Mailgun::BatchMessage.new(@mb_client, "example.com")
15
+ @mb_obj.should respond_to(:message_ids)
16
+ @mb_obj.should respond_to(:message)
17
+ @mb_obj.should respond_to(:counters)
18
+ @mb_obj.should respond_to(:recipient_variables)
19
+ @mb_obj.should respond_to(:domain)
20
+ end
21
+ end
22
+
23
+ describe 'The instantiation of Batch Message' do
24
+
25
+ before(:each) do
26
+ @mb_client = Mailgun::Client.new("My-Fake-API-Key")
27
+ @mb_obj = Mailgun::BatchMessage.new(@mb_client, "example.com")
28
+ end
29
+
30
+ it 'contains Message, which should be of type Multimap and empty' do
31
+ @mb_obj.message.should be_a(Multimap)
32
+ @mb_obj.message.length.should eq(0)
33
+ end
34
+
35
+ it 'contains recipient_variables, which should be of type Hash and empty' do
36
+ @mb_obj.recipient_variables.should be_a(Hash)
37
+ @mb_obj.recipient_variables.length.should eq(0)
38
+ end
39
+
40
+ it 'contains domain, which should be of type string and contain example.com' do
41
+ @mb_obj.domain.should be_a(String)
42
+ @mb_obj.domain.should eq('example.com')
43
+ end
44
+
45
+ it 'contains message_ids, which should be of type hash and empty' do
46
+ @mb_obj.message_ids.should be_a(Hash)
47
+ @mb_obj.message_ids.length.should eq(0)
48
+ end
49
+
50
+ it 'contains counters, which should be of type hash and contain several important counters' do
51
+ @mb_obj.counters.should be_a(Hash)
52
+ @mb_obj.counters.should include(:recipients)
53
+ end
54
+
55
+ it 'contains counters, which should be of type hash and contain several important counters' do
56
+ @mb_obj.counters.should be_a(Hash)
57
+
58
+ @mb_obj.counters.should include(:recipients)
59
+ @mb_obj.counters[:recipients].should include(:to)
60
+ @mb_obj.counters[:recipients].should include(:cc)
61
+ @mb_obj.counters[:recipients].should include(:bcc)
62
+
63
+ @mb_obj.counters.should include(:attributes)
64
+ @mb_obj.counters[:attributes].should include(:attachment)
65
+ @mb_obj.counters[:attributes].should include(:campaign_id)
66
+ @mb_obj.counters[:attributes].should include(:custom_option)
67
+ @mb_obj.counters[:attributes].should include(:tag)
68
+ end
69
+ end
70
+
71
+ describe 'The method add_recipient' do
72
+ before(:each) do
73
+ @mb_client = Mailgun::Client.new("My-Fake-API-Key")
74
+ @mb_obj = Mailgun::BatchMessage.new(@mb_client, "example.com")
75
+ @address_1 = 'jane@example.com'
76
+ @variables_1 = {'first' => 'Jane', 'last' => 'Doe', 'tracking' => 'ABC123'}
77
+ @address_2 = 'bob@example.com'
78
+ @variables_2 = {'first' => 'Bob', 'last' => 'Doe', 'tracking' => 'DEF123'}
79
+ @address_3 = 'sam@example.com'
80
+ @variables_3 = {'first' => 'Sam', 'last' => 'Doe', 'tracking' => 'GHI123'}
81
+ end
82
+
83
+ it 'adds 1,000 recipients to the message body and validates counter is incremented then reset' do
84
+ recipient_type = :to
85
+ 1000.times do
86
+ @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
87
+ end
88
+ @mb_obj.counters[:recipients][recipient_type].should eq(1000)
89
+ @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
90
+ @mb_obj.counters[:recipients][recipient_type].should eq(1)
91
+ end
92
+
93
+ it 'adds recipients to the message, calls finalize, and cleans up' do
94
+ recipient_type = :to
95
+ 1000.times do
96
+ @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
97
+ end
98
+ @mb_obj.counters[:recipients][recipient_type].should eq(1000)
99
+ @mb_obj.finalize
100
+ @mb_obj.message['recipient-variables'].length.should eq(0)
101
+ @mb_obj.message[:to].length.should eq(0)
102
+ @mb_obj.counters[:recipients][recipient_type].should eq(0)
103
+ end
104
+
105
+ it 'adds 5,005 recipients to the message body and validates we receive message_ids back' do
106
+ recipient_type = :to
107
+ 5005.times do
108
+ @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
109
+ end
110
+ @mb_obj.finalize
111
+ @mb_obj.message_ids.length.should eq(6)
112
+ end
113
+
114
+ it 'sets recipient-variables, for batch expansion' do
115
+ recipient_type = :to
116
+ @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
117
+ @mb_obj.recipient_variables[@address_1].should eq(@variables_1)
118
+ end
119
+
120
+ it 'sets multiple recipient-variables, for batch expansion' do
121
+ recipient_type = :to
122
+ @mb_obj.add_recipient(recipient_type, @address_1, @variables_1)
123
+ @mb_obj.add_recipient(recipient_type, @address_2, @variables_2)
124
+ @mb_obj.add_recipient(recipient_type, @address_3, @variables_3)
125
+ @mb_obj.recipient_variables[@address_1].should eq(@variables_1)
126
+ @mb_obj.recipient_variables[@address_2].should eq(@variables_2)
127
+ @mb_obj.recipient_variables[@address_3].should eq(@variables_3)
128
+ end
129
+
130
+ end