Module: Octo::OctoHooks::ClassMethods

Defined in:
lib/octocore-mongo/callbacks.rb

Overview

Add all the post-hook-call methods here. Also, extend the module

from here.

Instance Method Summary collapse

Instance Method Details

#add_session(opts) ⇒ Object

Adds user session for page_view and product_page_view to redis. It self expires in n seconds from last hit



75
76
77
78
79
80
81
82
83
# File 'lib/octocore-mongo/callbacks.rb', line 75

def add_session(opts)
  if Octo.is_not_flagged?(Octo::Funnel)
    if opts.has_key?(:type)
      createRedisShadowKey(opts[:enterprise].id.to_s + '_' + opts[:user].id.to_s,
                           opts[:type].to_s,
                           Octo.get_config(:session_length))
    end
  end
end

#createRedisShadowKey(keyname, value, duration) ⇒ Object

Method was created because when a redis key

expires you can just catch the key name,
and not its value. So what we do is
create a shadow key for the given key name
and make it expire in the given amt of
time (seconds).

This helps us in catching the event

when the (shadow) key expires, after which we
read the value of the main key and later
delete the main key

You can change it from rpush to lpush, or set

whichever you want to use.


97
98
99
100
# File 'lib/octocore-mongo/callbacks.rb', line 97

def createRedisShadowKey(keyname, value, duration)
  MongoMapper::Document.redis.setex("shadow:" + keyname,duration,"")
  MongoMapper::Document.redis.rpush(keyname, value)
end

#update_counters(opts) ⇒ Object

Updates the counters of various types depending

on the event.

Parameters:

  • opts (Hash)

    The options hash



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/octocore-mongo/callbacks.rb', line 54

def update_counters(opts)
  if opts.has_key?(:product)
    Octo::ProductHit.increment_for(opts[:product])
  end
  if opts.has_key?(:categories)
    opts[:categories].each do |cat|
      Octo::CategoryHit.increment_for(cat)
    end
  end
  if opts.has_key?(:tags)
    opts[:tags].each do |tag|
      Octo::TagHit.increment_for(tag)
    end
  end
  if opts.has_key?(:event)
    Octo::ApiHit.increment_for(opts[:event])
  end
end