voltron-notify 0.1.7 → 0.1.8
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.
- checksums.yaml +4 -4
- data/app/models/voltron/notification/sms_notification.rb +34 -28
- data/lib/voltron/notify/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a04574134da60686957218850a216519895e16b7
|
4
|
+
data.tar.gz: 59e67db3dcfc17a5e29f43ff6aa6c289425e9fbe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7989905b87687408ccb33d4377a9593efc607d855443518e853a3502d8c70bb5c11315156cffd2197218dd010385372ab35b6becce91bf753ef6c1d125d173e5
|
7
|
+
data.tar.gz: 7818586e3df9333880066dc55d3667dc3429610b3ea54d9e3118423e5bf5259223904609c2d0ecc80ccac6e09719d613272b6b8db8cf4c012f225ba59118a46e
|
@@ -14,7 +14,7 @@ class Voltron::Notification::SmsNotification < ActiveRecord::Base
|
|
14
14
|
|
15
15
|
after_create :deliver_later, if: :use_queue?
|
16
16
|
|
17
|
-
validates :status, presence: false, inclusion: { in: %w( queued
|
17
|
+
validates :status, presence: false, inclusion: { in: %w( accepted queued sending sent delivered received failed undelivered unknown ), message: 'must be one of: accepted, queued, sending, sent, delivered, received, failed, undelivered, or unknown' }, on: :update
|
18
18
|
|
19
19
|
def setup
|
20
20
|
@request = []
|
@@ -22,29 +22,28 @@ class Voltron::Notification::SmsNotification < ActiveRecord::Base
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def request
|
25
|
-
#
|
26
|
-
|
27
|
-
|
25
|
+
# Ensure returned object is an array, whose containing hashes all have symbolized keys, for consistency
|
26
|
+
out = Array.wrap((JSON.parse(request_json) rescue nil)).compact
|
27
|
+
out.each { |i| i.try(:deep_symbolize_keys!) }
|
28
|
+
out
|
28
29
|
end
|
29
30
|
|
30
31
|
def response
|
31
|
-
#
|
32
|
-
|
33
|
-
|
32
|
+
# Ensure returned object is an array, whose containing hashes all have symbolized keys, for consistency
|
33
|
+
out = Array.wrap((JSON.parse(response_json) rescue nil)).compact
|
34
|
+
out.each { |i| i.try(:deep_symbolize_keys!) }
|
35
|
+
out
|
34
36
|
end
|
35
37
|
|
36
38
|
def after_deliver
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
self.sid = response.first[:sid]
|
46
|
-
self.status = response.first[:status]
|
47
|
-
end
|
39
|
+
self.request_json = @request.to_json
|
40
|
+
self.response_json = @response.to_json
|
41
|
+
self.sid = response.first.try(:[], :sid)
|
42
|
+
self.status = response.first.try(:[], :status) || 'unknown'
|
43
|
+
|
44
|
+
# if use_queue?, meaning if this was sent via ActiveJob, we need to save ourself
|
45
|
+
# since we got to here within after_create, meaning setting the attributes alone won't cut it
|
46
|
+
self.save if use_queue?
|
48
47
|
end
|
49
48
|
|
50
49
|
def deliver_now
|
@@ -53,14 +52,14 @@ class Voltron::Notification::SmsNotification < ActiveRecord::Base
|
|
53
52
|
# If sending more than 1 attachment, iterate through all but one attachment and send each without a body...
|
54
53
|
if all_attachments.count > 1
|
55
54
|
begin
|
56
|
-
client.messages.create({ from: from_formatted, to: to_formatted, media_url: all_attachments.shift, status_callback:
|
55
|
+
client.messages.create({ from: from_formatted, to: to_formatted, media_url: all_attachments.shift, status_callback: callback_url }.compact)
|
57
56
|
@request << Rack::Utils.parse_nested_query(client.last_request.body)
|
58
57
|
@response << JSON.parse(client.last_response.body)
|
59
58
|
end until all_attachments.count == 1
|
60
59
|
end
|
61
60
|
|
62
61
|
# ... Then send the last attachment (if any) with the actual text body. This way we're not sending multiple SMS's with same body
|
63
|
-
client.messages.create({ from: from_formatted, to: to_formatted, body: message, media_url: all_attachments.shift, status_callback:
|
62
|
+
client.messages.create({ from: from_formatted, to: to_formatted, body: message, media_url: all_attachments.shift, status_callback: callback_url }.compact)
|
64
63
|
@request << Rack::Utils.parse_nested_query(client.last_request.body)
|
65
64
|
@response << JSON.parse(client.last_response.body)
|
66
65
|
after_deliver
|
@@ -68,13 +67,13 @@ class Voltron::Notification::SmsNotification < ActiveRecord::Base
|
|
68
67
|
|
69
68
|
def deliver_later
|
70
69
|
job = Voltron::SmsJob.set(wait: Voltron.config.notify.delay).perform_later self
|
71
|
-
@request << job
|
72
|
-
@response << { sid: nil, status:
|
70
|
+
@request << job
|
71
|
+
@response << { sid: nil, status: 'unknown' }
|
73
72
|
after_deliver
|
74
73
|
end
|
75
74
|
|
76
75
|
def attach(url)
|
77
|
-
if url.starts_with?
|
76
|
+
if url.starts_with? 'http'
|
78
77
|
attachments.build attachment: url
|
79
78
|
else
|
80
79
|
attachments.build attachment: Voltron.config.base_url + ActionController::Base.helpers.asset_url(url)
|
@@ -87,7 +86,7 @@ class Voltron::Notification::SmsNotification < ActiveRecord::Base
|
|
87
86
|
to_formatted
|
88
87
|
true
|
89
88
|
rescue => e
|
90
|
-
Voltron.log e.message,
|
89
|
+
Voltron.log e.message, 'Notify', :light_red
|
91
90
|
false
|
92
91
|
end
|
93
92
|
end
|
@@ -95,13 +94,20 @@ class Voltron::Notification::SmsNotification < ActiveRecord::Base
|
|
95
94
|
# TODO: Move this to actual validates_* methods
|
96
95
|
def error_messages
|
97
96
|
output = []
|
98
|
-
output <<
|
99
|
-
output <<
|
100
|
-
output <<
|
101
|
-
output <<
|
97
|
+
output << 'recipient cannot be blank' if to.blank?
|
98
|
+
output << 'recipient is not a valid phone number' unless valid_phone?
|
99
|
+
output << 'sender cannot be blank' if from.blank?
|
100
|
+
output << 'message cannot be blank' if message.blank?
|
102
101
|
output
|
103
102
|
end
|
104
103
|
|
104
|
+
def callback_url
|
105
|
+
url = try(:update_voltron_notification_url, host: Voltron.config.base_url).to_s
|
106
|
+
# Don't allow local or blank urls
|
107
|
+
return nil if url.include?('localhost') || url.include?('127.0.0.1') || url.blank?
|
108
|
+
url
|
109
|
+
end
|
110
|
+
|
105
111
|
private
|
106
112
|
|
107
113
|
def use_queue?
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: voltron-notify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eric Hainer
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-03-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -220,7 +220,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
220
220
|
version: '0'
|
221
221
|
requirements: []
|
222
222
|
rubyforge_project:
|
223
|
-
rubygems_version: 2.
|
223
|
+
rubygems_version: 2.6.6
|
224
224
|
signing_key:
|
225
225
|
specification_version: 4
|
226
226
|
summary: Send notifications easier
|