class Notifiable::NotifierBase

Attributes

notifier_attributes[R]
env[R]
notification[R]

Public Class Methods

new(notification) click to toggle source
# File lib/notifiable/notifier_base.rb, line 18
def initialize(notification)
  @notification = notification
  @mutex = Mutex.new
end
notifier_attribute(*vars) click to toggle source
# File lib/notifiable/notifier_base.rb, line 8
def self.notifier_attribute(*vars)
  @notifier_attributes ||= []
  @notifier_attributes.concat vars
  attr_writer(*vars)
end

Public Instance Methods

close() click to toggle source
# File lib/notifiable/notifier_base.rb, line 27
def close
  flush
  save_receipts if Notifiable.save_receipts
  @notification.save
end
send_notification(device_token) click to toggle source
# File lib/notifiable/notifier_base.rb, line 23
def send_notification(device_token)
  enqueue(device_token, notification)
 end

Protected Instance Methods

flush() click to toggle source
# File lib/notifiable/notifier_base.rb, line 43
def flush; end
logger() click to toggle source
# File lib/notifiable/notifier_base.rb, line 35
def logger
  if defined?(Rails)
    Rails.logger
  else
    @logger ||= Logger.new(STDOUT)
  end
end
processed(device_token, status = NotificationStatus::SENT_STATUS, error_code = nil, error_message = nil) click to toggle source
# File lib/notifiable/notifier_base.rb, line 45
def processed(device_token, status = NotificationStatus::SENT_STATUS, error_code = nil, error_message = nil)
  @mutex.synchronize {
    if @notification.app.save_notification_statuses
      receipts << { notification_id: notification.id, device_token_id: device_token.id, status: status, created_at: DateTime.now, error_code: error_code, error_message: error_message }
      save_receipts if receipts.count >= Notifiable.notification_status_batch_size
    end

    @notification.increment(:sent_count)
    @notification.increment(:gateway_accepted_count) if status == NotificationStatus::SENT_STATUS
    @notification.save if @notification.sent_count % Notifiable.notification_status_batch_size == 0
  }
end

Private Instance Methods

receipts() click to toggle source
# File lib/notifiable/notifier_base.rb, line 60
def receipts
  @receipts ||= []
end
save_receipts() click to toggle source
# File lib/notifiable/notifier_base.rb, line 64
def save_receipts
  Notifiable::NotificationStatus.import receipts, validate: false
  @receipts = []
end