Class: Fatboy::ViewTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/fatboy/view_tracker.rb

Instance Method Summary collapse

Constructor Details

#initialize(redis, model) ⇒ ViewTracker

Returns a new instance of ViewTracker.



5
6
7
8
9
# File 'lib/fatboy/view_tracker.rb', line 5

def initialize(redis, model)
  @redis = redis
  @model_name = model.class.to_s
  @id = model.id
end

Instance Method Details

#day(time) ⇒ Object

How often the model was viewed in a particular day



41
42
43
44
45
# 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) ⇒ Object

How often the model was viewed on a particular hour



33
34
35
36
37
# 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) ⇒ Object

How often the model was viewed in a particular month



48
49
50
51
52
# 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_hourObject

How often this model was viewed this hour



12
13
14
# File 'lib/fatboy/view_tracker.rb', line 12

def this_hour
  hour(Time.now)
end

#this_monthObject

How often this model was viewed this month



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

def this_month
  day(Time.now)
end

#this_yearObject

How often this model was viewed this year



28
29
30
# File 'lib/fatboy/view_tracker.rb', line 28

def this_year
  year(Time.now)
end

#todayObject

How often this model was viewed today



17
18
19
# File 'lib/fatboy/view_tracker.rb', line 17

def today
  day(Time.now)
end

#year(time) ⇒ Object

How often teh model was viewed in a particular year



55
56
57
58
59
# 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