class Garufa::API::Stats

Attributes

subscriptions[R]

Public Class Methods

new(subscriptions) click to toggle source
# File lib/garufa/api/stats.rb, line 7
def initialize(subscriptions)
  @subscriptions = subscriptions
end

Public Instance Methods

all_channels() click to toggle source
# File lib/garufa/api/stats.rb, line 15
def all_channels
  subscriptions.each_with_object({}) do |(channel, _), stats|
    stats[channel] = single_channel(channel)
  end
end
channel_size(channel) click to toggle source
# File lib/garufa/api/stats.rb, line 21
def channel_size(channel)
  (subscriptions[channel] || []).size
end
single_channel(channel) click to toggle source
# File lib/garufa/api/stats.rb, line 11
def single_channel(channel)
  { size: channel_size(channel), presence: presence(channel) }
end

Private Instance Methods

presence(channel) click to toggle source
# File lib/garufa/api/stats.rb, line 27
def presence(channel)
  return unless channel.start_with?('presence-')

  data = { ids: [], hash: {} }

  (subscriptions[channel] || []).each do |sub|

    channel_data = JSON.parse(sub.channel_data)
    id, info = channel_data.values_at('user_id', 'user_info')

    next if data[:ids].include?(id)

    data[:ids] << id
    data[:hash][id] = info
  end

  data.merge(count: data[:ids].count)
end