Class: Fatboy

Inherits:
Object
  • Object
show all
Defined in:
lib/fatboy.rb,
lib/fatboy/many.rb,
lib/fatboy/helpers.rb,
lib/fatboy/version.rb,
lib/fatboy/popularity.rb,
lib/fatboy/viewed_item.rb,
lib/fatboy/view_tracker.rb,
lib/fatboy/time_based_popularity.rb

Overview

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

Defined Under Namespace

Modules: Helpers Classes: Many, Popularity, TimeBasedPopularity, ViewTracker, ViewedItem

Constant Summary collapse

HOUR_FORMAT_STR =

Format string we use to store the views per hour

"%Y%m%d%H"
DAY_FORMAT_STR =

Format string we use to store the views per day

"%Y%m%d"
MONTH_FORMAT_STR =

Format string we use to store the views per month

"%Y%m"
YEAR_FORMAT_STR =

Format string we use to store the views per year

"%Y"
VERSION =

Gem version of Fatboy

"0.0.3"

Instance Method Summary collapse

Constructor Details

#initialize(redis: Redis.new) ⇒ Fatboy

Create a new Fatboy. Options:

* +redis:+ : The redis to store views in. By default, Redis.new

14
15
16
# File 'lib/fatboy.rb', line 14

def initialize(redis: Redis.new)
  @redis = redis
end

Instance Method Details

#[](obj) ⇒ Object

let users view with a shorthand


45
46
47
# File 'lib/fatboy.rb', line 45

def [](obj)
  view(obj)
end

#manyObject

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

Great if you need to view a lot of models.


22
23
24
# File 'lib/fatboy.rb', line 22

def many
  Fatboy::Many.new(@redis)
end

This method returns a Fatboy::Popularity, the main interface for determining the popularity of your models. Example:

fatboy.popular(Image)
fatboy.popular("Image")
fatboy.popular(model.class)

55
56
57
# File 'lib/fatboy.rb', line 55

def popular(model)
  Popularity.new(model, @redis)
end

#view(obj) ⇒ Object

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)


32
33
34
35
36
37
38
39
40
41
42
# 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) ⇒ Object


25
26
27
# File 'lib/fatboy.rb', line 25

def views_for(model)
  Fatboy::ViewTracker.new(@redis, model)
end