Ruby API ClientΒΆ

require 'recombee_api_client'
include RecombeeApiClient

client = RecombeeClient.new('client-test', 'jGGQ6ZKa8rQ1zTAyxTc0EMn55YPF7FJLUtaMLhbsGxmvwxgTwXYqmUk5xVZFw98L')

# Generate some random purchases of items by users
NUM = 100
PROBABILITY_PURCHASED = 0.1

users = (1..NUM).map { |i| "user-#{i}" }
items = (1..NUM).map { |i| "item-#{i}" }
purchases = []

users.each do |user_id|
  purchased = items.select { |_| rand(0.0..1.0) < PROBABILITY_PURCHASED }
  purchased.each { |item_id| purchases.push(

                AddPurchase.new(user_id, item_id,'cascadeCreate' => true)
                                                  # Use cascadeCreate to create the
                                                  # yet non-existing users and items
                )}

end

begin
  # Send the data to Recombee, use Batch for faster processing of larger data
  client.send(Batch.new(purchases))

  # Get recommendations for user 'user-25'
  recommended = client.send(UserBasedRecommendation.new('user-25', 5))
  puts "Recommended items for user-25: #{recommended}"
rescue APIError => e
  puts e
  # Use fallback
end