app/models/voltron/notification/sms_notification.rb in voltron-notify-0.1.8 vs app/models/voltron/notification/sms_notification.rb in voltron-notify-0.1.9

- old
+ new

@@ -1,6 +1,6 @@ -require "twilio-ruby" +require 'twilio-ruby' class Voltron::Notification::SmsNotification < ActiveRecord::Base include Rails.application.routes.url_helpers @@ -12,29 +12,35 @@ before_create :deliver_now, unless: :use_queue? after_create :deliver_later, if: :use_queue? - 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 + validates :status, presence: false, inclusion: { in: %w( accepted queued sending sent delivered received failed undelivered unknown ), message: I18n.t('voltron.notification.sms.status_invalid') }, on: :update + validates_presence_of :to, message: I18n.t('voltron.notification.sms.to_blank') + + validates_presence_of :from, message: I18n.t('voltron.notification.sms.from_blank') + + validates_presence_of :message, message: I18n.t('voltron.notification.sms.message_blank') + + validate :valid_phone_number + def setup @request = [] @response = [] end def request - # Ensure returned object is an array, whose containing hashes all have symbolized keys, for consistency + # Ensure returned object is an array of request hashes, for consistency out = Array.wrap((JSON.parse(request_json) rescue nil)).compact - out.each { |i| i.try(:deep_symbolize_keys!) } - out + out.map { |h| h.with_indifferent_access } end def response - # Ensure returned object is an array, whose containing hashes all have symbolized keys, for consistency + # Ensure returned object is an array of response hashes, for consistency out = Array.wrap((JSON.parse(response_json) rescue nil)).compact - out.each { |i| i.try(:deep_symbolize_keys!) } - out + out.map { |h| h.with_indifferent_access } end def after_deliver self.request_json = @request.to_json self.response_json = @response.to_json @@ -45,10 +51,13 @@ # since we got to here within after_create, meaning setting the attributes alone won't cut it self.save if use_queue? end def deliver_now + @request = request + @response = response + 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 @@ -66,50 +75,45 @@ end def deliver_later job = Voltron::SmsJob.set(wait: Voltron.config.notify.delay).perform_later self @request << job - @response << { sid: nil, status: 'unknown' } after_deliver end - def attach(url) - if url.starts_with? 'http' - attachments.build attachment: url - else - attachments.build attachment: Voltron.config.base_url + ActionController::Base.helpers.asset_url(url) + def attach(*urls) + urls.flatten.each do |url| + if url.starts_with? 'http' + attachments.build attachment: url + else + attachments.build attachment: Voltron.config.base_url + ActionController::Base.helpers.asset_url(url) + end end end def valid_phone? begin return true if to.blank? # Handle a blank `to` separately in the errors method below to_formatted true rescue => e - Voltron.log e.message, 'Notify', :light_red + Voltron.log e.message, 'Notify', Voltron::Notify::LOG_COLOR false end end - # TODO: Move this to actual validates_* methods - def error_messages - output = [] - output << 'recipient cannot be blank' if to.blank? - output << 'recipient is not a valid phone number' unless valid_phone? - output << 'sender cannot be blank' if from.blank? - output << 'message cannot be blank' if message.blank? - output - end - def callback_url url = try(:update_voltron_notification_url, host: Voltron.config.base_url).to_s # Don't allow local or blank urls return nil if url.include?('localhost') || url.include?('127.0.0.1') || url.blank? url end private + + def valid_phone_number + errors.add :to, I18n.t('voltron.notification.sms.invalid_phone', number: to) unless valid_phone? + end def use_queue? Voltron.config.notify.use_queue end