class Fatboy

Fatboy is the main class for interacting with the system. It provides a variety of functionality.

Constants

DAY_FORMAT_STR

Format string we use to store the views per day

HOUR_FORMAT_STR

Format string we use to store the views per hour

MONTH_FORMAT_STR

Format string we use to store the views per month

VERSION

Gem version of Fatboy

YEAR_FORMAT_STR

Format string we use to store the views per year

Public Class Methods

new(redis: Redis.new) click to toggle source

Create a new Fatboy. Options:

* +redis:+ : The redis to store views in. By default, Redis.new
# File lib/fatboy.rb, line 14
def initialize(redis: Redis.new)
  @redis = redis
end

Public Instance Methods

[](obj) click to toggle source

let users view with a shorthand

# File lib/fatboy.rb, line 45
def [](obj)
  view(obj)
end
many() click to toggle source

Many provides a way to view many different models at once, for speed reasons.

Great if you need to view a lot of models.

# File lib/fatboy.rb, line 22
def many
  Fatboy::Many.new(@redis)
end
view(obj) click to toggle source

Say that you have viewed an object, making the proper records for hour, day, month, and year.

  • model - a model of some sort. Should quack like an ActiveRecord model (that is, responding to .id)

# File lib/fatboy.rb, line 32
def view(obj)
  throw ArgumentError.new("That doesn't quack like a model!") unless obj.respond_to?(:id)
  stores = Fatboy::Helpers.all_format(Time.now).map do |time|
    Fatboy::Helpers.format_store(obj.class.to_s, time)
  end
  @redis.pipelined do
    stores.each do |store|
      inc_member(store, obj.id)
    end
  end
end
views_for(model) click to toggle source
# File lib/fatboy.rb, line 25
def views_for(model)
  Fatboy::ViewTracker.new(@redis, model)
end

Private Instance Methods

inc_member(store, id) click to toggle source
# File lib/fatboy.rb, line 71
def inc_member(store, id)
  @redis.zincrby(store, 1, id)
end