class Fatboy::ViewTracker

Public Class Methods

new(redis, model) click to toggle source
# File lib/fatboy/view_tracker.rb, line 5
def initialize(redis, model)
  @redis = redis
  @model_name = model.class.to_s
  @id = model.id
end

Public Instance Methods

day(time) click to toggle source

How often the model was viewed in a particular day

# File lib/fatboy/view_tracker.rb, line 41
def day(time)
  time = Fatboy::Helpers.day_format(time.utc)
  store = Fatboy::Helpers.format_store(@model_name, time)
  get_score(store)
end
hour(time) click to toggle source

How often the model was viewed on a particular hour

# File lib/fatboy/view_tracker.rb, line 33
def hour(time)
  time = Fatboy::Helpers.hour_format(time.utc)
  store = Fatboy::Helpers.format_store(@model_name, time)
  @redis.zscore(store, @id)
end
month(time) click to toggle source

How often the model was viewed in a particular month

# File lib/fatboy/view_tracker.rb, line 48
def month(time)
  time = Fatboy::Helpers.month_format(time.utc)
  store = Fatboy::Helpers.format_store(@model_name, time)
  get_score(store)
end
this_hour() click to toggle source

How often this model was viewed this hour

# File lib/fatboy/view_tracker.rb, line 12
def this_hour
  hour(Time.now)
end
this_month() click to toggle source

How often this model was viewed this month

# File lib/fatboy/view_tracker.rb, line 23
def this_month
  day(Time.now)
end
this_year() click to toggle source

How often this model was viewed this year

# File lib/fatboy/view_tracker.rb, line 28
def this_year
  year(Time.now)
end
today() click to toggle source

How often this model was viewed today

# File lib/fatboy/view_tracker.rb, line 17
def today
  day(Time.now)
end
year(time) click to toggle source

How often teh model was viewed in a particular year

# File lib/fatboy/view_tracker.rb, line 55
def year(time)
  time = Fatboy::Helpers.month_format(time.utc)
  store = Fatboy::Helpers.format_store(@model_name, time)
  get_score(store)
end

Protected Instance Methods

get_score(store) click to toggle source
# File lib/fatboy/view_tracker.rb, line 61
def get_score(store)
  @redis.zscore(store, @id)
end