recombee_api_client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +157 -0
  5. data/Rakefile +6 -0
  6. data/lib/recombee_api_client/api/add_bookmark.rb +70 -0
  7. data/lib/recombee_api_client/api/add_cart_addition.rb +70 -0
  8. data/lib/recombee_api_client/api/add_detail_view.rb +73 -0
  9. data/lib/recombee_api_client/api/add_group.rb +53 -0
  10. data/lib/recombee_api_client/api/add_item.rb +56 -0
  11. data/lib/recombee_api_client/api/add_item_property.rb +59 -0
  12. data/lib/recombee_api_client/api/add_purchase.rb +70 -0
  13. data/lib/recombee_api_client/api/add_rating.rb +73 -0
  14. data/lib/recombee_api_client/api/add_series.rb +53 -0
  15. data/lib/recombee_api_client/api/add_user.rb +54 -0
  16. data/lib/recombee_api_client/api/batch.rb +64 -0
  17. data/lib/recombee_api_client/api/delete_bookmark.rb +61 -0
  18. data/lib/recombee_api_client/api/delete_cart_addition.rb +61 -0
  19. data/lib/recombee_api_client/api/delete_detail_view.rb +61 -0
  20. data/lib/recombee_api_client/api/delete_group.rb +56 -0
  21. data/lib/recombee_api_client/api/delete_item.rb +58 -0
  22. data/lib/recombee_api_client/api/delete_item_property.rb +54 -0
  23. data/lib/recombee_api_client/api/delete_purchase.rb +61 -0
  24. data/lib/recombee_api_client/api/delete_rating.rb +61 -0
  25. data/lib/recombee_api_client/api/delete_series.rb +56 -0
  26. data/lib/recombee_api_client/api/delete_user.rb +56 -0
  27. data/lib/recombee_api_client/api/get_item_property_info.rb +54 -0
  28. data/lib/recombee_api_client/api/get_item_values.rb +55 -0
  29. data/lib/recombee_api_client/api/insert_to_group.rb +69 -0
  30. data/lib/recombee_api_client/api/insert_to_series.rb +72 -0
  31. data/lib/recombee_api_client/api/item_based_recommendation.rb +131 -0
  32. data/lib/recombee_api_client/api/list_group_items.rb +53 -0
  33. data/lib/recombee_api_client/api/list_groups.rb +50 -0
  34. data/lib/recombee_api_client/api/list_item_bookmarks.rb +54 -0
  35. data/lib/recombee_api_client/api/list_item_cart_additions.rb +54 -0
  36. data/lib/recombee_api_client/api/list_item_detail_views.rb +54 -0
  37. data/lib/recombee_api_client/api/list_item_properties.rb +51 -0
  38. data/lib/recombee_api_client/api/list_item_purchases.rb +54 -0
  39. data/lib/recombee_api_client/api/list_item_ratings.rb +54 -0
  40. data/lib/recombee_api_client/api/list_items.rb +63 -0
  41. data/lib/recombee_api_client/api/list_series.rb +50 -0
  42. data/lib/recombee_api_client/api/list_series_items.rb +53 -0
  43. data/lib/recombee_api_client/api/list_user_bookmarks.rb +53 -0
  44. data/lib/recombee_api_client/api/list_user_cart_additions.rb +53 -0
  45. data/lib/recombee_api_client/api/list_user_detail_views.rb +53 -0
  46. data/lib/recombee_api_client/api/list_user_purchases.rb +53 -0
  47. data/lib/recombee_api_client/api/list_user_ratings.rb +53 -0
  48. data/lib/recombee_api_client/api/list_users.rb +50 -0
  49. data/lib/recombee_api_client/api/merge_users.rb +72 -0
  50. data/lib/recombee_api_client/api/remove_from_group.rb +59 -0
  51. data/lib/recombee_api_client/api/remove_from_series.rb +62 -0
  52. data/lib/recombee_api_client/api/request.rb +7 -0
  53. data/lib/recombee_api_client/api/reset_database.rb +51 -0
  54. data/lib/recombee_api_client/api/set_item_values.rb +70 -0
  55. data/lib/recombee_api_client/api/user_based_recommendation.rb +116 -0
  56. data/lib/recombee_api_client/errors.rb +44 -0
  57. data/lib/recombee_api_client/version.rb +3 -0
  58. data/lib/recombee_api_client.rb +105 -0
  59. data/recombee_api_client.gemspec +26 -0
  60. metadata +173 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 61ee828200201d0a55afa950d68aae4a68aa7b6a
4
+ data.tar.gz: d2bd52cc028a1008cd9a529c7451ffb060754dea
5
+ SHA512:
6
+ metadata.gz: 5a6a691a074c2ef381d7717b1f7da9307c997c442a60e0e1d746c4db85348ec43f1af10b722f58862a4dd30592150274735d336798722f6c3a82a09a8e13ddc6
7
+ data.tar.gz: 5df8b96686575ae9db742e46b577ebf8568072c80a747ede138696681d5386b73670c679a03bc5dc28cd649a941f3480eb66f163c302949a75084f6bf08d7565
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Ondřej Fiedler
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,157 @@
1
+ # RecombeeApiClient
2
+
3
+ A Ruby client for easy use of the [Recombee](https://www.recombee.com/) recommendation API.
4
+
5
+ Documentation of the API can be found at [docs.recombee.com](https://docs.recombee.com/).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'recombee_api_client'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install recombee_api_client
22
+
23
+ ## Examples
24
+
25
+ ### Basic example
26
+ ```ruby
27
+ require 'recombee_api_client'
28
+ include RecombeeApiClient
29
+
30
+ # Prepare some items and users
31
+ NUM = 100
32
+ my_users = (1..NUM).map { |i| "user-#{i}" }
33
+ my_items = (1..NUM).map { |i| "item-#{i}" }
34
+
35
+ #Generate some random purchases of items by users
36
+ PROBABILITY_PURCHASED = 0.1
37
+ my_purchases = []
38
+ my_users.each do |user|
39
+ p = my_items.select { |_| rand(0.0..1.0) < PROBABILITY_PURCHASED }
40
+ p.each { |item| my_purchases.push('userId' => user, 'itemId' => item) }
41
+ end
42
+
43
+ # Use Recombee recommender
44
+ client = RecombeeClient.new('client-test', 'jGGQ6ZKa8rQ1zTAyxTc0EMn55YPF7FJLUtaMLhbsGxmvwxgTwXYqmUk5xVZFw98L')
45
+ begin
46
+ # Send the data to Recombee, use Batch for faster processing
47
+ puts 'Send users'
48
+ client.send(Batch.new(my_users.map { |userId| AddUser.new(userId) }))
49
+ puts 'Send items'
50
+ client.send(Batch.new(my_items.map { |itemId| AddItem.new(itemId) }))
51
+ puts 'Send purchases'
52
+ client.send(Batch.new(my_purchases.map { |p| AddPurchase.new(p['userId'], p['itemId'], 0) }))
53
+
54
+ # Get recommendations for user 'user-25'
55
+ puts 'Recommend for a user'
56
+ recommended = client.send(UserBasedRecommendation.new('user-25', 5, 'rotationRate' => 0))
57
+ puts "Recommended items: #{recommended}"
58
+ rescue ResponseError => e
59
+ puts e
60
+ end
61
+ ```
62
+
63
+ ### Using property values
64
+ ```ruby
65
+ #!/usr/bin/env ruby
66
+
67
+ require 'recombee_api_client'
68
+ include RecombeeApiClient
69
+
70
+ NUM = 100
71
+ PROBABILITY_PURCHASED = 0.1
72
+
73
+ client = RecombeeClient.new('client-test', 'jGGQ6ZKa8rQ1zTAyxTc0EMn55YPF7FJLUtaMLhbsGxmvwxgTwXYqmUk5xVZFw98L')
74
+ client.send(ResetDatabase.new)
75
+ # We will use computers as items in this example
76
+ # Computers have three properties
77
+ # - price (floating point number)
78
+ # - number of processor cores (integer number)
79
+ # - description (string)
80
+
81
+ # Add properties of items
82
+ client.send(AddItemProperty.new('price', 'double'))
83
+ client.send(AddItemProperty.new('num-cores', 'int'))
84
+ client.send(AddItemProperty.new('description', 'string'))
85
+
86
+ # Prepare requests for setting a catalog of computers
87
+ requests = (1..NUM).map do |i|
88
+ SetItemValues.new(
89
+ "computer-#{i}", #itemId
90
+ #values:
91
+ {
92
+ 'price' => rand(15000.0 .. 25000.0),
93
+ 'num-cores' => rand(1..8),
94
+ 'description' => 'Great computer',
95
+ '!cascadeCreate' => true # Use !cascadeCreate for creating item
96
+ # with given itemId, if it doesn't exist
97
+ }
98
+ )
99
+ end
100
+
101
+ # Send catalog to the recommender system
102
+ client.send(Batch.new(requests))
103
+
104
+ # Prepare some purchases of items by users
105
+ requests = []
106
+ (1..NUM).map{|i| "computer-#{i}"}.each do |item_id|
107
+ user_ids = (1..NUM).map{|i| "user-#{i}"}
108
+ user_ids = user_ids.select { |_| rand(0.0..1.0) < PROBABILITY_PURCHASED }
109
+ # Use cascadeCreate to create unexisting users
110
+ user_ids.each { |user_id| requests.push(AddPurchase.new(user_id, item_id, 0, 'cascadeCreate' => true)) }
111
+ end
112
+
113
+ # Send purchases to the recommender system
114
+ client.send(Batch.new(requests))
115
+
116
+ # Get 5 recommendations for user-42, who is currently viewing computer-6
117
+ recommended = client.send(ItemBasedRecommendation.new('computer-6', 5, 'targetUserId' => 'user-42') )
118
+ puts "Recommended items: #{recommended}"
119
+
120
+ # Get 5 recommendations for user-42, but recommend only computers that
121
+ # have at least 3 cores
122
+ recommended = client.send(
123
+ ItemBasedRecommendation.new('computer-6', 5, {'targetUserId' => 'user-42', 'filter' => "'num-cores'>=3"})
124
+ )
125
+ puts "Recommended items with at least 3 processor cores: #{recommended}"
126
+
127
+ # Get 5 recommendations for user-42, but recommend only items that
128
+ # are more expensive then currently viewed item (up-sell)
129
+ recommended = client.send(
130
+ ItemBasedRecommendation.new('computer-6', 5,
131
+ {'targetUserId' => 'user-42', 'filter' => "'price' > context_item[\"price\"]"})
132
+ )
133
+ puts "Recommended up-sell items: #{recommended}"
134
+ ```
135
+
136
+ ### Exception handling
137
+
138
+ For the sake of brevity, the above examples omit exception handling. However, various exceptions can occur while processing request, for example because of adding an already existing item, submitting interaction of nonexistent user or because of timeout.
139
+
140
+ We are doing our best to provide the fastest and most reliable service, but production-level applications must implement a fallback solution since errors can always happen. The fallback might be, for example, showing the most popular items from the current category, or not displaying recommendations at all.
141
+
142
+ Example:
143
+ ```ruby
144
+
145
+ begin
146
+ recommended = client.send(
147
+ ItemBasedRecommendation.new('computer-6', 5,
148
+ {'targetUserId' => 'user-42', 'filter' => "'price' > context_item[\"price\"]"})
149
+ )
150
+ rescue ResponseError => e
151
+ #Handle errorneous request => use fallback
152
+ rescue ApiTimeout => e
153
+ #Handle timeout => use fallback
154
+ rescue APIError => e
155
+ #APIError is parent of both ResponseError and ApiTimeout
156
+ end
157
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,70 @@
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
+ #Adds a bookmark of a given item made by a given user.
11
+ #
12
+ class AddBookmark < ApiRequest
13
+ attr_reader :user_id, :item_id, :timestamp, :cascade_create
14
+ attr_accessor :timeout
15
+
16
+ ##
17
+ # * *Required arguments*
18
+ # - +user_id+ -> User who bookmarked the item
19
+ # - +item_id+ -> Bookmarked item
20
+ # - +timestamp+ -> Unix timestamp of the bookmark. If you don't have the timestamp value available, you may use some artificial value, such as 0. It is preferable, however, to provide the timestamp whenever possible as the user's preferences may evolve over time.
21
+ #
22
+ # * *Optional arguments (given as hash optional)*
23
+ # - +cascadeCreate+ -> Sets whether the given user/item should be created if not present in the database.
24
+ #
25
+ def initialize(user_id, item_id, timestamp, optional = {})
26
+ @user_id = user_id
27
+ @item_id = item_id
28
+ @timestamp = timestamp
29
+ @cascade_create = optional['cascadeCreate']
30
+ @optional = optional
31
+ @timeout = 1000
32
+ @optional.each do |par, _|
33
+ fail UnknownOptionalParameter.new(par) unless ["cascadeCreate"].include? par
34
+ end
35
+ end
36
+
37
+ # HTTP method
38
+ def method
39
+ :post
40
+ end
41
+
42
+ # Values of body parameters as a Hash
43
+ def body_parameters
44
+ p = Hash.new
45
+ p['userId'] = @user_id
46
+ p['itemId'] = @item_id
47
+ p['timestamp'] = @timestamp
48
+ p['cascadeCreate'] = @optional['cascadeCreate'] if @optional['cascadeCreate']
49
+ p
50
+ end
51
+
52
+ # Values of query path parameters as a Hash.
53
+ # name of parameter => value of the parameter
54
+ def query_parameters
55
+ params = {}
56
+ params
57
+ end
58
+
59
+ # Relative path to the endpoint
60
+ def basic_path
61
+ "/{databaseId}/bookmarks/"
62
+ end
63
+
64
+ # Relative path to the endpoint including query parameters
65
+ def path
66
+ p = "/{databaseId}/bookmarks/"
67
+ p
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,70 @@
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
+ #Adds a cart addition of a given item made by a given user.
11
+ #
12
+ class AddCartAddition < ApiRequest
13
+ attr_reader :user_id, :item_id, :timestamp, :cascade_create
14
+ attr_accessor :timeout
15
+
16
+ ##
17
+ # * *Required arguments*
18
+ # - +user_id+ -> User who added the item to the cart
19
+ # - +item_id+ -> Item added to the cart
20
+ # - +timestamp+ -> Unix timestamp of the cart addition. If you don't have the timestamp value available, you may use some artificial value, such as 0. It is preferable, however, to provide the timestamp whenever possible as the user's preferences may evolve over time.
21
+ #
22
+ # * *Optional arguments (given as hash optional)*
23
+ # - +cascadeCreate+ -> Sets whether the given user/item should be created if not present in the database.
24
+ #
25
+ def initialize(user_id, item_id, timestamp, optional = {})
26
+ @user_id = user_id
27
+ @item_id = item_id
28
+ @timestamp = timestamp
29
+ @cascade_create = optional['cascadeCreate']
30
+ @optional = optional
31
+ @timeout = 1000
32
+ @optional.each do |par, _|
33
+ fail UnknownOptionalParameter.new(par) unless ["cascadeCreate"].include? par
34
+ end
35
+ end
36
+
37
+ # HTTP method
38
+ def method
39
+ :post
40
+ end
41
+
42
+ # Values of body parameters as a Hash
43
+ def body_parameters
44
+ p = Hash.new
45
+ p['userId'] = @user_id
46
+ p['itemId'] = @item_id
47
+ p['timestamp'] = @timestamp
48
+ p['cascadeCreate'] = @optional['cascadeCreate'] if @optional['cascadeCreate']
49
+ p
50
+ end
51
+
52
+ # Values of query path parameters as a Hash.
53
+ # name of parameter => value of the parameter
54
+ def query_parameters
55
+ params = {}
56
+ params
57
+ end
58
+
59
+ # Relative path to the endpoint
60
+ def basic_path
61
+ "/{databaseId}/cartadditions/"
62
+ end
63
+
64
+ # Relative path to the endpoint including query parameters
65
+ def path
66
+ p = "/{databaseId}/cartadditions/"
67
+ p
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,73 @@
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
+ #Adds a detail view of a given item made by a given user.
11
+ #
12
+ class AddDetailView < ApiRequest
13
+ attr_reader :user_id, :item_id, :timestamp, :duration, :cascade_create
14
+ attr_accessor :timeout
15
+
16
+ ##
17
+ # * *Required arguments*
18
+ # - +user_id+ -> User who viewed the item
19
+ # - +item_id+ -> Viewed item
20
+ # - +timestamp+ -> Unix timestamp of the view. If you don't have the timestamp value available, you may use some artificial value, such as 0. It is preferable, however, to provide the timestamp whenever possible as the user's preferences may evolve over time.
21
+ #
22
+ # * *Optional arguments (given as hash optional)*
23
+ # - +duration+ -> Duration of the view
24
+ # - +cascadeCreate+ -> Sets whether the given user/item should be created if not present in the database.
25
+ #
26
+ def initialize(user_id, item_id, timestamp, optional = {})
27
+ @user_id = user_id
28
+ @item_id = item_id
29
+ @timestamp = timestamp
30
+ @duration = optional['duration']
31
+ @cascade_create = optional['cascadeCreate']
32
+ @optional = optional
33
+ @timeout = 1000
34
+ @optional.each do |par, _|
35
+ fail UnknownOptionalParameter.new(par) unless ["duration","cascadeCreate"].include? par
36
+ end
37
+ end
38
+
39
+ # HTTP method
40
+ def method
41
+ :post
42
+ end
43
+
44
+ # Values of body parameters as a Hash
45
+ def body_parameters
46
+ p = Hash.new
47
+ p['userId'] = @user_id
48
+ p['itemId'] = @item_id
49
+ p['timestamp'] = @timestamp
50
+ p['duration'] = @optional['duration'] if @optional['duration']
51
+ p['cascadeCreate'] = @optional['cascadeCreate'] if @optional['cascadeCreate']
52
+ p
53
+ end
54
+
55
+ # Values of query path parameters as a Hash.
56
+ # name of parameter => value of the parameter
57
+ def query_parameters
58
+ params = {}
59
+ params
60
+ end
61
+
62
+ # Relative path to the endpoint
63
+ def basic_path
64
+ "/{databaseId}/detailviews/"
65
+ end
66
+
67
+ # Relative path to the endpoint including query parameters
68
+ def path
69
+ p = "/{databaseId}/detailviews/"
70
+ p
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,53 @@
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
+ #Creates new group in the database.
11
+ class AddGroup < ApiRequest
12
+ attr_reader :group_id
13
+ attr_accessor :timeout
14
+
15
+ ##
16
+ # * *Required arguments*
17
+ # - +group_id+ -> ID of the group to be created.
18
+ #
19
+ def initialize(group_id)
20
+ @group_id = group_id
21
+ @timeout = 1000
22
+ end
23
+
24
+ # HTTP method
25
+ def method
26
+ :put
27
+ end
28
+
29
+ # Values of body parameters as a Hash
30
+ def body_parameters
31
+ p = Hash.new
32
+ p
33
+ end
34
+
35
+ # Values of query path parameters as a Hash.
36
+ # name of parameter => value of the parameter
37
+ def query_parameters
38
+ params = {}
39
+ params
40
+ end
41
+
42
+ # Relative path to the endpoint
43
+ def basic_path
44
+ "/{databaseId}/groups/#{@group_id}"
45
+ end
46
+
47
+ # Relative path to the endpoint including query parameters
48
+ def path
49
+ p = "/{databaseId}/groups/#{@group_id}"
50
+ p
51
+ end
52
+ end
53
+ end