predictionio 0.3.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/predictionio/client.rb +103 -215
  2. metadata +2 -2
@@ -37,15 +37,15 @@ module PredictionIO
37
37
  # == Special Handling of Some Optional Arguments
38
38
  # Some optional arguments have additional special handling:
39
39
  # - 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.
40
- # - For all requests that accept "latlng" as input, they will also accept "latitude" and "longitude".
41
- # When these are supplied, they will override any existing "latlng" value.
42
- # - All time arguments (e.g. t, startT, endT, etc) can be supplied as either a Time or Float object.
40
+ # - For all requests that accept "pio_latlng" as input, they will also accept "pio_latitude" and "pio_longitude".
41
+ # When these are supplied, they will override any existing "pio_latlng" value.
42
+ # - All time arguments (e.g. t, pio_startT, pio_endT, etc) can be supplied as either a Time or Float object.
43
43
  # When supplied as a Float, the SDK will interpret it as a UNIX UTC timestamp in seconds.
44
44
  # The SDK will automatically round to the nearest millisecond, e.g. 3.14159 => 3.142.
45
45
  #
46
46
  # == Installation
47
- # Download the PredictionIO Ruby Gem from http://prediction.io
48
- # gem install predictionio-0.1.0.gem
47
+ # The easiest way is to use RubyGems:
48
+ # gem install predictionio-0.5.0.gem
49
49
  #
50
50
  # == Synopsis
51
51
  # The recommended usage of the SDK is to fire asynchronous requests as early as you can in your code,
@@ -79,26 +79,28 @@ module PredictionIO
79
79
  # log_and_email_error(...)
80
80
  # end
81
81
  #
82
- # === Import a User Action (View) form Your App (with synchronous/blocking requests)
82
+ # === Import a User Action (Rate) from Your App (with synchronous/blocking requests)
83
83
  # # PredictionIO call to record the view action
84
84
  # begin
85
- # result = client.user_view_item(4, 15)
85
+ # client.identify("foouser")
86
+ # result = client.record_action_on_item("rate", "baritem", "pio_rate" => 4)
86
87
  # rescue U2IActionNotCreatedError => e
87
88
  # ...
88
89
  # end
89
90
  #
90
91
  # === Retrieving Top N Recommendations for a User
91
92
  # # PredictionIO call to get recommendations
92
- # response = client.aget_recommendation(4, 10)
93
+ # client.identify("foouser")
94
+ # response = client.aget_itemrec_top_n("barengine", 10)
93
95
  #
94
96
  # #
95
97
  # # work you need to do for the page (rendering, db queries, etc)
96
98
  # #
97
99
  #
98
100
  # begin
99
- # result = client.get_recommendations(response)
101
+ # result = client.get_itemrec_top_n(response)
100
102
  # # display results, store results, or your other work...
101
- # rescue RecommendationsNotFoundError => e
103
+ # rescue ItemRecNotFoundError => e
102
104
  # # graceful error handling
103
105
  # end
104
106
 
@@ -113,6 +115,9 @@ module PredictionIO
113
115
  # Only JSON is currently supported as API response format.
114
116
  attr_accessor :apiformat
115
117
 
118
+ # The UID used for recording user-to-item actions and retrieving recommendations.
119
+ attr_accessor :apiuid
120
+
116
121
  # Raised when a user is not created after a synchronous API call.
117
122
  class UserNotCreatedError < StandardError; end
118
123
 
@@ -171,10 +176,10 @@ module PredictionIO
171
176
  # See also #create_user.
172
177
  def acreate_user(uid, params = {})
173
178
  rparams = params
174
- rparams["appkey"] = @appkey
175
- rparams["uid"] = uid
176
- if params["latitude"] != nil && params["longitude"] != nil then
177
- rparams["latlng"] = "#{params["latitude"]},#{params["longitude"]}"
179
+ rparams["pio_appkey"] = @appkey
180
+ rparams["pio_uid"] = uid
181
+ if params["pio_latitude"] != nil && params["pio_longitude"] != nil then
182
+ rparams["pio_latlng"] = "#{params["pio_latitude"]},#{params["pio_longitude"]}"
178
183
  end
179
184
 
180
185
  @http.apost(PredictionIO::AsyncRequest.new(versioned_path("/users.#{@apiformat}"), rparams))
@@ -191,7 +196,6 @@ module PredictionIO
191
196
  def create_user(*args)
192
197
  uid_or_res = args[0]
193
198
  if uid_or_res.is_a?(PredictionIO::AsyncResponse) then
194
- uid = uid_or_res.request.params["uid"]
195
199
  response = uid_or_res.get
196
200
  else
197
201
  uid = uid_or_res
@@ -219,8 +223,8 @@ module PredictionIO
219
223
  # See also #get_user.
220
224
  def aget_user(uid)
221
225
  @http.aget(PredictionIO::AsyncRequest.new(versioned_path("/users/#{uid}.#{@apiformat}"),
222
- "appkey" => @appkey,
223
- "uid" => uid))
226
+ "pio_appkey" => @appkey,
227
+ "pio_uid" => uid))
224
228
  end
225
229
 
226
230
  # :category: Synchronous Methods
@@ -243,12 +247,10 @@ module PredictionIO
243
247
  end
244
248
  if response.is_a?(Net::HTTPOK) then
245
249
  res = JSON.parse(response.body)
246
- ct = Rational(res["ct"], 1000)
247
- res["ct"] = Time.at(ct)
248
- if res["latlng"] != nil then
249
- latlng = res["latlng"]
250
- res["latitude"] = latlng[0]
251
- res["longitude"] = latlng[1]
250
+ if res["pio_latlng"] != nil then
251
+ latlng = res["pio_latlng"]
252
+ res["pio_latitude"] = latlng[0]
253
+ res["pio_longitude"] = latlng[1]
252
254
  end
253
255
  res
254
256
  else
@@ -269,8 +271,8 @@ module PredictionIO
269
271
  # See also #delete_user.
270
272
  def adelete_user(uid)
271
273
  @http.adelete(PredictionIO::AsyncRequest.new(versioned_path("/users/#{uid}.#{@apiformat}"),
272
- "appkey" => @appkey,
273
- "uid" => uid))
274
+ "pio_appkey" => @appkey,
275
+ "pio_uid" => uid))
274
276
  end
275
277
 
276
278
  # :category: Synchronous Methods
@@ -305,21 +307,21 @@ module PredictionIO
305
307
  # See also #create_item.
306
308
  def acreate_item(iid, itypes, params = {})
307
309
  rparams = params
308
- rparams["appkey"] = @appkey
309
- rparams["iid"] = iid
310
+ rparams["pio_appkey"] = @appkey
311
+ rparams["pio_iid"] = iid
310
312
  begin
311
- rparams["itypes"] = itypes.join(",")
313
+ rparams["pio_itypes"] = itypes.join(",")
312
314
  rescue Exception
313
- rparams["itypes"] = itypes
315
+ rparams["pio_itypes"] = itypes
314
316
  end
315
- if params["latitude"] != nil && params["longitude"] != nil then
316
- rparams["latlng"] = "#{params["latitude"]},#{params["longitude"]}"
317
+ if params["pio_latitude"] != nil && params["pio_longitude"] != nil then
318
+ rparams["pio_latlng"] = "#{params["pio_latitude"]},#{params["pio_longitude"]}"
317
319
  end
318
- if params["startT"] != nil then
319
- rparams["startT"] = ((params["startT"].to_r) * 1000).round(0).to_s
320
+ if params["pio_startT"] != nil then
321
+ rparams["pio_startT"] = ((params["pio_startT"].to_r) * 1000).round(0).to_s
320
322
  end
321
- if params["endT"] != nil then
322
- rparams["endT"] = ((params["endT"].to_r) * 1000).round(0).to_s
323
+ if params["pio_endT"] != nil then
324
+ rparams["pio_endT"] = ((params["pio_endT"].to_r) * 1000).round(0).to_s
323
325
  end
324
326
 
325
327
  @http.apost(PredictionIO::AsyncRequest.new(versioned_path("/items.#{@apiformat}"), rparams))
@@ -362,8 +364,8 @@ module PredictionIO
362
364
  # See also #get_item.
363
365
  def aget_item(iid)
364
366
  @http.aget(PredictionIO::AsyncRequest.new(versioned_path("/items/#{iid}.#{@apiformat}"),
365
- "appkey" => @appkey,
366
- "iid" => iid))
367
+ "pio_appkey" => @appkey,
368
+ "pio_iid" => iid))
367
369
  end
368
370
 
369
371
  # :category: Synchronous Methods
@@ -386,20 +388,18 @@ module PredictionIO
386
388
  end
387
389
  if response.is_a?(Net::HTTPOK) then
388
390
  res = JSON.parse(response.body)
389
- ct = Rational(res["ct"], 1000)
390
- res["ct"] = Time.at(ct)
391
- if res["latlng"] != nil then
392
- latlng = res["latlng"]
393
- res["latitude"] = latlng[0]
394
- res["longitude"] = latlng[1]
391
+ if res["pio_latlng"] != nil then
392
+ latlng = res["pio_latlng"]
393
+ res["pio_latitude"] = latlng[0]
394
+ res["pio_longitude"] = latlng[1]
395
395
  end
396
- if res["startT"] != nil then
397
- startT = Rational(res["startT"], 1000)
398
- res["startT"] = Time.at(startT)
396
+ if res["pio_startT"] != nil then
397
+ startT = Rational(res["pio_startT"], 1000)
398
+ res["pio_startT"] = Time.at(startT)
399
399
  end
400
- if res["endT"] != nil then
401
- endT = Rational(res["endT"], 1000)
402
- res["endT"] = Time.at(endT)
400
+ if res["pio_endT"] != nil then
401
+ endT = Rational(res["pio_endT"], 1000)
402
+ res["pio_endT"] = Time.at(endT)
403
403
  end
404
404
  res
405
405
  else
@@ -420,8 +420,8 @@ module PredictionIO
420
420
  # See also #delete_item.
421
421
  def adelete_item(iid)
422
422
  @http.adelete(PredictionIO::AsyncRequest.new(versioned_path("/items/#{iid}.#{@apiformat}"),
423
- "appkey" => @appkey,
424
- "iid" => iid))
423
+ "pio_appkey" => @appkey,
424
+ "pio_iid" => iid))
425
425
  end
426
426
 
427
427
  # :category: Synchronous Methods
@@ -448,32 +448,37 @@ module PredictionIO
448
448
  end
449
449
  end
450
450
 
451
+ # Set the user ID for use in all subsequent user-to-item action recording and user recommendation retrieval.
452
+ def identify(uid)
453
+ @apiuid = uid
454
+ end
455
+
451
456
  # :category: Asynchronous Methods
452
457
  # Asynchronously request to get the top n recommendations for a user from an ItemRec engine and return a PredictionIO::AsyncResponse object immediately.
453
458
  #
454
459
  # Corresponding REST API method: GET /engines/itemrec/:engine/topn
455
460
  #
456
461
  # See also #get_itemrec_top_n.
457
- def aget_itemrec_top_n(engine, uid, n, params = {})
462
+ def aget_itemrec_top_n(engine, n, params = {})
458
463
  rparams = Hash.new
459
- rparams["appkey"] = @appkey
460
- rparams["uid"] = uid
461
- rparams["n"] = n
462
- if params["itypes"] != nil &&
463
- params["itypes"].kind_of?(Array) &&
464
- params["itypes"].length > 0 then
465
- rparams["itypes"] = params["itypes"].join(",")
464
+ rparams["pio_appkey"] = @appkey
465
+ rparams["pio_uid"] = @apiuid
466
+ rparams["pio_n"] = n
467
+ if params["pio_itypes"] != nil &&
468
+ params["pio_itypes"].kind_of?(Array) &&
469
+ params["pio_itypes"].length > 0 then
470
+ rparams["pio_itypes"] = params["pio_itypes"].join(",")
466
471
  else
467
- rparams["itypes"] = params["itypes"]
472
+ rparams["pio_itypes"] = params["pio_itypes"]
468
473
  end
469
- if params["latitude"] != nil && params["longitude"] != nil then
470
- rparams["latlng"] = "#{params["latitude"]},#{params["longitude"]}"
474
+ if params["pio_latitude"] != nil && params["pio_longitude"] != nil then
475
+ rparams["pio_latlng"] = "#{params["pio_latitude"]},#{params["pio_longitude"]}"
471
476
  end
472
- if params["within"] != nil then
473
- rparams["within"] = params["within"]
477
+ if params["pio_within"] != nil then
478
+ rparams["pio_within"] = params["pio_within"]
474
479
  end
475
- if params["unit"] != nil then
476
- rparams["unit"] = params["unit"]
480
+ if params["pio_unit"] != nil then
481
+ rparams["pio_unit"] = params["pio_unit"]
477
482
  end
478
483
  @http.aget(PredictionIO::AsyncRequest.new(versioned_path("/engines/itemrec/#{engine}/topn.#{@apiformat}"), rparams))
479
484
  end
@@ -484,8 +489,8 @@ module PredictionIO
484
489
  # See #aget_itemrec_top_n for a description of special argument handling.
485
490
  #
486
491
  # call-seq:
487
- # get_recommendations(uid, n, params = {})
488
- # get_recommendations(async_response)
492
+ # aget_itemrec_top_n(engine, n, params = {})
493
+ # aget_itemrec_top_n(async_response)
489
494
  def get_itemrec_top_n(*args)
490
495
  uid_or_res = args[0]
491
496
  if uid_or_res.is_a?(PredictionIO::AsyncResponse) then
@@ -495,7 +500,7 @@ module PredictionIO
495
500
  end
496
501
  if response.is_a?(Net::HTTPOK) then
497
502
  res = JSON.parse(response.body)
498
- res["iids"]
503
+ res["pio_iids"]
499
504
  else
500
505
  begin
501
506
  msg = response.body
@@ -507,166 +512,40 @@ module PredictionIO
507
512
  end
508
513
 
509
514
  # :category: Asynchronous Methods
510
- # Asynchronously request to record a user-rate-item action and return a PredictionIO::AsyncResponse object immediately.
511
- #
512
- # Corresponding REST API method: POST /actions/u2i/rate
513
- #
514
- # See also #user_rate_item.
515
- def auser_rate_item(uid, iid, rate, params = {})
516
- params["rate"] = rate
517
- auser_action_item("rate", uid, iid, params)
518
- end
519
-
520
- # :category: Synchronous Methods
521
- # Synchronously request to record a user-rate-item action and block until a response is received.
522
- #
523
- # See #auser_rate_item.
524
- #
525
- # call-seq:
526
- # user_rate_item(uid, iid, rate, params = {})
527
- # user_rate_item(async_response)
528
- def user_rate_item(*args)
529
- if !args[0].is_a?(PredictionIO::AsyncResponse) then
530
- args.unshift("rate")
531
- params = args[4]
532
- if params == nil then
533
- params = Hash.new
534
- end
535
- params["rate"] = args[3]
536
- args[3] = params
537
- end
538
- user_action_item(*args)
539
- end
540
-
541
- # :category: Asynchronous Methods
542
- # Asynchronously request to record a user-like-item action and return a PredictionIO::AsyncResponse object immediately.
543
- #
544
- # Corresponding REST API method: POST /actions/u2i/like
515
+ # Asynchronously request to record an action on an item and return a PredictionIO::AsyncResponse object immediately.
545
516
  #
546
- # See also #user_like_item.
547
- def auser_like_item(uid, iid, params = {})
548
- auser_action_item("like", uid, iid, params)
549
- end
550
-
551
- # :category: Synchronous Methods
552
- # Synchronously request to record a user-like-item action and block until a response is received.
517
+ # Corresponding REST API method: POST /actions/u2i
553
518
  #
554
- # See also #auser_like_item.
555
- #
556
- # call-seq:
557
- # user_like_item(uid, iid, params = {})
558
- # user_like_item(async_response)
559
- def user_like_item(*args)
560
- if !args[0].is_a?(PredictionIO::AsyncResponse) then
561
- args.unshift("like")
519
+ # See also #record_action_on_item.
520
+ def arecord_action_on_item(action, iid, params = {})
521
+ rparams = params
522
+ rparams["pio_appkey"] = @appkey
523
+ rparams["pio_action"] = action
524
+ rparams["pio_uid"] = @apiuid
525
+ rparams["pio_iid"] = iid
526
+ if params["pio_t"] != nil then
527
+ rparams["pio_t"] = ((params["pio_t"].to_r) * 1000).round(0).to_s
562
528
  end
563
- user_action_item(*args)
564
- end
565
-
566
- # :category: Asynchronous Methods
567
- # Asynchronously request to record a user-dislike-item action and return a PredictionIO::AsyncResponse object immediately.
568
- #
569
- # Corresponding REST API method: POST /actions/u2i/dislike
570
- #
571
- # See also #user_dislike_item.
572
- def auser_dislike_item(uid, iid, params = {})
573
- auser_action_item("dislike", uid, iid, params)
574
- end
575
-
576
- # :category: Synchronous Methods
577
- # Synchronously request to record a user-dislike-item action and block until a response is received.
578
- #
579
- # See also #auser_dislike_item.
580
- #
581
- # call-seq:
582
- # user_dislike_item(uid, iid, params = {})
583
- # user_dislike_item(async_response)
584
- def user_dislike_item(*args)
585
- if !args[0].is_a?(PredictionIO::AsyncResponse) then
586
- args.unshift("dislike")
529
+ if params["pio_latitude"] != nil && params["pio_longitude"] != nil then
530
+ rparams["pio_latlng"] = "#{params["pio_latitude"]},#{params["pio_longitude"]}"
587
531
  end
588
- user_action_item(*args)
589
- end
590
-
591
- # :category: Asynchronous Methods
592
- # Asynchronously request to record a user-view-item action and return a PredictionIO::AsyncResponse object immediately.
593
- #
594
- # Corresponding REST API method: POST /actions/u2i/view
595
- #
596
- # See also #user_view_item.
597
- def auser_view_item(uid, iid, params = {})
598
- auser_action_item("view", uid, iid, params)
532
+ @http.apost(PredictionIO::AsyncRequest.new(versioned_path("/actions/u2i.#{@apiformat}"), rparams))
599
533
  end
600
534
 
601
535
  # :category: Synchronous Methods
602
- # Synchronously request to record a user-view-item action and block until a response is received.
536
+ # Synchronously request to record an action on an item and block until a response is received.
603
537
  #
604
- # See also #auser_view_item.
538
+ # See also #arecord_action_on_item.
605
539
  #
606
540
  # call-seq:
607
- # user_view_item(uid, iid, params = {})
608
- # user_view_item(async_response)
609
- def user_view_item(*args)
610
- if !args[0].is_a?(PredictionIO::AsyncResponse) then
611
- args.unshift("view")
612
- end
613
- user_action_item(*args)
614
- end
615
-
616
- # :category: Asynchronous Methods
617
- # Asynchronously request to record a user-conversion-item action and return a PredictionIO::AsyncResponse object immediately.
618
- #
619
- # Corresponding REST API method: POST /actions/u2i/conversion
620
- #
621
- # See also #user_conversion_item.
622
- def auser_conversion_item(uid, iid, params = {})
623
- auser_action_item("conversion", uid, iid, params)
624
- end
625
-
626
- # :category: Synchronous Methods
627
- # Synchronously request to record a user-conversion-item action and block until a response is received.
628
- #
629
- # See also #auser_conversion_item.
630
- #
631
- # call-seq:
632
- # user_conversion_item(uid, iid, params = {})
633
- # user_conversion_item(async_response)
634
- def user_conversion_item(*args)
635
- if !args[0].is_a?(PredictionIO::AsyncResponse) then
636
- args.unshift("conversion")
637
- end
638
- user_action_item(*args)
639
- end
640
-
641
- # :nodoc: all
642
- private
643
-
644
- def versioned_path(path)
645
- # disabled for now
646
- # "/#{@apiversion}#{path}"
647
- path
648
- end
649
-
650
- def auser_action_item(action, uid, iid, params = {})
651
- rparams = params
652
- rparams["appkey"] = @appkey
653
- rparams["uid"] = uid
654
- rparams["iid"] = iid
655
- if params["t"] != nil then
656
- rparams["t"] = ((params["t"].to_r) * 1000).round(0).to_s
657
- end
658
- if params["latitude"] != nil && params["longitude"] != nil then
659
- rparams["latlng"] = "#{params["latitude"]},#{params["longitude"]}"
660
- end
661
- @http.apost(PredictionIO::AsyncRequest.new(versioned_path("/actions/u2i/#{action}.#{@apiformat}"), rparams))
662
- end
663
-
664
- def user_action_item(*args)
541
+ # record_action_on_item(action, uid, iid, params = {})
542
+ # record_action_on_item(async_response)
543
+ def record_action_on_item(*args)
665
544
  action_or_res = args[0]
666
545
  if action_or_res.is_a?(PredictionIO::AsyncResponse) then
667
546
  response = action_or_res.get
668
547
  else
669
- response = auser_action_item(*args).get
548
+ response = arecord_action_on_item(*args).get
670
549
  end
671
550
  unless response.is_a?(Net::HTTPCreated) then
672
551
  begin
@@ -677,5 +556,14 @@ module PredictionIO
677
556
  raise U2IActionNotCreatedError, msg
678
557
  end
679
558
  end
559
+
560
+ # :nodoc: all
561
+ private
562
+
563
+ def versioned_path(path)
564
+ # disabled for now
565
+ # "/#{@apiversion}#{path}"
566
+ path
567
+ end
680
568
  end
681
569
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: predictionio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-25 00:00:00.000000000 Z
12
+ date: 2013-07-20 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! 'PredictionIO is a prediction server for building smart applications.
15
15
  This gem