Class: Notifiable::Notification

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/notifiable/notification.rb

Constant Summary collapse

ERROR_STATUS =
-1
WAITING_STATUS =
0
SENDING_STATUS =
1
FINISHED_STATUS =
2

Instance Method Summary collapse

Instance Method Details

#add_device_token(d) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# 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

#batchObject



21
22
23
24
25
26
27
28
29
30
# 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) ⇒ Object



50
51
52
53
54
55
56
57
# 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) ⇒ Object



59
60
61
62
63
64
65
66
# 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_paramsObject



46
47
48
# File 'lib/notifiable/notification.rb', line 46

def send_params
  @send_params ||= (parameters || {}).merge(n_id: id)
end