SuggestGrid Ruby Client
We will walk through how to get started with SuggestGrid Ruby Client in three steps:
If you did not sign up for SuggestGrid, this is the right time.
Getting Started
In this guide we will demonstrate how to display personalized recommendations on an existing Ruby project.
We have a movie catalog Ruby application, SuggestGridMovies, similar to IMDb. For logged in users we want to display movies that similar people viewed on movie pages. Let's implement this feature in five minutes with SuggestGrid!
1. Configuration
We are beginning the development by adding the client as a dependency.
gem 'suggestgrid'
Applications make their API requests to their dedicated sub-domain of suggestgrid.space
.
Most endpoints require a username and password for authentication.
An initial user name and password is given on sign up.
It is very convenient to configure SuggestGrid by setting an authenticated SUGGESTGRID_URL
environment variable in the format below:
http://{user}:{pass}@{region}.suggestgrid.space/{app-uuid}
You can authenticate your application using SUGGESTGRID_URL
environment variable like the example below:
sg_uri = ENV['SUGGESTGRID_URL']
# Initialize the SuggestGrid client.
::SuggestGridClient = SuggestGrid::SuggestGridClient.new sg_uri
Every recommendation logic needs to belong to a type.
For this demonstration we can create an implicit type named as views
.
This could be done either from the dashboard or with a snippet like this:
begin
SuggestGridClient.type.get_type('views')
puts "SuggestGrid type named 'views' already exists, skipping creation."
rescue SuggestGrid::APIException => e
SuggestGridClient.type.create_type('views')
puts "SuggestGrid type named 'views' is created."
end
2. Post actions
Once the type exists, let's start posting actions. We should invoke SuggestGrid client's action.post_action when an user views an item in our application.
We can do this by putting the snippet below on the relevant point:
# Send action to SuggestGrid.
begin
SuggestGridClient.action.post_action(SuggestGrid::Action.new('views',user.id, self.id))
rescue Exception => e
puts "Exception while sending action #{e}"
end
The more actions SuggestGrid gets, more relevant and diverse its responses are.
3. Get recommendations
Finally, let's show "movies similar users viewed" on movie pages.
SuggestGrid needs recommendation models for returning recommendations. Model generation is scheduled in every 24 hours. In addition, instant model generations can be triggered on the dashboard.
Once the first model generated for 'views' type, recommendations could be get using a snippet like the following:
begin
items_response = SuggestGridClient.recommendation.get_recommended_items({type: 'view', user_id: user.id, size: size})
items_response.items
rescue Exception => e
puts "Exception while getting recommendations #{e}"
end