predictionio 0.8.2.pre → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dd30688d83aedd5e4f1d2f6f7bd52daf0a3294dc
4
- data.tar.gz: 6b59b2ab5ab48e4874e15ea8bfaab49fc1dcadda
3
+ metadata.gz: ae1559308d2d39ef840543d0314af25680bb8af8
4
+ data.tar.gz: 26a21e3e749272d571e2a3ea537ed5df14497268
5
5
  SHA512:
6
- metadata.gz: 933590624c4c612dc5192eb4c3ed458edc3d5a557067a78ecff9b7c5a72d8036d6cd9eb1fd1b3a9bfe29f312d4338b054ea1a86c64b983f6a71aaaf54e882b87
7
- data.tar.gz: 8547a8cb09a0eeeac158523a9c86d7a8949da7beabe30d428cf454de99bcef03240110aac5b61d37a5e5b3f35362523593c749c48d85d47fb819251fa04bdb35
6
+ metadata.gz: 84f015137fc1abd14dbec10a956e8db25f6fbeeff2fe8642919ced1f689f07b97c25b0789c68defced6497951ef79e8faea6a642b377703d701112d82642478e
7
+ data.tar.gz: dafeed5306ec6b5fdba79175762ca47a4b6bd175bc5b87ac8386b8e9d361f6a32a0cadf24ab4e441c45bb2262388135e9b06deb43951f4bd4e283870b3ddd344
data/lib/predictionio.rb CHANGED
@@ -1,4 +1,3 @@
1
- require 'predictionio/client'
2
1
  require 'predictionio/event_client'
3
2
  require 'predictionio/engine_client'
4
3
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: predictionio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2.pre
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - The PredictionIO Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-14 00:00:00.000000000 Z
11
+ date: 2014-11-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  PredictionIO is a prediction server for building smart applications. This gem
@@ -22,7 +22,6 @@ files:
22
22
  - lib/predictionio.rb
23
23
  - lib/predictionio/async_request.rb
24
24
  - lib/predictionio/async_response.rb
25
- - lib/predictionio/client.rb
26
25
  - lib/predictionio/connection.rb
27
26
  - lib/predictionio/engine_client.rb
28
27
  - lib/predictionio/event_client.rb
@@ -41,9 +40,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
41
40
  version: '1.9'
42
41
  required_rubygems_version: !ruby/object:Gem::Requirement
43
42
  requirements:
44
- - - ">"
43
+ - - ">="
45
44
  - !ruby/object:Gem::Version
46
- version: 1.3.1
45
+ version: '0'
47
46
  requirements: []
48
47
  rubyforge_project:
49
48
  rubygems_version: 2.2.2
@@ -1,703 +0,0 @@
1
- # Ruby SDK for convenient access of PredictionIO Output API.
2
- #
3
- # Author:: TappingStone (help@tappingstone.com)
4
- # Copyright:: Copyright (c) 2013 TappingStone
5
- # License:: Apache License, Version 2.0
6
-
7
- require 'date'
8
- require 'json'
9
- require 'net/http'
10
- require 'predictionio/async_request'
11
- require 'predictionio/async_response'
12
- require 'predictionio/connection'
13
-
14
- module PredictionIO
15
- # This class contains methods that access PredictionIO via REST requests.
16
- #
17
- # Many REST request methods support optional arguments.
18
- # They can be supplied to these methods as Hash'es.
19
- # For a complete reference, please visit http://prediction.io.
20
- #
21
- # == High-performance Asynchronous Backend
22
- #
23
- # All REST request methods come in both synchronous and asynchronous flavors.
24
- # Both flavors accept the same set of arguments.
25
- # In addition, all synchronous request methods can instead accept a PredictionIO::AsyncResponse object generated from asynchronous request methods as its first argument.
26
- # In this case, the method will block until a response is received from it.
27
- #
28
- # Any network reconnection and request retry is automatically handled in the background.
29
- # Exceptions will be thrown after a request times out to avoid infinite blocking.
30
- #
31
- # == Special Handling of Some Optional Arguments
32
- # Some optional arguments have additional special handling:
33
- # - For all requests that accept "itypes" as input, the value can be supplied as either an Array of String's, or a comma-delimited String.
34
- # - For all requests that accept "pio_latlng" as input, they will also accept "pio_latitude" and "pio_longitude".
35
- # When these are supplied, they will override any existing "pio_latlng" value.
36
- # - All time arguments (e.g. t, pio_startT, pio_endT, etc) can be supplied as either a Time or Float object.
37
- # When supplied as a Float, the SDK will interpret it as a UNIX UTC timestamp in seconds.
38
- # The SDK will automatically round to the nearest millisecond, e.g. 3.14159 => 3.142.
39
- #
40
- # == Installation
41
- # The easiest way is to use RubyGems:
42
- # gem install predictionio
43
- #
44
- # == Synopsis
45
- # The recommended usage of the SDK is to fire asynchronous requests as early as you can in your code,
46
- # and check results later when you need them.
47
- #
48
- # === Instantiate PredictionIO Client
49
- # # Include the PredictionIO SDK
50
- # require "predictionio"
51
- #
52
- # client = PredictionIO::Client.new(<appkey>)
53
- #
54
- # === Import a User Record from Your App (with asynchronous/non-blocking requests)
55
- #
56
- # #
57
- # # (your user registration logic)
58
- # #
59
- #
60
- # uid = get_user_from_your_db()
61
- #
62
- # # PredictionIO call to create user
63
- # response = client.acreate_user(uid)
64
- #
65
- # #
66
- # # (other work to do for the rest of the page)
67
- # #
68
- #
69
- # begin
70
- # # PredictionIO call to retrieve results from an asynchronous response
71
- # result = client.create_user(response)
72
- # rescue UserNotCreatedError => e
73
- # log_and_email_error(...)
74
- # end
75
- #
76
- # === Import a User Action (Rate) from Your App (with synchronous/blocking requests)
77
- # # PredictionIO call to record the view action
78
- # begin
79
- # client.identify("foouser")
80
- # result = client.record_action_on_item("rate", "baritem", "pio_rate" => 4)
81
- # rescue U2IActionNotCreatedError => e
82
- # ...
83
- # end
84
- #
85
- # === Retrieving Top N Recommendations for a User
86
- # # PredictionIO call to get recommendations
87
- # client.identify("foouser")
88
- # response = client.aget_itemrec_top_n("barengine", 10)
89
- #
90
- # #
91
- # # work you need to do for the page (rendering, db queries, etc)
92
- # #
93
- #
94
- # begin
95
- # result = client.get_itemrec_top_n(response)
96
- # # display results, store results, or your other work...
97
- # rescue ItemRecNotFoundError => e
98
- # # graceful error handling
99
- # end
100
- #
101
- # === Retrieving Top N Similar Items for an Item
102
- # # PredictionIO call to get similar items
103
- # response = client.aget_itemsim_top_n("barengine", "fooitem", 10)
104
- #
105
- # #
106
- # # work you need to do for the page (rendering, db queries, etc)
107
- # #
108
- #
109
- # begin
110
- # result = client.get_itemsim_top_n(response)
111
- # # display results, store results, or your other work...
112
- # rescue ItemSimNotFoundError => e
113
- # # graceful error handling
114
- # end
115
-
116
- class Client
117
-
118
- # Appkey can be changed on-the-fly after creation of the client.
119
- attr_accessor :appkey
120
-
121
- # Only JSON is currently supported as API response format.
122
- attr_accessor :apiformat
123
-
124
- # The UID used for recording user-to-item actions and retrieving recommendations.
125
- attr_accessor :apiuid
126
-
127
- # Raised when a user is not created after a synchronous API call.
128
- class UserNotCreatedError < StandardError; end
129
-
130
- # Raised when a user is not found after a synchronous API call.
131
- class UserNotFoundError < StandardError; end
132
-
133
- # Raised when a user is not deleted after a synchronous API call.
134
- class UserNotDeletedError < StandardError; end
135
-
136
- # Raised when an item is not created after a synchronous API call.
137
- class ItemNotCreatedError < StandardError; end
138
-
139
- # Raised when an item is not found after a synchronous API call.
140
- class ItemNotFoundError < StandardError; end
141
-
142
- # Raised when an item is not deleted after a synchronous API call.
143
- class ItemNotDeletedError < StandardError; end
144
-
145
- # Raised when ItemRec results cannot be found for a user after a synchronous API call.
146
- class ItemRecNotFoundError < StandardError; end
147
-
148
- # Raised when ItemRank results cannot be found for a user after a synchronous API call.
149
- class ItemRankNotFoundError < StandardError; end
150
-
151
- # Raised when ItemSim results cannot be found for an item after a synchronous API call.
152
- class ItemSimNotFoundError < StandardError; end
153
-
154
- # Raised when an user-to-item action is not created after a synchronous API call.
155
- class U2IActionNotCreatedError < StandardError; end
156
-
157
- # Create a new PredictionIO client with default:
158
- # - 10 concurrent HTTP(S) connections (threads)
159
- # - API entry point at http://localhost:8000 (apiurl)
160
- # - a 60-second timeout for each HTTP(S) connection (thread_timeout)
161
- def initialize(appkey, threads = 10, apiurl = "http://localhost:8000", thread_timeout = 60)
162
- @appkey = appkey
163
- @apiformat = "json"
164
- @http = PredictionIO::Connection.new(URI(apiurl), threads, thread_timeout)
165
- end
166
-
167
- # Returns the number of pending requests within the current client.
168
- def pending_requests
169
- @http.packages.size
170
- end
171
-
172
- # Returns PredictionIO's status in string.
173
- def get_status
174
- status = @http.aget(PredictionIO::AsyncRequest.new("/")).get()
175
- begin
176
- status.body
177
- rescue Exception
178
- status
179
- end
180
- end
181
-
182
- # :category: Asynchronous Methods
183
- # Asynchronously request to create a user and return a PredictionIO::AsyncResponse object immediately.
184
- #
185
- # Corresponding REST API method: POST /users
186
- #
187
- # See also #create_user.
188
- def acreate_user(uid, params = {})
189
- rparams = params
190
- rparams["pio_appkey"] = @appkey
191
- rparams["pio_uid"] = uid
192
- if params["pio_latitude"] && params["pio_longitude"]
193
- rparams["pio_latlng"] = "#{params["pio_latitude"]},#{params["pio_longitude"]}"
194
- end
195
-
196
- @http.apost(PredictionIO::AsyncRequest.new("/users.#{@apiformat}", rparams))
197
- end
198
-
199
- # :category: Synchronous Methods
200
- # Synchronously request to create a user and block until a response is received.
201
- #
202
- # See also #acreate_user.
203
- #
204
- # call-seq:
205
- # create_user(uid, params = {})
206
- # create_user(async_response)
207
- def create_user(*args)
208
- uid_or_res = args[0]
209
- if uid_or_res.is_a?(PredictionIO::AsyncResponse)
210
- response = uid_or_res.get
211
- else
212
- uid = uid_or_res
213
- response = acreate_user(*args).get
214
- end
215
- unless response.is_a?(Net::HTTPCreated)
216
- begin
217
- msg = response.body
218
- rescue Exception
219
- raise UserNotCreatedError, response
220
- end
221
- raise UserNotCreatedError, msg
222
- end
223
- end
224
-
225
- # :category: Asynchronous Methods
226
- # Asynchronously request to get a user and return a PredictionIO::AsyncResponse object immediately.
227
- #
228
- # Creation time of the user will be returned as a Time object.
229
- #
230
- # If the result contains a latlng key, both latitude and longitude will also be available as separate keys.
231
- #
232
- # Corresponding REST API method: GET /users/:uid
233
- #
234
- # See also #get_user.
235
- def aget_user(uid)
236
- @http.aget(PredictionIO::AsyncRequest.new("/users/#{uid}.#{@apiformat}",
237
- "pio_appkey" => @appkey,
238
- "pio_uid" => uid))
239
- end
240
-
241
- # :category: Synchronous Methods
242
- # Synchronously request to get a user and block until a response is received.
243
- #
244
- # Creation time of the user will be returned as a Time object.
245
- #
246
- # If the result contains a latlng key, both latitude and longitude will also be available as separate keys.
247
- #
248
- # See also #aget_user.
249
- #
250
- # call-seq:
251
- # get_user(uid)
252
- # get_user(async_response)
253
- def get_user(uid_or_res)
254
- if uid_or_res.is_a?(PredictionIO::AsyncResponse)
255
- response = uid_or_res.get
256
- else
257
- response = aget_user(uid_or_res).get
258
- end
259
- if response.is_a?(Net::HTTPOK)
260
- res = JSON.parse(response.body)
261
- if res["pio_latlng"]
262
- latlng = res["pio_latlng"]
263
- res["pio_latitude"] = latlng[0]
264
- res["pio_longitude"] = latlng[1]
265
- end
266
- res
267
- else
268
- begin
269
- msg = response.body
270
- rescue Exception
271
- raise UserNotFoundError, response
272
- end
273
- raise UserNotFoundError, msg
274
- end
275
- end
276
-
277
- # :category: Asynchronous Methods
278
- # Asynchronously request to delete a user and return a PredictionIO::AsyncResponse object immediately.
279
- #
280
- # Corresponding REST API method: DELETE /users/:uid
281
- #
282
- # See also #delete_user.
283
- def adelete_user(uid)
284
- @http.adelete(PredictionIO::AsyncRequest.new("/users/#{uid}.#{@apiformat}",
285
- "pio_appkey" => @appkey,
286
- "pio_uid" => uid))
287
- end
288
-
289
- # :category: Synchronous Methods
290
- # Synchronously request to delete a user and block until a response is received.
291
- #
292
- # See also #adelete_user.
293
- #
294
- # call-seq:
295
- # delete_user(uid)
296
- # delete_user(async_response)
297
- def delete_user(uid_or_res)
298
- if uid_or_res.is_a?(PredictionIO::AsyncResponse)
299
- response = uid_or_res.get
300
- else
301
- response = adelete_user(uid_or_res).get
302
- end
303
- unless response.is_a?(Net::HTTPOK)
304
- begin
305
- msg = response.body
306
- rescue Exception
307
- raise UserNotDeletedError, response
308
- end
309
- raise msg
310
- end
311
- end
312
-
313
- # :category: Asynchronous Methods
314
- # Asynchronously request to create an item and return a PredictionIO::AsyncResponse object immediately.
315
- #
316
- # Corresponding REST API method: POST /items
317
- #
318
- # See also #create_item.
319
- def acreate_item(iid, itypes, params = {})
320
- rparams = params
321
- rparams["pio_appkey"] = @appkey
322
- rparams["pio_iid"] = iid
323
- begin
324
- rparams["pio_itypes"] = itypes.join(",")
325
- rescue Exception
326
- rparams["pio_itypes"] = itypes
327
- end
328
- if params["pio_latitude"] && params["pio_longitude"]
329
- rparams["pio_latlng"] = "#{params["pio_latitude"]},#{params["pio_longitude"]}"
330
- end
331
- rparams["pio_startT"] = ((params["pio_startT"].to_r) * 1000).round(0).to_s if params["pio_startT"]
332
- rparams["pio_endT"] = ((params["pio_endT"].to_r) * 1000).round(0).to_s if params["pio_endT"]
333
-
334
- @http.apost(PredictionIO::AsyncRequest.new("/items.#{@apiformat}", rparams))
335
- end
336
-
337
- # :category: Synchronous Methods
338
- # Synchronously request to create an item and block until a response is received.
339
- #
340
- # See #acreate_item for a description of other accepted arguments.
341
- #
342
- # call-seq:
343
- # create_item(iid, itypes, params = {})
344
- # create_item(async_response)
345
- def create_item(*args)
346
- iid_or_res = args[0]
347
- if iid_or_res.is_a?(PredictionIO::AsyncResponse)
348
- response = iid_or_res.get
349
- else
350
- response = acreate_item(*args).get
351
- end
352
- unless response.is_a?(Net::HTTPCreated)
353
- begin
354
- msg = response.body
355
- rescue Exception
356
- raise ItemNotCreatedError, response
357
- end
358
- raise ItemNotCreatedError, msg
359
- end
360
- end
361
-
362
- # :category: Asynchronous Methods
363
- # Asynchronously request to get an item and return a PredictionIO::AsyncResponse object immediately.
364
- #
365
- # Creation time of the user will be returned as a Time object.
366
- #
367
- # If the result contains a latlng key, both latitude and longitude will also be available as separate keys.
368
- #
369
- # Corresponding REST API method: GET /items/:iid
370
- #
371
- # See also #get_item.
372
- def aget_item(iid)
373
- @http.aget(PredictionIO::AsyncRequest.new("/items/#{iid}.#{@apiformat}",
374
- "pio_appkey" => @appkey,
375
- "pio_iid" => iid))
376
- end
377
-
378
- # :category: Synchronous Methods
379
- # Synchronously request to get an item and block until a response is received.
380
- #
381
- # Creation time of the item will be returned as a Time object.
382
- #
383
- # If the result contains a latlng key, both latitude and longitude will also be available as separate keys.
384
- #
385
- # See also #aget_item.
386
- #
387
- # call-seq:
388
- # get_item(iid)
389
- # get_item(async_response)
390
- def get_item(iid_or_res)
391
- if iid_or_res.is_a?(PredictionIO::AsyncResponse)
392
- response = iid_or_res.get
393
- else
394
- response = aget_item(iid_or_res).get
395
- end
396
- if response.is_a?(Net::HTTPOK)
397
- res = JSON.parse(response.body)
398
- if res["pio_latlng"]
399
- latlng = res["pio_latlng"]
400
- res["pio_latitude"] = latlng[0]
401
- res["pio_longitude"] = latlng[1]
402
- end
403
- if res["pio_startT"]
404
- startT = Rational(res["pio_startT"], 1000)
405
- res["pio_startT"] = Time.at(startT)
406
- end
407
- if res["pio_endT"]
408
- endT = Rational(res["pio_endT"], 1000)
409
- res["pio_endT"] = Time.at(endT)
410
- end
411
- res
412
- else
413
- begin
414
- msg = response.body
415
- rescue Exception
416
- raise ItemNotFoundError, response
417
- end
418
- raise ItemNotFoundError, msg
419
- end
420
- end
421
-
422
- # :category: Asynchronous Methods
423
- # Asynchronously request to delete an item and return a PredictionIO::AsyncResponse object immediately.
424
- #
425
- # Corresponding REST API method: DELETE /items/:iid
426
- #
427
- # See also #delete_item.
428
- def adelete_item(iid)
429
- @http.adelete(PredictionIO::AsyncRequest.new("/items/#{iid}.#{@apiformat}",
430
- "pio_appkey" => @appkey,
431
- "pio_iid" => iid))
432
- end
433
-
434
- # :category: Synchronous Methods
435
- # Synchronously request to delete an item and block until a response is received.
436
- #
437
- # See also #adelete_item.
438
- #
439
- # call-seq:
440
- # delete_item(iid)
441
- # delete_item(async_response)
442
- def delete_item(iid_or_res)
443
- if iid_or_res.is_a?(PredictionIO::AsyncResponse)
444
- response = iid_or_res.get
445
- else
446
- response = adelete_item(iid_or_res).get
447
- end
448
- unless response.is_a?(Net::HTTPOK)
449
- begin
450
- msg = response.body
451
- rescue Exception
452
- raise ItemNotDeletedError, response
453
- end
454
- raise ItemNotDeletedError, msg
455
- end
456
- end
457
-
458
- # Set the user ID for use in all subsequent user-to-item action recording and user recommendation retrieval.
459
- def identify(uid)
460
- @apiuid = uid
461
- end
462
-
463
- # :category: Asynchronous Methods
464
- # Asynchronously request to get the top n recommendations for a user from an ItemRec engine and return a PredictionIO::AsyncResponse object immediately.
465
- #
466
- # Corresponding REST API method: GET /engines/itemrec/:engine/topn
467
- #
468
- # See also #get_itemrec_top_n.
469
- def aget_itemrec_top_n(engine, n, params = {})
470
- rparams = Hash.new
471
- rparams["pio_appkey"] = @appkey
472
- rparams["pio_uid"] = @apiuid
473
- rparams["pio_n"] = n
474
- if params["pio_itypes"]
475
- if params["pio_itypes"].kind_of?(Array) && params["pio_itypes"].any?
476
- rparams["pio_itypes"] = params["pio_itypes"].join(",")
477
- else
478
- rparams["pio_itypes"] = params["pio_itypes"]
479
- end
480
- end
481
- if params["pio_latitude"] && params["pio_longitude"]
482
- rparams["pio_latlng"] = "#{params["pio_latitude"]},#{params["pio_longitude"]}"
483
- end
484
- rparams["pio_within"] = params["pio_within"] if params["pio_within"]
485
- rparams["pio_unit"] = params["pio_unit"] if params["pio_unit"]
486
- if params["pio_attributes"]
487
- if params["pio_attributes"].kind_of?(Array) && params["pio_attributes"].any?
488
- rparams["pio_attributes"] = params["pio_attributes"].join(",")
489
- else
490
- rparams["pio_attributes"] = params["pio_attributes"]
491
- end
492
- end
493
- @http.aget(PredictionIO::AsyncRequest.new("/engines/itemrec/#{engine}/topn.#{@apiformat}", rparams))
494
- end
495
-
496
- # :category: Synchronous Methods
497
- # Synchronously request to get the top n recommendations for a user from an ItemRec engine and block until a response is received.
498
- #
499
- # See #aget_itemrec_top_n for a description of special argument handling.
500
- #
501
- # call-seq:
502
- # get_itemrec_top_n(engine, n, params = {})
503
- # get_itemrec_top_n(async_response)
504
- def get_itemrec_top_n(*args)
505
- uid_or_res = args[0]
506
- if uid_or_res.is_a?(PredictionIO::AsyncResponse)
507
- response = uid_or_res
508
- else
509
- response = aget_itemrec_top_n(*args)
510
- end
511
- http_response = response.get
512
- if http_response.is_a?(Net::HTTPOK)
513
- res = JSON.parse(http_response.body)
514
- if response.request.params.has_key?('pio_attributes')
515
- attributes = response.request.params['pio_attributes'].split(',')
516
- list_of_attribute_values = attributes.map { |attrib| res[attrib] }
517
- res["pio_iids"].zip(*list_of_attribute_values).map { |v| Hash[(['pio_iid'] + attributes).zip(v)] }
518
- else
519
- res["pio_iids"]
520
- end
521
- else
522
- begin
523
- msg = response.body
524
- rescue Exception
525
- raise ItemRecNotFoundError, response
526
- end
527
- raise ItemRecNotFoundError, msg
528
- end
529
- end
530
-
531
- # :category: Asynchronous Methods
532
- # Asynchronously request to get the ranking for a user from an ItemRank engine and return a PredictionIO::AsyncResponse object immediately.
533
- #
534
- # Corresponding REST API method: GET /engines/itemrank/:engine/ranked
535
- #
536
- # See also #get_itemrank_ranked.
537
- def aget_itemrank_ranked(engine, iids, params = {})
538
- rparams = Hash.new
539
- rparams["pio_appkey"] = @appkey
540
- rparams["pio_uid"] = @apiuid
541
- if iids.kind_of?(Array) && iids.any?
542
- rparams["pio_iids"] = iids.join(",")
543
- else
544
- rparams["pio_iids"] = iids
545
- end
546
- if params["pio_attributes"]
547
- if params["pio_attributes"].kind_of?(Array) && params["pio_attributes"].any?
548
- rparams["pio_attributes"] = params["pio_attributes"].join(",")
549
- else
550
- rparams["pio_attributes"] = params["pio_attributes"]
551
- end
552
- end
553
- @http.aget(PredictionIO::AsyncRequest.new("/engines/itemrank/#{engine}/ranked.#{@apiformat}", rparams))
554
- end
555
-
556
- # :category: Synchronous Methods
557
- # Synchronously request to get the ranking for a user from an ItemRank engine and block until a response is received.
558
- #
559
- # See #aget_itemrank_ranked for a description of special argument handling.
560
- #
561
- # call-seq:
562
- # get_itemrank_ranked(engine, n, params = {})
563
- # get_itemrank_ranked(async_response)
564
- def get_itemrank_ranked(*args)
565
- uid_or_res = args[0]
566
- if uid_or_res.is_a?(PredictionIO::AsyncResponse)
567
- response = uid_or_res
568
- else
569
- response = aget_itemrank_ranked(*args)
570
- end
571
- http_response = response.get
572
- if http_response.is_a?(Net::HTTPOK)
573
- res = JSON.parse(http_response.body)
574
- if response.request.params.has_key?('pio_attributes')
575
- attributes = response.request.params['pio_attributes'].split(',')
576
- list_of_attribute_values = attributes.map { |attrib| res[attrib] }
577
- res["pio_iids"].zip(*list_of_attribute_values).map { |v| Hash[(['pio_iid'] + attributes).zip(v)] }
578
- else
579
- res["pio_iids"]
580
- end
581
- else
582
- begin
583
- msg = response.body
584
- rescue Exception
585
- raise ItemRankNotFoundError, response
586
- end
587
- raise ItemRankNotFoundError, msg
588
- end
589
- end
590
-
591
- # :category: Asynchronous Methods
592
- # Asynchronously request to get the top n similar items for an item from an ItemSim engine and return a PredictionIO::AsyncResponse object immediately.
593
- #
594
- # Corresponding REST API method: GET /engines/itemsim/:engine/topn
595
- #
596
- # See also #get_itemsim_top_n.
597
- def aget_itemsim_top_n(engine, iid, n, params = {})
598
- rparams = Hash.new
599
- rparams["pio_appkey"] = @appkey
600
- rparams["pio_iid"] = iid
601
- rparams["pio_n"] = n
602
- if params["pio_itypes"]
603
- if params["pio_itypes"].kind_of?(Array) && params["pio_itypes"].any?
604
- rparams["pio_itypes"] = params["pio_itypes"].join(",")
605
- else
606
- rparams["pio_itypes"] = params["pio_itypes"]
607
- end
608
- end
609
- if params["pio_latitude"] && params["pio_longitude"]
610
- rparams["pio_latlng"] = "#{params["pio_latitude"]},#{params["pio_longitude"]}"
611
- end
612
- rparams["pio_within"] = params["pio_within"] if params["pio_within"]
613
- rparams["pio_unit"] = params["pio_unit"] if params["pio_unit"]
614
- if params["pio_attributes"]
615
- if params["pio_attributes"].kind_of?(Array) && params["pio_attributes"].any?
616
- rparams["pio_attributes"] = params["pio_attributes"].join(",")
617
- else
618
- rparams["pio_attributes"] = params["pio_attributes"]
619
- end
620
- end
621
- @http.aget(PredictionIO::AsyncRequest.new("/engines/itemsim/#{engine}/topn.#{@apiformat}", rparams))
622
- end
623
-
624
- # :category: Synchronous Methods
625
- # Synchronously request to get the top n similar items for an item from an ItemSim engine and block until a response is received.
626
- #
627
- # See #aget_itemsim_top_n for a description of special argument handling.
628
- #
629
- # call-seq:
630
- # get_itemsim_top_n(engine, iid, n, params = {})
631
- # get_itemsim_top_n(async_response)
632
- def get_itemsim_top_n(*args)
633
- uid_or_res = args[0]
634
- if uid_or_res.is_a?(PredictionIO::AsyncResponse)
635
- response = uid_or_res
636
- else
637
- response = aget_itemsim_top_n(*args)
638
- end
639
- http_response = response.get
640
- if http_response.is_a?(Net::HTTPOK)
641
- res = JSON.parse(http_response.body)
642
- if response.request.params.has_key?('pio_attributes')
643
- attributes = response.request.params['pio_attributes'].split(',')
644
- list_of_attribute_values = attributes.map { |attrib| res[attrib] }
645
- res["pio_iids"].zip(*list_of_attribute_values).map { |v| Hash[(['pio_iid'] + attributes).zip(v)] }
646
- else
647
- res["pio_iids"]
648
- end
649
- else
650
- begin
651
- msg = response.body
652
- rescue Exception
653
- raise ItemSimNotFoundError, response
654
- end
655
- raise ItemSimNotFoundError, msg
656
- end
657
- end
658
-
659
- # :category: Asynchronous Methods
660
- # Asynchronously request to record an action on an item and return a PredictionIO::AsyncResponse object immediately.
661
- #
662
- # Corresponding REST API method: POST /actions/u2i
663
- #
664
- # See also #record_action_on_item.
665
- def arecord_action_on_item(action, iid, params = {})
666
- rparams = params
667
- rparams["pio_appkey"] = @appkey
668
- rparams["pio_action"] = action
669
- rparams["pio_uid"] = @apiuid
670
- rparams["pio_iid"] = iid
671
- rparams["pio_t"] = ((params["pio_t"].to_r) * 1000).round(0).to_s if params["pio_t"]
672
- if params["pio_latitude"] && params["pio_longitude"]
673
- rparams["pio_latlng"] = "#{params["pio_latitude"]},#{params["pio_longitude"]}"
674
- end
675
- @http.apost(PredictionIO::AsyncRequest.new("/actions/u2i.#{@apiformat}", rparams))
676
- end
677
-
678
- # :category: Synchronous Methods
679
- # Synchronously request to record an action on an item and block until a response is received.
680
- #
681
- # See also #arecord_action_on_item.
682
- #
683
- # call-seq:
684
- # record_action_on_item(action, iid, params = {})
685
- # record_action_on_item(async_response)
686
- def record_action_on_item(*args)
687
- action_or_res = args[0]
688
- if action_or_res.is_a?(PredictionIO::AsyncResponse)
689
- response = action_or_res.get
690
- else
691
- response = arecord_action_on_item(*args).get
692
- end
693
- unless response.is_a?(Net::HTTPCreated)
694
- begin
695
- msg = response.body
696
- rescue Exception
697
- raise U2IActionNotCreatedError, response
698
- end
699
- raise U2IActionNotCreatedError, msg
700
- end
701
- end
702
- end
703
- end