class Notifiable::Notification

Constants

ERROR_STATUS
FINISHED_STATUS
SENDING_STATUS
WAITING_STATUS

Public Instance Methods

add_device_token(d) click to toggle source
# File lib/notifiable/notification.rb, line 32
def add_device_token(d)
  provider = d.provider.to_sym

  unless notifiers[provider]
    clazz = Notifiable.notifier_class(self, d)
    raise "Notifier #{provider} not configured" unless clazz
    notifier = clazz.new(self)
    app.configure(provider, notifier)
    @notifiers[provider] = notifier
  end

  notifiers[provider].send_notification(d)
end
batch() { |self| ... } click to toggle source
# File lib/notifiable/notification.rb, line 21
def batch
  update(status: SENDING_STATUS)
  yield(self)
  update_attributes({status: FINISHED_STATUS, last_error_message: nil})
rescue => e
  update_attributes({status: ERROR_STATUS, last_error_message: e.message})
  raise e
ensure
  close
end
delivered!(device_token) click to toggle source
# File lib/notifiable/notification.rb, line 50
def delivered!(device_token)
  if notification_statuses.exists?(device_token: device_token)
    s = notification_statuses.find_by(device_token: device_token)
    return if s.delivered?
    s.delivered!
  end
  increment!(:delivered_count)
end
opened!(device_token) click to toggle source
# File lib/notifiable/notification.rb, line 59
def opened!(device_token)
  if notification_statuses.exists?(device_token: device_token)
    s = notification_statuses.find_by(device_token: device_token)
    return if s.opened?
    s.opened!
  end
  increment!(:opened_count)
end
send_params() click to toggle source
# File lib/notifiable/notification.rb, line 46
def send_params
  @send_params ||= (parameters || {}).merge(n_id: id)
end

Private Instance Methods

close() click to toggle source
# File lib/notifiable/notification.rb, line 78
def close
  notifiers.each_value(&:close)
  @notifiers = nil
end
notifiers() click to toggle source
# File lib/notifiable/notification.rb, line 74
def notifiers
  @notifiers ||= {}
end
set_default_status() click to toggle source
# File lib/notifiable/notification.rb, line 70
def set_default_status
  self.status ||= WAITING_STATUS
end