Skip to content

Commit 12b77c9

Browse files
committed
Support setting the region of your DB. Added Update / Delete More Items endpoints.
Breaking changes: RecombeeClient.initialize: protocol parameter (HTTP/HTTPS) moved to options hash. Removed deprecated endpoints Item Based Recommendation and User Based Recommendation.
1 parent 31255d7 commit 12b77c9

21 files changed

+224
-377
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Or install it yourself as:
2929
require 'recombee_api_client'
3030
include RecombeeApiClient
3131

32-
client = RecombeeClient('--my-database-id--', '--db-private-token--')
32+
client = RecombeeClient('--my-database-id--', '--db-private-token--', {:region => 'us-west'})
3333

3434
# Generate some random purchases of items by users
3535
NUM = 100
@@ -76,7 +76,7 @@ include RecombeeApiClient
7676
NUM = 100
7777
PROBABILITY_PURCHASED = 0.1
7878

79-
client = RecombeeClient('--my-database-id--', '--db-private-token--')
79+
client = RecombeeClient('--my-database-id--', '--db-private-token--', {:region => 'ap-se'})
8080
client.send(ResetDatabase.new) # Clear everything from the database (asynchronous)
8181

8282
# We will use computers as items in this example

lib/recombee_api_client.rb

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,16 @@ class RecombeeClient
1818
include HTTParty
1919

2020
BATCH_MAX_SIZE = 10000
21-
USER_AGENT = {'User-Agent' => 'recombee-ruby-api-client/3.2.0'}
21+
USER_AGENT = {'User-Agent' => 'recombee-ruby-api-client/4.0.0'}
2222

2323
##
2424
# - +account+ -> Name of your account at Recombee
2525
# - +token+ -> Secret token obtained from Recombee for signing requests
26-
# - +protocol+ -> Default protocol for sending requests. Possible values: 'http', 'https'.
27-
def initialize(account, token, protocol = 'https', options = {})
26+
def initialize(account, token, options = {})
2827
@account = account
2928
@token = token
30-
@protocol = protocol
31-
@base_uri = ENV['RAPI_URI'] if ENV.key? 'RAPI_URI'
32-
@base_uri||= options[:base_uri]
33-
@base_uri||= 'rapi.recombee.com'
29+
@protocol = options[:protocol] || 'https'
30+
@base_uri = get_base_uri(options)
3431
end
3532

3633
##
@@ -42,7 +39,7 @@ def send(request)
4239
timeout = request.timeout / 1000
4340
uri = process_request_uri(request)
4441
uri = sign_url(uri)
45-
protocol = request.ensure_https ? 'https' : @protocol
42+
protocol = request.ensure_https ? 'https' : @protocol.to_s
4643
uri = protocol + '://' + @base_uri + uri
4744
# puts uri
4845
begin
@@ -63,10 +60,33 @@ def send(request)
6360

6461
private
6562

63+
def get_regional_base_uri(region)
64+
uri = {
65+
'ap-se' => 'rapi-ap-se.recombee.com',
66+
'ca-east' => 'rapi-ca-east.recombee.com',
67+
'eu-west' => 'rapi-eu-west.recombee.com',
68+
'us-west' => 'rapi-us-west.recombee.com'
69+
}[region.to_s.gsub('_', '-').downcase]
70+
raise ArgumentError.new("Region \"#{region}\" is unknown. You may need to update the version of the SDK.") if uri == nil
71+
uri
72+
end
73+
74+
def get_base_uri(options)
75+
base_uri = ENV['RAPI_URI']
76+
base_uri ||= options[:base_uri]
77+
78+
if options.key? :region
79+
raise ArgumentError.new(':base_uri and :region cannot be specified at the same time') if base_uri
80+
base_uri = get_regional_base_uri(options[:region])
81+
end
82+
base_uri||= 'rapi.recombee.com'
83+
base_uri
84+
end
85+
6686
def put(request, uri, timeout)
6787
response = self.class.put(uri, body: request.body_parameters.to_json,
68-
headers: { 'Content-Type' => 'application/json' }.merge(USER_AGENT),
69-
timeout: timeout)
88+
headers: { 'Content-Type' => 'application/json' }.merge(USER_AGENT),
89+
timeout: timeout)
7090
check_errors(response, request)
7191
response.body
7292
end
@@ -78,10 +98,9 @@ def get(request, uri, timeout)
7898
end
7999

80100
def post(request, uri, timeout)
81-
# pass arguments in body
82101
response = self.class.post(uri, body: request.body_parameters.to_json,
83-
headers: { 'Content-Type' => 'application/json' }.merge(USER_AGENT),
84-
timeout: timeout)
102+
headers: { 'Content-Type' => 'application/json' }.merge(USER_AGENT),
103+
timeout: timeout)
85104
check_errors(response, request)
86105
begin
87106
return JSON.parse(response.body)
@@ -91,9 +110,15 @@ def post(request, uri, timeout)
91110
end
92111

93112
def delete(request, uri, timeout)
94-
response = self.class.delete(uri, timeout: timeout, headers: USER_AGENT)
113+
response = self.class.delete(uri, body: request.body_parameters.to_json,
114+
headers: { 'Content-Type' => 'application/json' }.merge(USER_AGENT),
115+
timeout: timeout)
95116
check_errors(response, request)
96-
response.body
117+
begin
118+
return JSON.parse(response.body)
119+
rescue JSON::ParserError
120+
return response.body
121+
end
97122
end
98123

99124
def check_errors(response, request)

lib/recombee_api_client/api/delete_item.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module RecombeeApiClient
1111
#
1212
#If there are any *purchases*, *ratings*, *bookmarks*, *cart additions* or *detail views* of the item present in the database, they will be deleted in cascade as well. Also, if the item is present in some *series*, it will be removed from all the *series* where present.
1313
#
14-
#If an item becomes obsolete/no longer available, it is often meaningful to keep it in the catalog (along with all the interaction data, which are very useful), and only exclude the item from recommendations. In such a case, use [ReQL filter](https://docs.recombee.com/reql.html) instead of deleting the item completely.
14+
#If an item becomes obsolete/no longer available, it is meaningful to keep it in the catalog (along with all the interaction data, which are very useful), and **only exclude the item from recommendations**. In such a case, use [ReQL filter](https://docs.recombee.com/reql.html) instead of deleting the item completely.
1515
#
1616
class DeleteItem < ApiRequest
1717
attr_reader :item_id
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#
2+
# This file is auto-generated, do not edit
3+
#
4+
5+
module RecombeeApiClient
6+
require_relative 'request'
7+
require_relative '../errors'
8+
9+
##
10+
#Delete all the items that pass the filter.
11+
#
12+
#If an item becomes obsolete/no longer available, it is meaningful to **keep it in the catalog** (along with all the interaction data, which are very useful), and **only exclude the item from recommendations**. In such a case, use [ReQL filter](https://docs.recombee.com/reql.html) instead of deleting the item completely.
13+
class DeleteMoreItems < ApiRequest
14+
attr_reader :filter
15+
attr_accessor :timeout
16+
attr_accessor :ensure_https
17+
18+
##
19+
# * *Required arguments*
20+
# - +filter+ -> A [ReQL](https://docs.recombee.com/reql.html) expression, which return `true` for the items that shall be updated.
21+
#
22+
def initialize(filter)
23+
@filter = filter
24+
@timeout = 1000
25+
@ensure_https = false
26+
end
27+
28+
# HTTP method
29+
def method
30+
:delete
31+
end
32+
33+
# Values of body parameters as a Hash
34+
def body_parameters
35+
p = Hash.new
36+
p['filter'] = @filter
37+
p
38+
end
39+
40+
# Values of query parameters as a Hash.
41+
# name of parameter => value of the parameter
42+
def query_parameters
43+
params = {}
44+
params
45+
end
46+
47+
# Relative path to the endpoint
48+
def path
49+
"/{databaseId}/more-items/"
50+
end
51+
end
52+
end

lib/recombee_api_client/api/delete_user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class DeleteUser < ApiRequest
1818

1919
##
2020
# * *Required arguments*
21-
# - +user_id+ -> ID of the user to be added.
21+
# - +user_id+ -> ID of the user to be deleted.
2222
#
2323
def initialize(user_id)
2424
@user_id = user_id

lib/recombee_api_client/api/item_based_recommendation.rb

Lines changed: 0 additions & 159 deletions
This file was deleted.

lib/recombee_api_client/api/recommend_items_to_user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class RecommendItemsToUser < ApiRequest
117117
#
118118
# - +minRelevance+ -> **Expert option** Specifies the threshold of how much relevant must the recommended items be to the user. Possible values one of: "low", "medium", "high". The default value is "low", meaning that the system attempts to recommend number of items equal to *count* at any cost. If there are not enough data (such as interactions or item properties), this may even lead to bestseller-based recommendations to be appended to reach the full *count*. This behavior may be suppressed by using "medium" or "high" values. In such case, the system only recommends items of at least the requested relevance, and may return less than *count* items when there is not enough data to fulfill it.
119119
#
120-
# - +rotationRate+ -> **Expert option** 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. Default: `0.1`.
120+
# - +rotationRate+ -> **Expert option** 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. Default: `0`.
121121
#
122122
# - +rotationTime+ -> **Expert option** 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. Default: `7200.0`.
123123
#

0 commit comments

Comments
 (0)