module Octo::NewsFeed::Feed

Public Instance Methods

feed_for(user, opts = {}) click to toggle source

Generate the newsfeed for a user. Optionally,

specify a time so that things relevant at that time would show

@param [Octo::User] user The user for whom feed is to be generated @param [Hash] opts The options to use for for generating feed

# File lib/octonewsfeed/feed.rb, line 14
def feed_for(user, opts = {})
  feed_products = {
      recommended: recommender.recommended_products(user),
      trending: trending_prods(user.enterprise),
      similar: similar_prods_user(user)
  }
  weave(feed_products)
end

Private Instance Methods

recommender() click to toggle source

Generate the recommender instance @return [Octo::Recommender]

# File lib/octonewsfeed/feed.rb, line 27
def recommender
  @recommender = Octo::Recommender.new unless @recommender
  @recommender
end
similar_prods(products, opts={}) click to toggle source

Get similar products for a set of products. @param [Array<Octo::Product>] products An array of products for whom

similarities have to be found

@return [Hash<Octo::Product => Array<Octo::Product>] Hash containing similar

products array as value for product as key
# File lib/octonewsfeed/feed.rb, line 67
def similar_prods(products, opts={})
  products.inject({}) do |r,e|
    r[e] = e.similarities(opts)
    r
  end
end
similar_prods_user(user, opts={}) click to toggle source

Finds the products similar to the ones that the user has seen before. @param [Octo::User] user The user for whom similar products have to be

found

@return [Array<Octo::Product>] An array of products

# File lib/octonewsfeed/feed.rb, line 49
def similar_prods_user(user, opts={})
  args = {
      enterprise_id: user.enterprise.id,
      userid: user.id
  }
  last_ppvs = Octo::ProductPageView.where(args).limit(opts.fetch(:limit, 10))
  last_seen_products = last_ppvs.collect do |ppv|
    Octo::Product.get_cached({ enterprise_id: user.enterprise.id,
                               id: ppv.product_id})
  end
  similar_prods last_seen_products
end