Class: Notifications

Inherits:
Object
  • Object
show all
Defined in:
lib/models/notifications.rb

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Notifications

Returns a new instance of Notifications.



15
16
17
18
19
20
# File 'lib/models/notifications.rb', line 15

def initialize(**options)
  @dt = options[:dt]
  @start_at = options[:start_at]
  @finish_at = options[:finish_at]
  @notifications = Rpush::Notification.all
end

Instance Method Details

#dataObject



22
23
24
25
26
27
28
29
30
# File 'lib/models/notifications.rb', line 22

def data
  fetch = -> a { a['delivered'] ? [true, a['delivered_at']] : [false, a['failed_at']] }
  @notifications.map(&:attributes)
    .map(&fetch)
    .select { |_,d| d.to_i >= @start_at.to_i && d.to_i <= @finish_at.to_i }
    .sort_by(&:last)
    .group_by { |_,d| (d.to_i - @start_at.to_i) / @dt.to_i }
    .map_value { |g| {true => 0, false => 0}.merge g.group_by(&:first).map_value(&:size) }
end