Skip to content

Commit bea184b

Browse files
committed
Adds Userlist delivery method
1 parent dcb27e6 commit bea184b

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ gem 'sidekiq'
1212
gem 'activejob'
1313
gem 'uri'
1414
gem 'irb'
15+
gem 'mail'

lib/userlist.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require 'userlist/retryable'
77
require 'userlist/push'
88
require 'userlist/token'
9+
require 'userlist/delivery_method'
910

1011
module Userlist
1112
class Error < StandardError; end

lib/userlist/delivery_method.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class Userlist::DeliveryMethod
2+
attr_reader :userlist
3+
attr_reader :settings
4+
5+
def initialize(settings = {})
6+
@settings = settings
7+
8+
@userlist = Userlist::Push.new(settings)
9+
end
10+
11+
def deliver!(mail)
12+
message = serialize(mail)
13+
14+
userlist.messages.push(message.merge(theme: nil))
15+
end
16+
17+
private
18+
19+
def serialize(mail)
20+
{
21+
to: serialize_address(mail.to),
22+
from: serialize_address(mail.from),
23+
subject: mail.subject,
24+
body: serialize_body(mail.body)
25+
}.compact
26+
end
27+
28+
def serialize_address(address)
29+
address.first.to_s
30+
end
31+
32+
def serialize_body(body)
33+
return if body.nil?
34+
35+
if body.multipart?
36+
parts = body.parts.filter_map { |part| serialize_part(part) }
37+
38+
return parts.first if parts.size == 1
39+
40+
{ type: :multipart, content: parts }
41+
else
42+
{ type: :text, content: body.decoded }
43+
end
44+
end
45+
46+
def serialize_part(part)
47+
if part.content_type.start_with?('text/html')
48+
{ type: :html, content: part.decoded }
49+
elsif part.content_type.start_with?('text/plain')
50+
{ type: :text, content: part.decoded }
51+
end
52+
end
53+
end
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
require 'spec_helper'
2+
require 'mail'
3+
4+
RSpec.describe Userlist::DeliveryMethod do
5+
let(:push_client) { instance_double(Userlist::Push) }
6+
let(:messages) { instance_double('Messages') }
7+
let(:config) { { push_key: 'test-key' } }
8+
9+
before do
10+
allow(Userlist::Push).to receive(:new).with(config).and_return(push_client)
11+
allow(push_client).to receive(:messages).and_return(messages)
12+
allow(messages).to receive(:push)
13+
end
14+
15+
describe '#deliver!' do
16+
let(:delivery_method) { described_class.new(config) }
17+
18+
context 'with plain text email' do
19+
let(:mail) do
20+
Mail.new(
21+
to: 'Example User <user@example.com>',
22+
from: 'Example Sender <sender@example.com>',
23+
subject: 'Test Subject',
24+
body: 'Hello world'
25+
)
26+
end
27+
28+
it 'delivers the message correctly' do
29+
expected_payload = {
30+
to: 'user@example.com',
31+
from: 'sender@example.com',
32+
subject: 'Test Subject',
33+
body: { type: :text, content: 'Hello world' },
34+
theme: nil
35+
}
36+
37+
expect(messages).to receive(:push).with(expected_payload)
38+
delivery_method.deliver!(mail)
39+
end
40+
end
41+
42+
context 'with HTML email' do
43+
let(:mail) do
44+
Mail.new do
45+
to 'user@example.com'
46+
from 'sender@example.com'
47+
subject 'Test Subject'
48+
49+
html_part do
50+
content_type 'text/html'
51+
body '<p>Hello world</p>'
52+
end
53+
end
54+
end
55+
56+
it 'delivers the message correctly' do
57+
expected_payload = {
58+
to: 'user@example.com',
59+
from: 'sender@example.com',
60+
subject: 'Test Subject',
61+
body: { type: :html, content: '<p>Hello world</p>' },
62+
theme: nil
63+
}
64+
65+
expect(messages).to receive(:push).with(expected_payload)
66+
delivery_method.deliver!(mail)
67+
end
68+
end
69+
70+
context 'with multipart email' do
71+
let(:mail) do
72+
Mail.new do
73+
to 'user@example.com'
74+
from 'sender@example.com'
75+
subject 'Test Subject'
76+
77+
text_part do
78+
body 'Hello world'
79+
end
80+
81+
html_part do
82+
content_type 'text/html'
83+
body '<p>Hello world</p>'
84+
end
85+
end
86+
end
87+
88+
it 'delivers the message correctly' do
89+
expected_payload = {
90+
to: 'user@example.com',
91+
from: 'sender@example.com',
92+
subject: 'Test Subject',
93+
body: {
94+
type: :multipart,
95+
content: [
96+
{ type: :text, content: 'Hello world' },
97+
{ type: :html, content: '<p>Hello world</p>' }
98+
]
99+
},
100+
theme: nil
101+
}
102+
103+
expect(messages).to receive(:push).with(expected_payload)
104+
delivery_method.deliver!(mail)
105+
end
106+
end
107+
end
108+
end

0 commit comments

Comments
 (0)