app/models/voltron/notification/sms_notification.rb in voltron-notify-0.1.5 vs app/models/voltron/notification/sms_notification.rb in voltron-notify-0.1.6
- old
+ new
@@ -1,20 +1,22 @@
require "twilio-ruby"
class Voltron::Notification::SmsNotification < ActiveRecord::Base
+ include Rails.application.routes.url_helpers
+
has_many :attachments
belongs_to :notification
after_initialize :setup
before_create :deliver_now, unless: :use_queue?
after_create :deliver_later, if: :use_queue?
- include Rails.application.routes.url_helpers
+ validates :status, presence: false, inclusion: { in: %w( queued failed sent delivered undelivered ), message: "must be one of: queued, failed, sent, delivered, undelivered" }, on: :update
def setup
@request = []
@response = []
end
@@ -38,28 +40,28 @@
self.update(request_json: @request.to_json, response_json: @response.to_json, sid: @response.first[:sid], status: @response.first[:status])
else
# We are before_create so we can just set the attribute values, it will be saved after this
self.request_json = @request.to_json
self.response_json = @response.to_json
- self.sid = @response.first[:sid]
- self.status = @response.first[:status]
+ self.sid = response.first[:sid]
+ self.status = response.first[:status]
end
end
def deliver_now
all_attachments = attachments.map(&:attachment)
# If sending more than 1 attachment, iterate through all but one attachment and send each without a body...
if all_attachments.count > 1
begin
- client.messages.create({ from: from_formatted, to: to_formatted, media_url: all_attachments.shift }.compact)
+ 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)
@request << Rack::Utils.parse_nested_query(client.last_request.body)
@response << JSON.parse(client.last_response.body)
end until all_attachments.count == 1
end
# ... Then send the last attachment (if any) with the actual text body. This way we're not sending multiple SMS's with same body
- client.messages.create({ from: from_formatted, to: to_formatted, body: message, media_url: all_attachments.shift }.compact)
+ 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)
@request << Rack::Utils.parse_nested_query(client.last_request.body)
@response << JSON.parse(client.last_response.body)
after_deliver
end