class Fatboy::TimeBasedPopularity

TimeBasedPopularity measures the popularity based on a set period of time. You should probably never initialize this yourself.

Public Class Methods

new(redis, store) click to toggle source

What redis to look in, and what sorted set we’re using. Probably don’t ever initialize this yourself.

# File lib/fatboy/time_based_popularity.rb, line 10
def initialize(redis, store)
  @redis = redis
  @store = store
end

Public Instance Methods

enumerator() click to toggle source

Get an enumerator of all viewed items, as a Fatboy::ViewedItem, in rank order. Pretty useful for lazy operations and such.

# File lib/fatboy/time_based_popularity.rb, line 18
def enumerator
  Enumerator.new(size) do |yielder|
    range(0..(size-1)).each{|x| yielder.yield x}
  end
end
least() click to toggle source

Get the least viewed item. Returns a Fatboy::ViewedItem

# File lib/fatboy/time_based_popularity.rb, line 32
def least
  range(-1..-2).first
end
most() click to toggle source

Get the most viewed item. Returns a Fatboy::ViewedItem

# File lib/fatboy/time_based_popularity.rb, line 26
def most
  range(0..1).first
end
range(rng) click to toggle source

Specify a range of ranks, and gets them. Returns an array of Fatboy::ViewedItem

# File lib/fatboy/time_based_popularity.rb, line 45
def range(rng)
  start = rng.first
  stop = rng.last
  ##
  # Build up a list of pairs: [id, score]
  pairs = @redis.zrevrange(@store, start, stop, withscores: true)
  ##
  # Get rid of nils, zip up list with range of rank
  triplets = pairs.reject{|p| !p}.zip(start..stop)
  # After the zip, we have [[[id, score], rank], [[id, score], rank]]
  # So we flatten out the inner arrays, giving us
  # [[id, score, rank], [id, score, rank]]
  triplets.map!(&:flatten)
  ##
  # Use the array splat to more easily pass in the 3 arguments
  triplets.map{|trip| Fatboy::ViewedItem.new(*trip)}
end
size() click to toggle source

Get the total number of items viewed.

# File lib/fatboy/time_based_popularity.rb, line 38
def size
  @redis.zcard(@store)
end