Class: ActionPushWeb::Notification

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Defined in:
lib/action_push_web/notification.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title:, path: nil, body: nil, icon_path: nil, urgency: nil, badge: nil, silent: nil, **context) ⇒ Notification

Returns a new instance of Notification.



20
21
22
23
24
25
26
27
28
29
# File 'lib/action_push_web/notification.rb', line 20

def initialize(title:, path: nil, body: nil, icon_path: nil, urgency: nil, badge: nil, silent: nil, **context)
  @title = title
  @path = path
  @body = body.to_s
  @icon_path = icon_path
  @urgency = urgency
  @silent = silent
  @badge = badge
  @context = context
end

Instance Attribute Details

#badgeObject

Returns the value of attribute badge.



5
6
7
# File 'lib/action_push_web/notification.rb', line 5

def badge
  @badge
end

#bodyObject

Returns the value of attribute body.



5
6
7
# File 'lib/action_push_web/notification.rb', line 5

def body
  @body
end

#contextObject

Returns the value of attribute context.



5
6
7
# File 'lib/action_push_web/notification.rb', line 5

def context
  @context
end

#icon_pathObject



44
45
46
# File 'lib/action_push_web/notification.rb', line 44

def icon_path
  @icon_path.presence || config.fetch(:icon_path, nil)
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/action_push_web/notification.rb', line 5

def path
  @path
end

#silentObject

Returns the value of attribute silent.



5
6
7
# File 'lib/action_push_web/notification.rb', line 5

def silent
  @silent
end

#titleObject

Returns the value of attribute title.



5
6
7
# File 'lib/action_push_web/notification.rb', line 5

def title
  @title
end

#urgencyObject

Returns the value of attribute urgency.



5
6
7
# File 'lib/action_push_web/notification.rb', line 5

def urgency
  @urgency
end

Class Method Details

.queue_as(name) ⇒ Object



15
16
17
# File 'lib/action_push_web/notification.rb', line 15

def queue_as(name)
  self.queue_name = name
end

Instance Method Details

#as_jsonObject



52
53
54
55
# File 'lib/action_push_web/notification.rb', line 52

def as_json
  { title:, body:, path:, icon_path:, urgency:,
    silent:, badge:, **context }.compact
end

#deliver_later_to(subscriptions) ⇒ Object



37
38
39
40
41
42
# File 'lib/action_push_web/notification.rb', line 37

def deliver_later_to(subscriptions)
  Array(subscriptions).each do |subscription|
    ApplicationPushWebNotificationJob.set(queue: queue_name).
      perform_later(self.class.name, self.as_json, subscription)
  end
end

#deliver_to(subscription) ⇒ Object



31
32
33
34
35
# File 'lib/action_push_web/notification.rb', line 31

def deliver_to(subscription)
  if enabled
    run_callbacks(:delivery) { subscription.push(self) }
  end
end