Skip to content

Commit

Permalink
Support returnProperties and includedProperties parameters of ListIte…
Browse files Browse the repository at this point in the history
…ms and ListUsers. Send User-Agent HTTP header.
  • Loading branch information
OndraFiedler committed Aug 25, 2017
1 parent 6732701 commit 3ad7396
Show file tree
Hide file tree
Showing 33 changed files with 229 additions and 89 deletions.
11 changes: 7 additions & 4 deletions lib/recombee_api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class RecombeeClient
include HTTParty

BATCH_MAX_SIZE = 10000
USER_AGENT = {'User-Agent' => 'recombee-ruby-api-client/1.4.0'}

##
# - +account+ -> Name of your account at Recombee
Expand Down Expand Up @@ -63,21 +64,23 @@ def send(request)
private

def put(request, uri, timeout)
response = self.class.put(uri, timeout: timeout)
response = self.class.put(uri, body: request.body_parameters.to_json,
headers: { 'Content-Type' => 'application/json' }.merge(USER_AGENT),
timeout: timeout)
check_errors(response, request)
response.body
end

def get(request, uri, timeout)
response = self.class.get(uri, timeout: timeout)
response = self.class.get(uri, timeout: timeout, headers: USER_AGENT)
check_errors(response, request)
JSON.parse(response.body)
end

def post(request, uri, timeout)
# pass arguments in body
response = self.class.post(uri, body: request.body_parameters.to_json,
headers: { 'Content-Type' => 'application/json' },
headers: { 'Content-Type' => 'application/json' }.merge(USER_AGENT),
timeout: timeout)
check_errors(response, request)
begin
Expand All @@ -88,7 +91,7 @@ def post(request, uri, timeout)
end

def delete(request, uri, timeout)
response = self.class.delete(uri, timeout: timeout)
response = self.class.delete(uri, timeout: timeout, headers: USER_AGENT)
check_errors(response, request)
response.body
end
Expand Down
4 changes: 2 additions & 2 deletions lib/recombee_api_client/api/add_bookmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def body_parameters
p = Hash.new
p['userId'] = @user_id
p['itemId'] = @item_id
p['timestamp'] = @optional['timestamp'] if @optional['timestamp']
p['cascadeCreate'] = @optional['cascadeCreate'] if @optional['cascadeCreate']
p['timestamp'] = @optional['timestamp'] if @optional.include? 'timestamp'
p['cascadeCreate'] = @optional['cascadeCreate'] if @optional.include? 'cascadeCreate'
p
end

Expand Down
4 changes: 2 additions & 2 deletions lib/recombee_api_client/api/add_cart_addition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def body_parameters
p = Hash.new
p['userId'] = @user_id
p['itemId'] = @item_id
p['timestamp'] = @optional['timestamp'] if @optional['timestamp']
p['cascadeCreate'] = @optional['cascadeCreate'] if @optional['cascadeCreate']
p['timestamp'] = @optional['timestamp'] if @optional.include? 'timestamp'
p['cascadeCreate'] = @optional['cascadeCreate'] if @optional.include? 'cascadeCreate'
p
end

Expand Down
6 changes: 3 additions & 3 deletions lib/recombee_api_client/api/add_detail_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ def body_parameters
p = Hash.new
p['userId'] = @user_id
p['itemId'] = @item_id
p['timestamp'] = @optional['timestamp'] if @optional['timestamp']
p['duration'] = @optional['duration'] if @optional['duration']
p['cascadeCreate'] = @optional['cascadeCreate'] if @optional['cascadeCreate']
p['timestamp'] = @optional['timestamp'] if @optional.include? 'timestamp'
p['duration'] = @optional['duration'] if @optional.include? 'duration'
p['cascadeCreate'] = @optional['cascadeCreate'] if @optional.include? 'cascadeCreate'
p
end

Expand Down
4 changes: 2 additions & 2 deletions lib/recombee_api_client/api/add_purchase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def body_parameters
p = Hash.new
p['userId'] = @user_id
p['itemId'] = @item_id
p['timestamp'] = @optional['timestamp'] if @optional['timestamp']
p['cascadeCreate'] = @optional['cascadeCreate'] if @optional['cascadeCreate']
p['timestamp'] = @optional['timestamp'] if @optional.include? 'timestamp'
p['cascadeCreate'] = @optional['cascadeCreate'] if @optional.include? 'cascadeCreate'
p
end

Expand Down
4 changes: 2 additions & 2 deletions lib/recombee_api_client/api/add_rating.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def body_parameters
p['userId'] = @user_id
p['itemId'] = @item_id
p['rating'] = @rating
p['timestamp'] = @optional['timestamp'] if @optional['timestamp']
p['cascadeCreate'] = @optional['cascadeCreate'] if @optional['cascadeCreate']
p['timestamp'] = @optional['timestamp'] if @optional.include? 'timestamp'
p['cascadeCreate'] = @optional['cascadeCreate'] if @optional.include? 'cascadeCreate'
p
end

Expand Down
2 changes: 1 addition & 1 deletion lib/recombee_api_client/api/insert_to_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def body_parameters
p = Hash.new
p['itemType'] = @item_type
p['itemId'] = @item_id
p['cascadeCreate'] = @optional['cascadeCreate'] if @optional['cascadeCreate']
p['cascadeCreate'] = @optional['cascadeCreate'] if @optional.include? 'cascadeCreate'
p
end

Expand Down
2 changes: 1 addition & 1 deletion lib/recombee_api_client/api/insert_to_series.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def body_parameters
p['itemType'] = @item_type
p['itemId'] = @item_id
p['time'] = @time
p['cascadeCreate'] = @optional['cascadeCreate'] if @optional['cascadeCreate']
p['cascadeCreate'] = @optional['cascadeCreate'] if @optional.include? 'cascadeCreate'
p
end

Expand Down
53 changes: 31 additions & 22 deletions lib/recombee_api_client/api/item_based_recommendation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ module RecombeeApiClient
##
#Recommends set of items that are somehow related to one given item, *X*. Typical scenario for using item-based recommendation is when user *A* is viewing *X*. Then you may display items to the user that he might be also interested in. Item-recommendation request gives you Top-N such items, optionally taking the target user *A* into account.
#
# It is also possible to use POST HTTP method (for example in case of very long ReQL filter) - query parameters then become body parameters.
#
class ItemBasedRecommendation < ApiRequest
attr_reader :item_id, :count, :target_user_id, :user_impact, :filter, :booster, :allow_nonexistent, :cascade_create, :scenario, :return_properties, :included_properties, :diversity, :min_relevance, :rotation_rate, :rotation_time
attr_reader :item_id, :count, :target_user_id, :user_impact, :filter, :booster, :allow_nonexistent, :cascade_create, :scenario, :return_properties, :included_properties, :diversity, :min_relevance, :rotation_rate, :rotation_time, :expert_settings
attr_accessor :timeout
attr_accessor :ensure_https

##
# * *Required arguments*
# - +item_id+ -> ID of the item recommendations for which are to be generated.
# - +item_id+ -> ID of the item for which the recommendations are to be generated.
# - +count+ -> Number of items to be recommended (N for the top-N recommendation).
#
# * *Optional arguments (given as hash optional)*
Expand All @@ -25,15 +27,18 @@ class ItemBasedRecommendation < ApiRequest
#Specifying the *targetUserId* is beneficial because:
#
#* It makes the recommendations personalized
#* Allows calculations of Actions and Conversions in the graphical user interface, as Recombee can pair the user who got recommendations and who afterwards viewed/purchased an item.
#* Allows the calculation of Actions and Conversions in the graphical user interface,
# as Recombee can pair the user who got recommendations and who afterwards viewed/purchased an item.
#
#For the above reasons, we encourage you to set the *targetUserId* even for anonymous/unregistered users (i.e. use their session ID).
#
# - +userImpact+ -> If *targetUserId* parameter is present, the recommendations are biased towards the user given. Using *userImpact*, you may control this bias. For an extreme case of `userImpact=0.0`, the interactions made by the user are not taken into account at all (with the exception of history-based blacklisting), for `userImpact=1.0`, you'll get user-based recommendation. The default value is `0.1`
# - +userImpact+ -> If *targetUserId* parameter is present, the recommendations are biased towards the user given. Using *userImpact*, you may control this bias. For an extreme case of `userImpact=0.0`, the interactions made by the user are not taken into account at all (with the exception of history-based blacklisting), for `userImpact=1.0`, you'll get user-based recommendation. The default value is `0`.
#
# - +filter+ -> Boolean-returning [ReQL](https://docs.recombee.com/reql.html) expression which allows you to filter recommended items based on the values of their attributes.
# - +booster+ -> Number-returning [ReQL](https://docs.recombee.com/reql.html) expression which allows you to boost recommendation rate of some items based on the values of their attributes.
# - +allowNonexistent+ -> Instead of causing HTTP 404 error, returns some (non-personalized) recommendations if either item of given *itemId* or user of given *targetUserId* does not exist in the database. It creates neither of the missing entities in the database.
# - +cascadeCreate+ -> If item of given *itemId* or user of given *targetUserId* doesn't exist in the database, it creates the missing enity/entities and returns some (non-personalized) recommendations. This allows for example rotations in the following recommendations for the user of given *targetUserId*, as the user will be already known to the system.
# - +scenario+ -> Scenario defines a particular application of recommendations. It can be for example "homepage" or "cart". The AI which optimizes models in order to get the best results may optimize different scenarios separately, or even use different models in each of the scenarios.
# - +scenario+ -> Scenario defines a particular application of recommendations. It can be for example "homepage", "cart" or "emailing". You can see each scenario in the UI separately, so you can check how well each application performs. The AI which optimizes models in order to get the best results may optimize different scenarios separately, or even use different models in each of the scenarios.
# - +returnProperties+ -> With `returnProperties=true`, property values of the recommended items are returned along with their IDs in a JSON dictionary. The acquired property values can be used for easy displaying of the recommended items to the user.
#
#Example response:
Expand Down Expand Up @@ -80,7 +85,9 @@ class ItemBasedRecommendation < ApiRequest
#
# - +rotationRate+ -> **Expert option** If the *targetUserId* is provided: If your users browse the system in real-time, it may easily happen that you wish to offer them recommendations multiple times. Here comes the question: how much should the recommendations change? Should they remain the same, or should they rotate? Recombee API allows you to control this per-request in backward fashion. You may penalize an item for being recommended in the near past. For the specific user, `rotationRate=1` means maximal rotation, `rotationRate=0` means absolutely no rotation. You may also use, for example `rotationRate=0.2` for only slight rotation of recommended items.
#
# - +rotationTime+ -> **Expert option** If the *targetUserId* is provided: Taking *rotationRate* into account, specifies how long time it takes to an item to fully recover from the penalization. For example, `rotationTime=7200.0` means that items recommended more than 2 hours ago are definitely not penalized anymore. Currently, the penalization is linear, so for `rotationTime=7200.0`, an item is still penalized by `0.5` to the user after 1 hour.
# - +rotationTime+ -> **Expert option** If the *targetUserId* is provided: Taking *rotationRate* into account, specifies how long time it takes to an item to recover from the penalization. For example, `rotationTime=7200.0` means that items recommended less than 2 hours ago are penalized.
#
# - +expertSettings+ -> Dictionary of custom options.
#
#
def initialize(item_id, count, optional = {})
Expand All @@ -99,43 +106,45 @@ def initialize(item_id, count, optional = {})
@min_relevance = optional['minRelevance']
@rotation_rate = optional['rotationRate']
@rotation_time = optional['rotationTime']
@expert_settings = optional['expertSettings']
@optional = optional
@timeout = 3000
@ensure_https = false
@optional.each do |par, _|
fail UnknownOptionalParameter.new(par) unless ["targetUserId","userImpact","filter","booster","allowNonexistent","cascadeCreate","scenario","returnProperties","includedProperties","diversity","minRelevance","rotationRate","rotationTime"].include? par
fail UnknownOptionalParameter.new(par) unless ["targetUserId","userImpact","filter","booster","allowNonexistent","cascadeCreate","scenario","returnProperties","includedProperties","diversity","minRelevance","rotationRate","rotationTime","expertSettings"].include? par
end
end

# HTTP method
def method
:get
:post
end

# Values of body parameters as a Hash
def body_parameters
p = Hash.new
p['count'] = @count
p['targetUserId'] = @optional['targetUserId'] if @optional.include? 'targetUserId'
p['userImpact'] = @optional['userImpact'] if @optional.include? 'userImpact'
p['filter'] = @optional['filter'] if @optional.include? 'filter'
p['booster'] = @optional['booster'] if @optional.include? 'booster'
p['allowNonexistent'] = @optional['allowNonexistent'] if @optional.include? 'allowNonexistent'
p['cascadeCreate'] = @optional['cascadeCreate'] if @optional.include? 'cascadeCreate'
p['scenario'] = @optional['scenario'] if @optional.include? 'scenario'
p['returnProperties'] = @optional['returnProperties'] if @optional.include? 'returnProperties'
p['includedProperties'] = @optional['includedProperties'] if @optional.include? 'includedProperties'
p['diversity'] = @optional['diversity'] if @optional.include? 'diversity'
p['minRelevance'] = @optional['minRelevance'] if @optional.include? 'minRelevance'
p['rotationRate'] = @optional['rotationRate'] if @optional.include? 'rotationRate'
p['rotationTime'] = @optional['rotationTime'] if @optional.include? 'rotationTime'
p['expertSettings'] = @optional['expertSettings'] if @optional.include? 'expertSettings'
p
end

# Values of query parameters as a Hash.
# name of parameter => value of the parameter
def query_parameters
params = {}
params['count'] = @count
params['targetUserId'] = @optional['targetUserId'] if @optional['targetUserId']
params['userImpact'] = @optional['userImpact'] if @optional['userImpact']
params['filter'] = @optional['filter'] if @optional['filter']
params['booster'] = @optional['booster'] if @optional['booster']
params['allowNonexistent'] = @optional['allowNonexistent'] if @optional['allowNonexistent']
params['cascadeCreate'] = @optional['cascadeCreate'] if @optional['cascadeCreate']
params['scenario'] = @optional['scenario'] if @optional['scenario']
params['returnProperties'] = @optional['returnProperties'] if @optional['returnProperties']
params['includedProperties'] = @optional['includedProperties'] if @optional['includedProperties']
params['diversity'] = @optional['diversity'] if @optional['diversity']
params['minRelevance'] = @optional['minRelevance'] if @optional['minRelevance']
params['rotationRate'] = @optional['rotationRate'] if @optional['rotationRate']
params['rotationTime'] = @optional['rotationTime'] if @optional['rotationTime']
params
end

Expand Down
2 changes: 1 addition & 1 deletion lib/recombee_api_client/api/list_groups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ListGroups < ApiRequest
##
#
def initialize()
@timeout = 30000
@timeout = 239000
@ensure_https = false
end

Expand Down
2 changes: 1 addition & 1 deletion lib/recombee_api_client/api/list_item_bookmarks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ListItemBookmarks < ApiRequest
#
def initialize(item_id)
@item_id = item_id
@timeout = 1000
@timeout = 100000
@ensure_https = false
end

Expand Down
2 changes: 1 addition & 1 deletion lib/recombee_api_client/api/list_item_cart_additions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ListItemCartAdditions < ApiRequest
#
def initialize(item_id)
@item_id = item_id
@timeout = 1000
@timeout = 100000
@ensure_https = false
end

Expand Down
2 changes: 1 addition & 1 deletion lib/recombee_api_client/api/list_item_detail_views.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ListItemDetailViews < ApiRequest
#
def initialize(item_id)
@item_id = item_id
@timeout = 1000
@timeout = 100000
@ensure_https = false
end

Expand Down
2 changes: 1 addition & 1 deletion lib/recombee_api_client/api/list_item_purchases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ListItemPurchases < ApiRequest
#
def initialize(item_id)
@item_id = item_id
@timeout = 1000
@timeout = 100000
@ensure_https = false
end

Expand Down
2 changes: 1 addition & 1 deletion lib/recombee_api_client/api/list_item_ratings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ListItemRatings < ApiRequest
#
def initialize(item_id)
@item_id = item_id
@timeout = 1000
@timeout = 100000
@ensure_https = false
end

Expand Down
56 changes: 53 additions & 3 deletions lib/recombee_api_client/api/list_items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,68 @@ module RecombeeApiClient
##
#Gets a list of IDs of items currently present in the catalog.
class ListItems < ApiRequest
attr_reader :filter
attr_reader :filter, :count, :offset, :return_properties, :included_properties
attr_accessor :timeout
attr_accessor :ensure_https

##
#
# * *Optional arguments (given as hash optional)*
# - +filter+ -> Boolean-returning [ReQL](https://docs.recombee.com/reql.html) expression, which allows you to filter items to be listed. Only the items for which the expression is *true* will be returned.
# - +count+ -> The number of items to be listed.
# - +offset+ -> Specifies the number of items to skip (ordered by `itemId`).
# - +returnProperties+ -> With `returnProperties=true`, property values of the listed items are returned along with their IDs in a JSON dictionary.
#
#Example response:
#```
# [
# {
# "itemId": "tv-178",
# "description": "4K TV with 3D feature",
# "categories": ["Electronics", "Televisions"],
# "price": 342,
# "url": "myshop.com/tv-178"
# },
# {
# "itemId": "mixer-42",
# "description": "Stainless Steel Mixer",
# "categories": ["Home & Kitchen"],
# "price": 39,
# "url": "myshop.com/mixer-42"
# }
# ]
#```
#
# - +includedProperties+ -> Allows to specify, which properties should be returned when `returnProperties=true` is set. The properties are given as a comma-separated list.
#
#Example response for `includedProperties=description,price`:
#```
# [
# {
# "itemId": "tv-178",
# "description": "4K TV with 3D feature",
# "price": 342
# },
# {
# "itemId": "mixer-42",
# "description": "Stainless Steel Mixer",
# "price": 39
# }
# ]
#```
#
#
def initialize(optional = {})
@filter = optional['filter']
@count = optional['count']
@offset = optional['offset']
@return_properties = optional['returnProperties']
@included_properties = optional['includedProperties']
@optional = optional
@timeout = 30000
@timeout = 600000
@ensure_https = false
@optional.each do |par, _|
fail UnknownOptionalParameter.new(par) unless ["filter"].include? par
fail UnknownOptionalParameter.new(par) unless ["filter","count","offset","returnProperties","includedProperties"].include? par
end
end

Expand All @@ -44,6 +90,10 @@ def body_parameters
def query_parameters
params = {}
params['filter'] = @optional['filter'] if @optional['filter']
params['count'] = @optional['count'] if @optional['count']
params['offset'] = @optional['offset'] if @optional['offset']
params['returnProperties'] = @optional['returnProperties'] if @optional['returnProperties']
params['includedProperties'] = @optional['includedProperties'] if @optional['includedProperties']
params
end

Expand Down
2 changes: 1 addition & 1 deletion lib/recombee_api_client/api/list_series.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ListSeries < ApiRequest
##
#
def initialize()
@timeout = 30000
@timeout = 239000
@ensure_https = false
end

Expand Down
2 changes: 1 addition & 1 deletion lib/recombee_api_client/api/list_user_bookmarks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ListUserBookmarks < ApiRequest
#
def initialize(user_id)
@user_id = user_id
@timeout = 1000
@timeout = 100000
@ensure_https = false
end

Expand Down
2 changes: 1 addition & 1 deletion lib/recombee_api_client/api/list_user_cart_additions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ListUserCartAdditions < ApiRequest
#
def initialize(user_id)
@user_id = user_id
@timeout = 1000
@timeout = 100000
@ensure_https = false
end

Expand Down
Loading

0 comments on commit 3ad7396

Please sign in to comment.