1
|
1 |
|
module Textris |
2
|
1 |
|
module Delivery |
3
|
1 |
|
class Mail < Textris::Delivery::Base |
4
|
1 |
|
class Mailer < ActionMailer::Base |
5
|
1 |
|
def notify(from, to, subject, body) |
6
|
1 |
|
mail :from => from, :to => to, :subject => subject, :body => body |
7
|
|
|
end |
8
|
|
|
end |
9
|
|
|
|
10
|
1 |
|
def deliver(to) |
11
|
12 |
|
template_vars = { :to_phone => to } |
12
|
|
|
|
13
|
12 |
|
from = apply_template from_template, template_vars |
14
|
12 |
|
to = apply_template to_template, template_vars |
15
|
12 |
|
subject = apply_template subject_template, template_vars |
16
|
12 |
|
body = apply_template body_template, template_vars |
17
|
|
|
|
18
|
|
|
::Textris::Delivery::Mail::Mailer.notify( |
19
|
12 |
|
from, to, subject, body).deliver |
20
|
|
|
end |
21
|
|
|
|
22
|
1 |
|
private |
23
|
|
|
|
24
|
1 |
|
def from_template |
25
|
|
|
Rails.application.config.try(:textris_mail_from_template) || |
26
|
12 |
|
"%{from_name:d}-%{from_phone}@%{env:d}.%{app:d}.com" |
27
|
|
|
end |
28
|
|
|
|
29
|
1 |
|
def to_template |
30
|
|
|
Rails.application.config.try(:textris_mail_to_template) || |
31
|
12 |
|
"%{app:d}-%{env:d}-%{to_phone}[email protected]" |
32
|
|
|
end |
33
|
|
|
|
34
|
1 |
|
def subject_template |
35
|
|
|
Rails.application.config.try(:textris_mail_subject_template) || |
36
|
12 |
|
"%{texter:dh} texter: %{action:h}" |
37
|
|
|
end |
38
|
|
|
|
39
|
1 |
|
def body_template |
40
|
|
|
Rails.application.config.try(:textris_mail_body_template) || |
41
|
12 |
|
"%{content}" |
42
|
|
|
end |
43
|
|
|
|
44
|
1 |
|
def apply_template(template, variables) |
45
|
48 |
|
template.gsub(/\%\{[a-z_:]+\}/) do |match| |
46
|
124 |
|
directive = match.gsub(/[%{}]/, '') |
47
|
124 |
|
key = directive.split(':').first |
48
|
124 |
|
modifiers = directive.split(':')[1] || '' |
49
|
|
|
|
50
|
124 |
|
content = get_template_interpolation(key, variables) |
51
|
124 |
|
content = apply_template_modifiers(content, modifiers.chars) |
52
|
124 |
|
content = 'unknown' unless content.present? |
53
|
|
|
|
54
|
124 |
|
content |
55
|
|
|
end |
56
|
|
|
end |
57
|
|
|
|
58
|
1 |
|
def get_template_interpolation(key, variables) |
59
|
|
|
case key |
60
|
|
|
when 'app', 'env' |
61
|
38 |
|
get_rails_variable(key) |
62
|
|
|
when 'texter', 'action', 'from_name', 'from_phone', 'content' |
63
|
76 |
|
message.send(key) |
64
|
|
|
when 'media_urls' |
65
|
2 |
|
message.media_urls.join(', ') |
66
|
|
|
else |
67
|
8 |
|
variables[key.to_sym] |
68
|
124 |
|
end.to_s.strip |
69
|
|
|
end |
70
|
|
|
|
71
|
1 |
|
def get_rails_variable(var) |
72
|
38 |
|
case var |
73
|
|
|
when 'app' |
74
|
20 |
|
Rails.application.class.parent_name |
75
|
|
|
when 'env' |
76
|
18 |
|
Rails.env |
77
|
|
|
end |
78
|
|
|
end |
79
|
|
|
|
80
|
1 |
|
def apply_template_modifiers(content, modifiers) |
81
|
124 |
|
modifiers.each do |modifier| |
82
|
80 |
|
case modifier |
83
|
|
|
when 'd' |
84
|
52 |
|
content = content.underscore.dasherize |
85
|
|
|
when 'h' |
86
|
24 |
|
content = content.humanize.gsub(/[-_]/, ' ') |
87
|
|
|
when 'p' |
88
|
2 |
|
content = Phony.format(content) rescue content |
89
|
|
|
end |
90
|
|
|
end |
91
|
|
|
|
92
|
124 |
|
content |
93
|
|
|
end |
94
|
|
|
end |
95
|
|
|
end |
96
|
|
|
end |
97
|
|
|
|