alphamail 1.0.4.pre2 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +36 -2
- data/lib/alphamail.rb +72 -71
- data/spec/alphamail_test.rb +5 -5
- metadata +3 -4
data/README.rdoc
CHANGED
@@ -24,7 +24,7 @@ First you need to require alphamail:
|
|
24
24
|
require 'alphamail'
|
25
25
|
|
26
26
|
Create the payload:
|
27
|
-
myPayload =
|
27
|
+
myPayload = AlphaMail::MessagePayload.new
|
28
28
|
|
29
29
|
Now, set the fields:
|
30
30
|
# Set payload fields
|
@@ -39,7 +39,7 @@ Now, set the fields:
|
|
39
39
|
Where myPayload.body is the object that will be insertet into the email template. Make sure that the class has implemented the to_json method.
|
40
40
|
|
41
41
|
Setup the service:
|
42
|
-
myService =
|
42
|
+
myService = AlphaMail::EmailService.new 'http://api.am1.comfirm.se/v1', 'YOUR-TOKEN-HERE'
|
43
43
|
|
44
44
|
Replace YOUR-TOKEN-HERE with a valid token. You can crate new tokens at the dashboard under settings.
|
45
45
|
|
@@ -48,6 +48,40 @@ Now it's time for magic:
|
|
48
48
|
|
49
49
|
Send the payload by calling queue. The respond object will be stored in am_res as showed above.
|
50
50
|
|
51
|
+
Shorthand example:
|
52
|
+
|
53
|
+
require 'rubygems'
|
54
|
+
require 'alphamail'
|
55
|
+
|
56
|
+
class NewMember
|
57
|
+
attr_accessor :name
|
58
|
+
attr_accessor :password
|
59
|
+
|
60
|
+
def initialize(name = 'Unknown', password = 'unset1')
|
61
|
+
@name = name
|
62
|
+
@password = password
|
63
|
+
end
|
64
|
+
|
65
|
+
def to_json(*a)
|
66
|
+
{
|
67
|
+
'name' => name,
|
68
|
+
'password' => password
|
69
|
+
|
70
|
+
}.to_json(*a)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
myService = AlphaMail::EmailService.new 'http://api.am1.comfirm.se/v1', 'YOUR-TOKEN-HERE'
|
75
|
+
|
76
|
+
am_res = myService.queue(AlphaMail::MessagePayload.new(
|
77
|
+
139, # Project ID
|
78
|
+
2, # Sender ID
|
79
|
+
AlphaMail::Contact.new('Jack Sender', 'jack@example.com'), # Sender
|
80
|
+
AlphaMail::Contact.new('John Doe', 'john.doe@example.com'), # Receiver
|
81
|
+
NewMember.new('John Doe', 'wh4tswhAt') # Body object
|
82
|
+
))
|
83
|
+
puts "#{am_res.error_code} #{am_res.message}"
|
84
|
+
|
51
85
|
== Error codes
|
52
86
|
|
53
87
|
The respond will be returned as an AplhaMailError object.
|
data/lib/alphamail.rb
CHANGED
@@ -4,92 +4,93 @@ require 'uri'
|
|
4
4
|
require 'net/http'
|
5
5
|
require 'json'
|
6
6
|
|
7
|
-
|
8
|
-
class
|
9
|
-
|
10
|
-
|
7
|
+
module AlphaMail
|
8
|
+
# Email contact class
|
9
|
+
class Contact
|
10
|
+
attr_accessor :name
|
11
|
+
attr_accessor :email
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
def initialize(name = "Undefined", email = "")
|
14
|
+
@name = name
|
15
|
+
@email = email
|
16
|
+
end
|
15
17
|
end
|
16
|
-
end
|
17
18
|
|
18
|
-
# Message payload class
|
19
|
-
class
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
def initialize(project_id = 0, receiver_id = 0, sender = AlphaMailEmailContact.new('', ''), receiver = AlphaMailEmailContact.new('', ''), body = '')
|
27
|
-
@project_id = project_id
|
28
|
-
@receiver_id = receiver_id
|
29
|
-
@sender = sender
|
30
|
-
@receiver = receiver
|
31
|
-
@body = body
|
32
|
-
@body_json = ''
|
33
|
-
end
|
19
|
+
# Message payload class
|
20
|
+
class MessagePayload
|
21
|
+
attr_accessor :project_id
|
22
|
+
attr_accessor :receiver_id
|
23
|
+
attr_accessor :sender
|
24
|
+
attr_accessor :receiver
|
25
|
+
attr_accessor :body
|
34
26
|
|
35
|
-
|
36
|
-
|
37
|
-
@
|
27
|
+
def initialize(project_id = 0, receiver_id = 0, sender = Contact.new('', ''), receiver = Contact.new('', ''), body = '')
|
28
|
+
@project_id = project_id
|
29
|
+
@receiver_id = receiver_id
|
30
|
+
@sender = sender
|
31
|
+
@receiver = receiver
|
32
|
+
@body = body
|
33
|
+
@body_json = ''
|
38
34
|
end
|
35
|
+
|
36
|
+
def to_json(*a)
|
37
|
+
if @body.respond_to?('to_json')
|
38
|
+
@body_json = @body.to_json
|
39
|
+
end
|
39
40
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
41
|
+
{
|
42
|
+
'project_id' => project_id,
|
43
|
+
'receiver_id' => receiver_id,
|
44
|
+
'sender_name' => sender.name,
|
45
|
+
'sender_email' => sender.email,
|
46
|
+
'receiver_name' => receiver.name,
|
47
|
+
'receiver_email' => receiver.email,
|
48
|
+
'body' => @body_json
|
48
49
|
|
49
|
-
|
50
|
+
}.to_json(*a)
|
51
|
+
end
|
50
52
|
end
|
51
|
-
end
|
52
53
|
|
53
|
-
class
|
54
|
-
|
55
|
-
|
54
|
+
class Result
|
55
|
+
attr_accessor :error_code
|
56
|
+
attr_accessor :message
|
56
57
|
|
57
|
-
|
58
|
-
|
59
|
-
|
58
|
+
def initialize(error_code = 0, message = '')
|
59
|
+
@error_code = error_code
|
60
|
+
@message = message
|
61
|
+
end
|
60
62
|
end
|
61
|
-
end
|
62
63
|
|
63
|
-
# Alpha-mail email service
|
64
|
-
class
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
64
|
+
# Alpha-mail email service
|
65
|
+
class EmailService
|
66
|
+
def initialize(service_url, api_token)
|
67
|
+
@service_url = service_url + '/email/queue'
|
68
|
+
@api_token = api_token
|
69
|
+
end
|
69
70
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
71
|
+
def queue(payload = '')
|
72
|
+
# Queue the message
|
73
|
+
uri = URI.parse(@service_url)
|
74
|
+
req = Net::HTTP::Post.new(uri.path)
|
75
|
+
req.basic_auth '', @api_token
|
76
|
+
req.body = payload.to_json
|
77
|
+
req.content_type = 'application/json'
|
78
|
+
res = Net::HTTP.start(uri.host, uri.port) {|http|
|
79
|
+
http.request(req)
|
80
|
+
}
|
80
81
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
82
|
+
# Check response
|
83
|
+
case res.code
|
84
|
+
when '200' then
|
85
|
+
am_res = JSON.parse(res.body)
|
86
|
+
return Result.new(am_res['error_code'], am_res['message'])
|
87
|
+
else
|
88
|
+
am_res = JSON.parse(res.body)
|
89
|
+
return Result.new(am_res['error_code'], am_res['message'])
|
90
|
+
end
|
91
|
+
return Result.new(-1, 'Unknown error')
|
92
|
+
end
|
91
93
|
end
|
92
94
|
end
|
93
95
|
|
94
96
|
|
95
|
-
|
data/spec/alphamail_test.rb
CHANGED
@@ -25,7 +25,7 @@ end
|
|
25
25
|
myMember = NewMember.new('Ruby Diamond Ruben', 'my3Passw0rd89')
|
26
26
|
|
27
27
|
# The payload
|
28
|
-
myPayload =
|
28
|
+
myPayload = AlphaMail::MessagePayload.new
|
29
29
|
|
30
30
|
# Set payload fields
|
31
31
|
myPayload.project_id = 139
|
@@ -36,7 +36,7 @@ myPayload.receiver.name = 'John Doe'
|
|
36
36
|
myPayload.receiver.email = 'john.doe@example.com'
|
37
37
|
myPayload.body = myMember
|
38
38
|
|
39
|
-
myService =
|
39
|
+
myService = AlphaMail::EmailService.new 'http://api.am1.comfirm.se/v1', 'YOUR-TOKEN-HERE'
|
40
40
|
am_res = myService.queue(myPayload)
|
41
41
|
puts "#{am_res.error_code} #{am_res.message}"
|
42
42
|
|
@@ -44,11 +44,11 @@ puts "#{am_res.error_code} #{am_res.message}"
|
|
44
44
|
# Demo 2 ---------------------
|
45
45
|
|
46
46
|
# The payload
|
47
|
-
am_res = myService.queue(
|
47
|
+
am_res = myService.queue(AlphaMail::MessagePayload.new(
|
48
48
|
139, # Project ID
|
49
49
|
2, # Sender ID
|
50
|
-
|
51
|
-
|
50
|
+
AlphaMail::Contact.new('Jack Sender', 'jack@example.com'), # Sender
|
51
|
+
AlphaMail::Contact.new('John Doe', 'john.doe@example.com'), # Receiver
|
52
52
|
NewMember.new('John Doe', 'wh4tswhAt') # Body object
|
53
53
|
))
|
54
54
|
puts "#{am_res.error_code} #{am_res.message}"
|
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: 31
|
5
|
+
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
9
|
- 4
|
10
|
-
|
11
|
-
version: 1.0.4.pre2
|
10
|
+
version: 1.0.4
|
12
11
|
platform: ruby
|
13
12
|
authors:
|
14
13
|
- Jack Engqvist Johansson
|