|
1
|
1 |
|
module Textris |
|
2
|
1 |
|
class Message |
|
3
|
1 |
|
attr_reader :content, :from_name, :from_phone, :to, :texter, :action, :args, |
|
4
|
|
|
:media_urls |
|
5
|
|
|
|
|
6
|
1 |
|
def initialize(options = {}) |
|
7
|
59 |
|
initialize_content(options) |
|
8
|
58 |
|
initialize_author(options) |
|
9
|
58 |
|
initialize_recipients(options) |
|
10
|
|
|
|
|
11
|
57 |
|
@texter = options[:texter] |
|
12
|
57 |
|
@action = options[:action] |
|
13
|
57 |
|
@args = options[:args] |
|
14
|
57 |
|
@media_urls = options[:media_urls] |
|
15
|
|
|
end |
|
16
|
|
|
|
|
17
|
1 |
|
def deliver |
|
18
|
4 |
|
deliveries = ::Textris::Delivery.get |
|
19
|
4 |
|
deliveries.each do |delivery| |
|
20
|
4 |
|
delivery.new(self).deliver_to_all |
|
21
|
|
|
end |
|
22
|
|
|
|
|
23
|
4 |
|
self |
|
24
|
|
|
end |
|
25
|
|
|
|
|
26
|
1 |
|
def texter(options = {}) |
|
27
|
40 |
|
if options[:raw] |
|
28
|
3 |
|
@texter |
|
29
|
37 |
|
elsif @texter.present? |
|
30
|
15 |
|
@texter.to_s.split('::').last.to_s.sub(/Texter$/, '') |
|
31
|
|
|
end |
|
32
|
|
|
end |
|
33
|
|
|
|
|
34
|
1 |
|
def from |
|
35
|
14 |
|
if @from_phone.present? |
|
36
|
8 |
|
if @from_name.present? |
|
37
|
6 |
|
"#{@from_name} <#{Phony.format(@from_phone)}>" |
|
38
|
|
|
else |
|
39
|
2 |
|
Phony.format(@from_phone) |
|
40
|
|
|
end |
|
41
|
6 |
|
elsif @from_name.present? |
|
42
|
2 |
|
@from_name |
|
43
|
|
|
end |
|
44
|
|
|
end |
|
45
|
|
|
|
|
46
|
1 |
|
def content |
|
47
|
45 |
|
@content ||= parse_content(@renderer.render_content) |
|
48
|
|
|
end |
|
49
|
|
|
|
|
50
|
1 |
|
private |
|
51
|
|
|
|
|
52
|
1 |
|
def initialize_content(options) |
|
53
|
59 |
|
if options[:content].present? |
|
54
|
51 |
|
@content = parse_content options[:content] |
|
55
|
8 |
|
elsif options[:renderer].present? |
|
56
|
7 |
|
@renderer = options[:renderer] |
|
57
|
|
|
else |
|
58
|
1 |
|
raise(ArgumentError, "Content must be provided") |
|
59
|
|
|
end |
|
60
|
|
|
end |
|
61
|
|
|
|
|
62
|
1 |
|
def initialize_author(options) |
|
63
|
58 |
|
if options.has_key?(:from) |
|
64
|
30 |
|
@from_name, @from_phone = parse_from options[:from] |
|
65
|
|
|
else |
|
66
|
28 |
|
@from_name = options[:from_name] |
|
67
|
28 |
|
@from_phone = options[:from_phone] |
|
68
|
|
|
end |
|
69
|
|
|
end |
|
70
|
|
|
|
|
71
|
1 |
|
def initialize_recipients(options) |
|
72
|
58 |
|
@to = parse_to options[:to] |
|
73
|
|
|
|
|
74
|
58 |
|
unless @to.present? |
|
75
|
1 |
|
raise(ArgumentError, "Recipients must be provided and E.164 compilant") |
|
76
|
|
|
end |
|
77
|
|
|
end |
|
78
|
|
|
|
|
79
|
1 |
|
def parse_from(from) |
|
80
|
30 |
|
parse_from_dual(from) || parse_from_singular(from) |
|
81
|
|
|
end |
|
82
|
|
|
|
|
83
|
1 |
|
def parse_from_dual(from) |
|
84
|
30 |
|
if (matches = from.to_s.match(/(.*)\<(.*)\>\s*$/).to_a).size == 3 && |
|
85
|
30 |
|
Phony.plausible?(matches[2]) |
|
86
|
12 |
|
[matches[1].strip, Phony.normalize(matches[2])] |
|
87
|
|
|
end |
|
88
|
|
|
end |
|
89
|
|
|
|
|
90
|
1 |
|
def parse_from_singular(from) |
|
91
|
18 |
|
if Phony.plausible?(from) |
|
92
|
2 |
|
[nil, Phony.normalize(from)] |
|
93
|
16 |
|
elsif from.present? |
|
94
|
16 |
|
[from.strip, nil] |
|
95
|
|
|
end |
|
96
|
|
|
end |
|
97
|
|
|
|
|
98
|
1 |
|
def parse_to(to) |
|
99
|
58 |
|
to = [*to] |
|
100
|
141 |
|
to = to.select { |phone| Phony.plausible?(phone.to_s) } |
|
101
|
140 |
|
to = to.map { |phone| Phony.normalize(phone.to_s) } |
|
102
|
|
|
|
|
103
|
58 |
|
to |
|
104
|
|
|
end |
|
105
|
|
|
|
|
106
|
1 |
|
def parse_content(content) |
|
107
|
51 |
|
content = content.to_s |
|
108
|
51 |
|
content = content.rstrip |
|
109
|
|
|
|
|
110
|
51 |
|
content |
|
111
|
|
|
end |
|
112
|
|
|
end |
|
113
|
|
|
end |
|
114
|
|
|
|