Recombee Docs
Visit recombee.comStart Free
docs20User Documentation
adminuiAdmin UI
reql32ReQL
codeAPI Clients & Integrations
cookhatScenario Recipes
suitcaseMisc

Ruby API Client

Example

Ruby
require 'recombee_api_client'
include RecombeeApiClient

client = RecombeeClient.new('--your-database-id--', '--your-db-private-token--', {:region => 'us-west'})

# 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'
  response = client.send(RecommendItemsToUser.new('user-25', 5))
  puts "Recommended items for user-25: #{response}"

  # User scrolled down - get next 3 recommended items
  response = client.send(RecommendNextItems.new(response['recommId'], 3))
  puts "Next recommended items for user-25: #{response}"

rescue APIError => e
  puts e
  # Use fallback
end
© Copyright 2024, Recombee s.r.o
docs.recombee.com