module Octo::Helpers::ApiConsumerHelper
Helper Module for Octo
Consumer
Public Instance Methods
Get the API events. These are the ones that the client is billed for
This should eventually be placed under kong helpers when that is ready.
@return [Set<Symbol>] Set of api_events
# File lib/octocore/helpers/api_consumer_helper.rb, line 20 def api_events Set.new(%w(app.init app.login app.logout page.view productpage.view update.profile)) end
Handles Kafka messages and perform required operations @param [Hash] msg Hash
Message
# File lib/octocore/helpers/api_consumer_helper.rb, line 26 def handle(msg) msg_obj = Octo::Message::Message.new(msg) msg = msg_obj.to_h eventName = msg.delete(:event_name) if (valid_events.include?eventName) enterprise = checkEnterprise(msg) unless enterprise Octo.logger.info 'Unable to find enterprise. Something\'s wrong' end user = checkUser(enterprise, msg) hook_opts = { enterprise: enterprise, user: user } if api_events.include?eventName hook_opts[:event] = register_api_event(enterprise, eventName) Octo::ApiTrack.new(customid: msg[:id], created_at: Time.now, json_dump: msg_obj.message, type: eventName).save! end case eventName when 'app.init' Octo::AppInit.new(enterprise: enterprise, created_at: Time.now, userid: user.id).save! updateUserDeviceDetails(user, msg) hook_opts.merge!({type: 'init'}) call_hooks(eventName, hook_opts) when 'app.login' Octo::AppLogin.new(enterprise: enterprise, created_at: Time.now, userid: user.id).save! updateUserDeviceDetails(user, msg) call_hooks(eventName, hook_opts) when 'app.logout' event = Octo::AppLogout.new(enterprise: enterprise, created_at: Time.now, userid: user.id).save! updateUserDeviceDetails(user, msg) call_hooks(eventName, hook_opts) when 'page.view' page, categories, tags = checkPage(enterprise, msg) Octo::PageView.new(enterprise: enterprise, created_at: Time.now, userid: user.id, routeurl: page.routeurl ).save! hook_opts.merge!({type: page.routeurl}) updateUserDeviceDetails(user, msg) call_hooks(eventName, hook_opts) when 'productpage.view' product, categories, tags = checkProduct(enterprise, msg) Octo::ProductPageView.new( enterprise: enterprise, created_at: Time.now, userid: user.id, product_id: product.id ).save! updateUserDeviceDetails(user, msg) hook_opts.merge!({ product: product, categories: categories, tags: tags, type: product.routeurl }) call_hooks(eventName, hook_opts) when 'update.profile' checkUserProfileDetails(enterprise, user, msg) updateUserDeviceDetails(user, msg) call_hooks(eventName, hook_opts) when 'update.push_token' checkPushToken(enterprise, user, msg) checkPushKey(enterprise, msg) when 'funnel_update' checkRedisSession(enterprise,msg) end end end
Get all the valid events @return [Set<Symbol>] Valid events globally
# File lib/octocore/helpers/api_consumer_helper.rb, line 12 def valid_events Set.new(Octo.get_config(:allowed_events)) end
Private Instance Methods
Set Octo
callbacks @param [String] event Event name @param [Hash] *args Points to the messsage hash
# File lib/octocore/helpers/api_consumer_helper.rb, line 121 def call_hooks(event, *args) hook = [:after, event.gsub('.', '_')].join('_').to_sym Octo::Callbacks.run_hook(hook, *args) end
Checks for categories and creates if not found @param [Octo::Enterprise] enterprise The enterprise object @param [Array<String>] categories An array of categories to be checked @return [Array<Octo::Category>] An array of categories object
# File lib/octocore/helpers/api_consumer_helper.rb, line 353 def checkCategories(enterprise, categories) if categories categories.collect do |category| Octo::Category.findOrCreate({enterprise_id: enterprise.id, cat_text: category}) end end end
Check if the enterprise exists. Create a new enterprise if it does
not exist. This method makes sense because the enterprise authentication is handled by kong. Hence we can be sure that all these enterprises are valid.
@param [Hash] msg The message hash @return [Octo::Enterprise] The enterprise object
# File lib/octocore/helpers/api_consumer_helper.rb, line 230 def checkEnterprise(msg) Octo::Enterprise.findOrCreate({id: msg[:enterpriseId]}, {name: msg[:enterpriseName]}) end
Checks the existence of a page and creates if not found @param [Octo::Enterprise] enterprise The Enterprise
object @param [Hash] msg The message hash @return [Array<Octo::Page, Array<Octo::Category>, Array<Octo::Tag>] The
page object, array of categories objects and the array of tags object
# File lib/octocore/helpers/api_consumer_helper.rb, line 308 def checkPage(enterprise, msg) cats = checkCategories(enterprise, msg[:categories]) tags = checkTags(enterprise, msg[:tags]) args = { enterprise_id: enterprise.id, routeurl: msg[:routeUrl] } opts = { categories: Set.new(msg[:categories]), tags: Set.new(msg[:tags]) } page = Octo::Page.findOrCreateOrUpdate(args, opts) [page, cats, tags] end
Checks for existence of a product and creates if not found @param [Octo::Enterprise] enterprise The Enterprise
object @param [Hash] msg The message hash @return [Array<Octo::Product, Array<Octo::Category>, Array<Octo::Tag>] The
product object, array of categories objects and the array of tags object
# File lib/octocore/helpers/api_consumer_helper.rb, line 330 def checkProduct(enterprise, msg) categories = checkCategories(enterprise, msg[:categories]) tags = checkTags(enterprise, msg[:tags]) args = { enterprise_id: enterprise.id, id: msg[:productId] } opts = { categories: Set.new(msg[:categories]), tags: Set.new(msg[:tags]), price: msg[:price].to_f.round(2), name: msg[:productName], routeurl: msg[:routeUrl] } prod = Octo::Product.findOrCreateOrUpdate(args, opts) [prod, categories, tags] end
Checks for push keys and creates or updates it @param [Octo::Enterprise] enterprise The Enterprise
object @param [Hash] msg The message hash @return [Octo::PushKey] The push key object corresponding to this user
# File lib/octocore/helpers/api_consumer_helper.rb, line 213 def checkPushKey(enterprise, msg) args = { enterprise_id: enterprise.id, push_type: msg[:pushType].to_i } opts = { key: msg[:pushKey] } Octo::PushKey.findOrCreateOrUpdate(args, opts) end
Checks for push tokens and creates or updates it @param [Octo::Enterprise] enterprise The Enterprise
object @param [Octo::User] user The user to whom this token belongs to @param [Hash] msg The message hash @return [Octo::PushToken] The push token object corresponding to this user
# File lib/octocore/helpers/api_consumer_helper.rb, line 197 def checkPushToken(enterprise, user, msg) args = { user_id: user.id, user_enterprise_id: enterprise.id, push_type: msg[:pushType].to_i } opts = { pushtoken: msg[:pushToken] } Octo::PushToken.findOrCreateOrUpdate(args, opts) end
Checks for msg in redis, parses it and
then calls updateFunnelTracker.
@param [Octo::Enterprise] enterprise The Enterprise
object @param [Hash] msg The message hash, MUST contain, :rediskey @return [void]
# File lib/octocore/helpers/api_consumer_helper.rb, line 131 def checkRedisSession(enterprise,msg) sessionList = Cequel::Record.redis.lrange(msg[:rediskey],0,-1) Cequel::Record.redis.del(msg[:rediskey]) sessionList.each_index{ |index| if index!=(sessionList.length-1) updateFunnelTracker(enterprise,sessionList[index],sessionList[index+1]) end } end
Checks for tags and creates if not found @param [Octo::Enterprise] enterprise The enterprise object @param [Array<String>] tags An array of tags to be checked @return [Array<Octo::Tag>] An array of tags object
# File lib/octocore/helpers/api_consumer_helper.rb, line 366 def checkTags(enterprise, tags) if tags tags.collect do |tag| Octo::Tag.findOrCreate({enterprise_id: enterprise.id, tag_text: tag}) end end end
Checks for user and creates if not exists @param [Octo::Enterprise] enterprise The Enterprise
object @param [Hash] msg The message hash @return [Octo::User] The push user object corresponding to this user
# File lib/octocore/helpers/api_consumer_helper.rb, line 239 def checkUser(enterprise, msg) args = { enterprise_id: enterprise.id, id: msg[:userId] } Octo::User.findOrCreate(args) end
Creeate or Update Profile details of a user @param [Octo::Enterprise] enterprise Object of enterprise model @param [Octo::User] user Object of user model @param [Hash] msg Hash
of message @return [Octo::UserProfileDetails] User
Profile
# File lib/octocore/helpers/api_consumer_helper.rb, line 175 def checkUserProfileDetails(enterprise, user, msg) args = { user_id: user.id, user_enterprise_id: enterprise.id, email: msg[:profileDetails].fetch('email') } opts = { username: msg[:profileDetails].fetch('username', ''), gender: msg[:profileDetails].fetch('gender', ''), dob: msg[:profileDetails].fetch('dob', ''), alternate_email: msg[:profileDetails].fetch('alternate_email', ''), mobile: msg[:profileDetails].fetch('mobile', ''), extras: msg[:profileDetails].fetch('extras', '{}').to_s } Octo::UserProfileDetails.findOrCreateOrUpdate(args, opts) end
Make an entry of Event type @param [Octo::Enterprise] enterprise @param [String] event_name Name of Event
# File lib/octocore/helpers/api_consumer_helper.rb, line 113 def register_api_event(enterprise, event_name) Octo::ApiEvent.findOrCreate({ enterprise_id: enterprise.id, eventname: event_name}) end
Checks if transition from page1 -> page2 exists, then
updates the value of its weight, else creates the transition with default weight 1. It also creates an entry for page 2 <- page 1, which helps us understand the incoming entries for a particular node.
@param [Octo::Enterprise] enterprise The Enterprise
object @param [string] page1 The url of page1 @param [string] page2 The url of page2 @return [void]
# File lib/octocore/helpers/api_consumer_helper.rb, line 150 def updateFunnelTracker(enterprise,page1,page2) args_to = { enterprise_id: enterprise.id, p1: page1, direction:1, p2: page2 } args_from = { enterprise_id: enterprise.id, p1: page2, direction: 0, p2: page1 } counters = { weight:1, } Octo::FunnelTracker.findOrCreateOrAdjust(args_to,counters) Octo::FunnelTracker.findOrCreateOrAdjust(args_from,counters) end
Updates location for a user @param [Octo::User] user The user to whom this token belongs to @param [Hash] msg The message hash @return [Octo::UserLocationHistory] The location history object
corresponding to this user
# File lib/octocore/helpers/api_consumer_helper.rb, line 252 def updateLocationHistory(user, msg) Octo::UserLocationHistory.new( user: user, latitude: msg[:phone].fetch('latitude', 0.0), longitude: msg[:phone].fetch('longitude', 0.0), created_at: Time.now ).save! end
Updates user's browser details @param [Hash] args The user details to whom this token belongs to @param [Hash] msg The message hash @return [Octo::UserBrowserDetails] The browser details object
corresponding to this user
# File lib/octocore/helpers/api_consumer_helper.rb, line 294 def updateUserBrowserDetails(args, msg) opts = {name: msg[:browser].fetch('name', ''), platform: msg[:browser].fetch('platform', ''), manufacturer: msg[:browser].fetch('manufacturer', ''), cookieid: msg[:browser].fetch('cookieid', '')} Octo::UserBrowserDetails.findOrCreateOrUpdate(args, opts) end
Updates user's device details @param [Octo::User] user The user to whom this token belongs to @param [Hash] msg The message hash
# File lib/octocore/helpers/api_consumer_helper.rb, line 264 def updateUserDeviceDetails(user, msg) args = {user_id: user.id, user_enterprise_id: user.enterprise.id} # Check Device Type if msg[:browser] updateUserBrowserDetails(args, msg) elsif msg[:phone] updateLocationHistory(user, msg) updateUserPhoneDetails(args, msg) end end
Updates user's phone details @param [Hash] args The user details to whom this token belongs to @param [Hash] msg The message hash @return [Octo::UserPhoneDetails] The phone details object
corresponding to this user
# File lib/octocore/helpers/api_consumer_helper.rb, line 281 def updateUserPhoneDetails(args, msg) opts = {deviceid: msg[:phone].fetch('deviceId', ''), manufacturer: msg[:phone].fetch('manufacturer', ''), model: msg[:phone].fetch('model', ''), os: msg[:phone].fetch('os', '')} Octo::UserPhoneDetails.findOrCreateOrUpdate(args, opts) end