voltron-notify 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -1
- data/app/controllers/voltron/notification_controller.rb +21 -0
- data/app/models/voltron/notification.rb +2 -2
- data/app/models/voltron/notification/email_notification.rb +8 -2
- data/app/models/voltron/notification/sms_notification.rb +7 -5
- data/lib/generators/templates/db/migrate/create_voltron_notification_email_notifications.rb +2 -0
- data/lib/generators/templates/db/migrate/create_voltron_notification_sms_notifications.rb +3 -0
- data/lib/voltron/notify.rb +1 -0
- data/lib/voltron/notify/action_dispatch/routes.rb +14 -0
- data/lib/voltron/notify/engine.rb +1 -0
- data/lib/voltron/notify/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ee6f4cf0e055127232f8ea6dc0bc719592e1ee0b
|
4
|
+
data.tar.gz: 8f6163ec33b58958a9e35d718accef4aedfb2a58
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa78b9f193772a252ec4969c6861ef6c6d296b0f722cc289baf02d1009024d5bcd5c55bd6f9c20fe6bf6e8270883575dd3a54471639d1146e4ded3827cfdfd82
|
7
|
+
data.tar.gz: 05f5cc27b664ce2cba50017fe181265a112cb8d8ac094fa3f6b2c033246f0a0abc62a1c353a28311e2654fcd3e2fe1e2a41f251deb911e3370b05ebd03521ad2
|
data/Gemfile
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
class Voltron::NotificationController < ApplicationController
|
2
|
+
|
3
|
+
skip_before_action :verify_authenticity_token
|
4
|
+
|
5
|
+
def update
|
6
|
+
if Voltron::Notification::SmsNotification.exists?(sid: params[:MessageSid])
|
7
|
+
sms = Voltron::Notification::SmsNotification.find_by(sid: params[:MessageSid])
|
8
|
+
update_params = { status: params[:MessageStatus], error_code: params[:ErrorCode] }.compact
|
9
|
+
if sms.update(update_params)
|
10
|
+
head :ok
|
11
|
+
else
|
12
|
+
Voltron.log "(SID: #{params[:MessageSid]}) " + sms.errors.full_messages.join(""), "Notification Update", :light_yellow
|
13
|
+
head :unprocessable_entity
|
14
|
+
end
|
15
|
+
else
|
16
|
+
Voltron.log "SMS Notification with id #{params[:MessageSid]} not found.", "Notification Update", :light_yellow
|
17
|
+
head :not_found
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -11,11 +11,11 @@ module Voltron
|
|
11
11
|
|
12
12
|
before_validation :validate
|
13
13
|
|
14
|
-
PERMITTED_ATTRIBUTES = [:to, :from]
|
14
|
+
PERMITTED_ATTRIBUTES = [:to, :from, :template]
|
15
15
|
|
16
16
|
def email(subject, **args, &block)
|
17
17
|
# Get the remaining args as params, that will eventually become assigns in the mailer template
|
18
|
-
params = { subject: subject, notifyable.class.name.downcase => notifyable }.merge(**args)
|
18
|
+
params = { subject: subject, notifyable.class.name.downcase => notifyable }.compact.merge(**args)
|
19
19
|
|
20
20
|
# Build the options hash from the provided arguments
|
21
21
|
options = { subject: subject }.merge(**args.select { |k,v| PERMITTED_ATTRIBUTES.include?(k.to_sym) })
|
@@ -74,6 +74,12 @@ class Voltron::Notification::EmailNotification < ActiveRecord::Base
|
|
74
74
|
@mailer_arguments = *args
|
75
75
|
end
|
76
76
|
|
77
|
+
def template(fullpath)
|
78
|
+
parts = fullpath.split("/")
|
79
|
+
self.template_name = parts.pop.sub(/\.(html|text)\..*$/, "")
|
80
|
+
self.template_path = parts.join("/")
|
81
|
+
end
|
82
|
+
|
77
83
|
# TODO: Move this to actual validates_* methods
|
78
84
|
def error_messages
|
79
85
|
output = []
|
@@ -91,8 +97,8 @@ class Voltron::Notification::EmailNotification < ActiveRecord::Base
|
|
91
97
|
def mail
|
92
98
|
# If no mailer arguments, use default order of arguments as defined in Voltron::NotificationMailer.notify
|
93
99
|
if @mailer_arguments.blank?
|
94
|
-
@request << { to: to, from: from, subject: subject }.compact.merge(vars: vars, attachments: attachments)
|
95
|
-
mailer.send method, { to: to, from: from, subject: subject }.compact, vars, attachments
|
100
|
+
@request << { to: to, from: from, subject: subject, template_path: template_path, template_name: template_name }.compact.merge(vars: vars, attachments: attachments)
|
101
|
+
mailer.send method, { to: to, from: from, subject: subject, template_path: template_path, template_name: template_name }.compact, vars, attachments
|
96
102
|
else
|
97
103
|
@request << @mailer_arguments.compact
|
98
104
|
mailer.send method, *@mailer_arguments.compact
|
@@ -2,6 +2,8 @@ require "twilio-ruby"
|
|
2
2
|
|
3
3
|
class Voltron::Notification::SmsNotification < ActiveRecord::Base
|
4
4
|
|
5
|
+
include Rails.application.routes.url_helpers
|
6
|
+
|
5
7
|
has_many :attachments
|
6
8
|
|
7
9
|
belongs_to :notification
|
@@ -12,7 +14,7 @@ class Voltron::Notification::SmsNotification < ActiveRecord::Base
|
|
12
14
|
|
13
15
|
after_create :deliver_later, if: :use_queue?
|
14
16
|
|
15
|
-
|
17
|
+
validates :status, presence: false, inclusion: { in: %w( queued failed sent delivered undelivered ), message: "must be one of: queued, failed, sent, delivered, undelivered" }, on: :update
|
16
18
|
|
17
19
|
def setup
|
18
20
|
@request = []
|
@@ -40,8 +42,8 @@ class Voltron::Notification::SmsNotification < ActiveRecord::Base
|
|
40
42
|
# We are before_create so we can just set the attribute values, it will be saved after this
|
41
43
|
self.request_json = @request.to_json
|
42
44
|
self.response_json = @response.to_json
|
43
|
-
self.sid =
|
44
|
-
self.status =
|
45
|
+
self.sid = response.first[:sid]
|
46
|
+
self.status = response.first[:status]
|
45
47
|
end
|
46
48
|
end
|
47
49
|
|
@@ -51,14 +53,14 @@ class Voltron::Notification::SmsNotification < ActiveRecord::Base
|
|
51
53
|
# If sending more than 1 attachment, iterate through all but one attachment and send each without a body...
|
52
54
|
if all_attachments.count > 1
|
53
55
|
begin
|
54
|
-
client.messages.create({ from: from_formatted, to: to_formatted, media_url: all_attachments.shift }.compact)
|
56
|
+
client.messages.create({ from: from_formatted, to: to_formatted, media_url: all_attachments.shift, status_callback: try(:update_voltron_notification_url, host: Voltron.config.base_url) }.compact)
|
55
57
|
@request << Rack::Utils.parse_nested_query(client.last_request.body)
|
56
58
|
@response << JSON.parse(client.last_response.body)
|
57
59
|
end until all_attachments.count == 1
|
58
60
|
end
|
59
61
|
|
60
62
|
# ... Then send the last attachment (if any) with the actual text body. This way we're not sending multiple SMS's with same body
|
61
|
-
client.messages.create({ from: from_formatted, to: to_formatted, body: message, media_url: all_attachments.shift }.compact)
|
63
|
+
client.messages.create({ from: from_formatted, to: to_formatted, body: message, media_url: all_attachments.shift, status_callback: try(:update_voltron_notification_url, host: Voltron.config.base_url) }.compact)
|
62
64
|
@request << Rack::Utils.parse_nested_query(client.last_request.body)
|
63
65
|
@response << JSON.parse(client.last_response.body)
|
64
66
|
after_deliver
|
@@ -9,8 +9,11 @@ class CreateVoltronNotificationSmsNotifications < ActiveRecord::Migration
|
|
9
9
|
t.integer :notification_id
|
10
10
|
t.string :status
|
11
11
|
t.string :sid
|
12
|
+
t.string :error_code
|
12
13
|
|
13
14
|
t.timestamps null: false
|
14
15
|
end
|
16
|
+
|
17
|
+
add_index :voltron_notification_sms_notifications, :sid, unique: true
|
15
18
|
end
|
16
19
|
end
|
data/lib/voltron/notify.rb
CHANGED
@@ -0,0 +1,14 @@
|
|
1
|
+
module Voltron
|
2
|
+
module Notify
|
3
|
+
module Routes
|
4
|
+
|
5
|
+
def allow_notification_update(options={})
|
6
|
+
path = (options[:path] || "/notification/update").gsub(/(^[\s\/]+)|([\s\/]+$)/, '')
|
7
|
+
controller = (options[:controller] || "voltron/notification")
|
8
|
+
action = (options[:action] || "update")
|
9
|
+
post path, to: "#{controller}##{action}", as: :update_voltron_notification
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
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.6
|
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-02-
|
11
|
+
date: 2017-02-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -185,6 +185,7 @@ files:
|
|
185
185
|
- LICENSE.txt
|
186
186
|
- README.md
|
187
187
|
- Rakefile
|
188
|
+
- app/controllers/voltron/notification_controller.rb
|
188
189
|
- app/jobs/voltron/sms_job.rb
|
189
190
|
- app/mailers/voltron/notification_mailer.rb
|
190
191
|
- app/models/voltron/notification.rb
|
@@ -201,6 +202,7 @@ files:
|
|
201
202
|
- lib/generators/voltron/notify/install_generator.rb
|
202
203
|
- lib/voltron/config/notify.rb
|
203
204
|
- lib/voltron/notify.rb
|
205
|
+
- lib/voltron/notify/action_dispatch/routes.rb
|
204
206
|
- lib/voltron/notify/engine.rb
|
205
207
|
- lib/voltron/notify/version.rb
|
206
208
|
- voltron-notify.gemspec
|