alphamail 1.0.1.pre → 1.0.3
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.
- data/lib/alphamail.rb +30 -8
- data/spec/alphamail_test.rb +35 -12
- metadata +4 -5
data/lib/alphamail.rb
CHANGED
|
@@ -5,7 +5,7 @@ require 'net/http'
|
|
|
5
5
|
require 'json'
|
|
6
6
|
|
|
7
7
|
# Email contact class
|
|
8
|
-
class
|
|
8
|
+
class AlphaMailEmailContact
|
|
9
9
|
attr_accessor :name
|
|
10
10
|
attr_accessor :email
|
|
11
11
|
|
|
@@ -16,22 +16,27 @@ class EmailContact
|
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
# Message payload class
|
|
19
|
-
class
|
|
19
|
+
class AlphaMailEmailMessagePayload
|
|
20
20
|
attr_accessor :project_id
|
|
21
21
|
attr_accessor :receiver_id
|
|
22
22
|
attr_accessor :sender
|
|
23
23
|
attr_accessor :receiver
|
|
24
24
|
attr_accessor :body
|
|
25
25
|
|
|
26
|
-
def initialize(project_id = 0, receiver_id = 0, sender =
|
|
26
|
+
def initialize(project_id = 0, receiver_id = 0, sender = AlphaMailEmailContact.new('', ''), receiver = AlphaMailEmailContact.new('', ''), body = '')
|
|
27
27
|
@project_id = project_id
|
|
28
28
|
@receiver_id = receiver_id
|
|
29
29
|
@sender = sender
|
|
30
30
|
@receiver = receiver
|
|
31
31
|
@body = body
|
|
32
|
+
@body_json = ''
|
|
32
33
|
end
|
|
33
34
|
|
|
34
35
|
def to_json(*a)
|
|
36
|
+
if @body.respond_to?('to_json')
|
|
37
|
+
@body_json = @body.to_json
|
|
38
|
+
end
|
|
39
|
+
|
|
35
40
|
{
|
|
36
41
|
'project_id' => project_id,
|
|
37
42
|
'receiver_id' => receiver_id,
|
|
@@ -39,11 +44,20 @@ class EmailMessagePayload
|
|
|
39
44
|
'sender_email' => sender.email,
|
|
40
45
|
'receiver_name' => receiver.name,
|
|
41
46
|
'receiver_email' => receiver.email,
|
|
42
|
-
'body' =>
|
|
47
|
+
'body' => @body_json
|
|
43
48
|
|
|
44
49
|
}.to_json(*a)
|
|
45
50
|
end
|
|
51
|
+
end
|
|
46
52
|
|
|
53
|
+
class AlphaMailResult
|
|
54
|
+
attr_accessor :error_code
|
|
55
|
+
attr_accessor :message
|
|
56
|
+
|
|
57
|
+
def initialize(error_code = 0, message = '')
|
|
58
|
+
@error_code = error_code
|
|
59
|
+
@message = message
|
|
60
|
+
end
|
|
47
61
|
end
|
|
48
62
|
|
|
49
63
|
# Alpha-mail email service
|
|
@@ -60,12 +74,20 @@ class AlphaMailEmailService
|
|
|
60
74
|
req.basic_auth '', @api_token
|
|
61
75
|
req.body = payload.to_json
|
|
62
76
|
req.content_type = 'application/json'
|
|
63
|
-
res = Net::HTTP.start(
|
|
77
|
+
res = Net::HTTP.start(uri.host, uri.port) {|http|
|
|
64
78
|
http.request(req)
|
|
65
79
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
80
|
+
|
|
81
|
+
# Check response
|
|
82
|
+
case res.code
|
|
83
|
+
when '200' then
|
|
84
|
+
am_res = JSON.parse(res.body)
|
|
85
|
+
return AlphaMailResult.new(am_res['error_code'], am_res['message'])
|
|
86
|
+
else
|
|
87
|
+
am_res = JSON.parse(res.body)
|
|
88
|
+
return AlphaMailResult.new(am_res['error_code'], am_res['message'])
|
|
89
|
+
end
|
|
90
|
+
return AlphaMailResult.new(-1, 'Unknown error')
|
|
69
91
|
end
|
|
70
92
|
end
|
|
71
93
|
|
data/spec/alphamail_test.rb
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
require 'rubygems'
|
|
2
2
|
require 'alphamail'
|
|
3
3
|
|
|
4
|
-
class
|
|
4
|
+
class NewMember
|
|
5
5
|
attr_accessor :name
|
|
6
6
|
attr_accessor :password
|
|
7
7
|
|
|
8
|
-
def initialize(name = 'Unknown', password = '')
|
|
8
|
+
def initialize(name = 'Unknown', password = 'unset1')
|
|
9
9
|
@name = name
|
|
10
10
|
@password = password
|
|
11
11
|
end
|
|
@@ -19,15 +19,38 @@ class Member
|
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
# Demo 1 --------------------------------------------------
|
|
23
|
+
|
|
24
|
+
# The email content (in this case, member info)
|
|
25
|
+
myMember = NewMember.new('Ruby Diamond Ruben', 'my3Passw0rd89')
|
|
26
|
+
|
|
27
|
+
# The payload
|
|
28
|
+
myPayload = AlphaMailEmailMessagePayload.new
|
|
29
|
+
|
|
30
|
+
# Set payload fields
|
|
31
|
+
myPayload.project_id = 139
|
|
32
|
+
myPayload.receiver_id = 1
|
|
33
|
+
myPayload.sender.name = 'Jack Sender'
|
|
34
|
+
myPayload.sender.email = 'jack@example.com'
|
|
35
|
+
myPayload.receiver.name = 'John Doe'
|
|
36
|
+
myPayload.receiver.email = 'john.doe@example.com'
|
|
37
|
+
myPayload.body = myMember
|
|
38
|
+
|
|
39
|
+
myService = AlphaMailEmailService.new 'http://api.am1.comfirm.se/v1', 'YOUR-TOKEN-HERE'
|
|
40
|
+
am_res = myService.queue(myPayload)
|
|
41
|
+
puts "#{am_res.error_code} #{am_res.message}"
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
# Demo 2 ---------------------
|
|
45
|
+
|
|
46
|
+
# The payload
|
|
47
|
+
am_res = myService.queue(AlphaMailEmailMessagePayload.new(
|
|
48
|
+
139, # Project ID
|
|
49
|
+
2, # Sender ID
|
|
50
|
+
AlphaMailEmailContact.new('Jack Sender', 'jack@example.com'), # Sender
|
|
51
|
+
AlphaMailEmailContact.new('John Doe', 'john.doe@example.com'), # Receiver
|
|
52
|
+
NewMember.new('John Doe', 'wh4tswhAt') # Body object
|
|
53
|
+
))
|
|
54
|
+
puts "#{am_res.error_code} #{am_res.message}"
|
|
23
55
|
|
|
24
|
-
a = EmailMessagePayload.new
|
|
25
|
-
a.project_id = 139
|
|
26
|
-
a.sender.name = 'Jack'
|
|
27
|
-
a.sender.email = 'jack.johansson@comfirm.se'
|
|
28
|
-
a.receiver.name = 'Timothy'
|
|
29
|
-
a.receiver.email = 'jack@skysel.com'
|
|
30
|
-
a.body = m.to_json
|
|
31
56
|
|
|
32
|
-
service = AlphaMailEmailService.new 'http://api.am1.comfirm.se/v1', '4e8c684c47a2f0-50424246'
|
|
33
|
-
service.queue(a)
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: alphamail
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
hash:
|
|
5
|
-
prerelease:
|
|
4
|
+
hash: 17
|
|
5
|
+
prerelease: false
|
|
6
6
|
segments:
|
|
7
7
|
- 1
|
|
8
8
|
- 0
|
|
9
|
-
-
|
|
10
|
-
|
|
11
|
-
version: 1.0.1.pre
|
|
9
|
+
- 3
|
|
10
|
+
version: 1.0.3
|
|
12
11
|
platform: ruby
|
|
13
12
|
authors:
|
|
14
13
|
- Jack Engqvist Johansson
|