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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 98bac0d7281f37ea5c0dfbf728f6701699c4b85b
4
- data.tar.gz: 913ef565e6f2b5678d5f3c0bd37a78658e3b6664
3
+ metadata.gz: ee6f4cf0e055127232f8ea6dc0bc719592e1ee0b
4
+ data.tar.gz: 8f6163ec33b58958a9e35d718accef4aedfb2a58
5
5
  SHA512:
6
- metadata.gz: 0c33fbbaed37d2bb9e4539546e9f7046b3e538c59c62211fdab0ca8ac1fb10ec0b56b5b7e8d60f62391b24741437b6e15269a122ea84a998782580d3381767a8
7
- data.tar.gz: 5c181238a194d6b8c29d848e972837e8781041704ae2e50a5f51761d7f5f5b68377ba1568aa310e2b381c631acc97d92c33a8ad910c460b18b4fdb3f9e9165b1
6
+ metadata.gz: fa78b9f193772a252ec4969c6861ef6c6d296b0f722cc289baf02d1009024d5bcd5c55bd6f9c20fe6bf6e8270883575dd3a54471639d1146e4ded3827cfdfd82
7
+ data.tar.gz: 05f5cc27b664ce2cba50017fe181265a112cb8d8ac094fa3f6b2c033246f0a0abc62a1c353a28311e2654fcd3e2fe1e2a41f251deb911e3370b05ebd03521ad2
data/Gemfile CHANGED
@@ -2,4 +2,5 @@ source 'https://rubygems.org'
2
2
  source 'http://gem.minow.io'
3
3
 
4
4
  # Specify your gem's dependencies in voltron-notify.gemspec
5
- gemspec
5
+ gem 'colorize'
6
+ gemspec
@@ -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
- include Rails.application.routes.url_helpers
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 = @response.first[:sid]
44
- self.status = @response.first[: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
@@ -4,6 +4,8 @@ class CreateVoltronNotificationEmailNotifications < ActiveRecord::Migration
4
4
  t.string :to
5
5
  t.string :from
6
6
  t.string :subject
7
+ t.string :template_path
8
+ t.string :template_name
7
9
  t.string :mailer_class
8
10
  t.string :mailer_method
9
11
  t.text :request_json
@@ -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
@@ -1,5 +1,6 @@
1
1
  require "voltron"
2
2
  require "voltron/notify/version"
3
+ require "voltron/notify/action_dispatch/routes"
3
4
  require "voltron/config/notify"
4
5
 
5
6
  module Voltron
@@ -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
@@ -6,6 +6,7 @@ module Voltron
6
6
 
7
7
  initializer "voltron.notify.initialize" do
8
8
  ::ActiveRecord::Base.send :extend, ::Voltron::Notify
9
+ ::ActionDispatch::Routing::Mapper.send :include, ::Voltron::Notify::Routes
9
10
  end
10
11
  end
11
12
  end
@@ -1,5 +1,5 @@
1
1
  module Voltron
2
2
  module Notify
3
- VERSION = "0.1.5".freeze
3
+ VERSION = "0.1.6".freeze
4
4
  end
5
5
  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.5
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-07 00:00:00.000000000 Z
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