predictionio 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4ff41097d6e2912ea649a62771deb609c9a3f025
4
+ data.tar.gz: 51a35d920b0918024faa2b51fbd4ed191582afba
5
+ SHA512:
6
+ metadata.gz: 60eb1cabaf2fea7df32447b602af24545731130e664b7584e7a5b2cdb90ba4278b55ab1051adf66d5ae872b992107a6a35f414c82c09f0fa99c40f8e6777a12c
7
+ data.tar.gz: 6753cb134986efe6f0d0dd05235efe5639f948f47896fdd6e555d65c490408da67bb2f184a832dd3b81054edbf80a5e8c7b327d0f0b33ef446b67ba20e52f217
@@ -11,9 +11,7 @@ module PredictionIO
11
11
  def initialize(request, response = nil)
12
12
  @request = request
13
13
  @response = Queue.new
14
- if response != nil then
15
- set(response)
16
- end
14
+ set(response) if response
17
15
  end
18
16
 
19
17
  # Save a Net::HTTPResponse instance to the current instance.
@@ -45,7 +45,7 @@ module PredictionIO
45
45
  #
46
46
  # == Installation
47
47
  # The easiest way is to use RubyGems:
48
- # gem install predictionio-0.6.0.gem
48
+ # gem install predictionio
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,
@@ -53,7 +53,7 @@ module PredictionIO
53
53
  #
54
54
  # === Instantiate PredictionIO Client
55
55
  # # Include the PredictionIO SDK
56
- # require "PredictionIO"
56
+ # require "predictionio"
57
57
  #
58
58
  # client = PredictionIO::Client.new(<appkey>)
59
59
  #
@@ -124,9 +124,6 @@ module PredictionIO
124
124
  # Appkey can be changed on-the-fly after creation of the client.
125
125
  attr_accessor :appkey
126
126
 
127
- # API version can be changed on-the-fly after creation of the client.
128
- attr_accessor :apiversion
129
-
130
127
  # Only JSON is currently supported as API response format.
131
128
  attr_accessor :apiformat
132
129
 
@@ -164,11 +161,10 @@ module PredictionIO
164
161
  # - API entry point at http://localhost:8000
165
162
  # - API return data format of json
166
163
  # - 10 concurrent HTTP(S) connections
167
- def initialize(appkey, threads = 10, apiurl = "http://localhost:8000", apiversion = "")
164
+ def initialize(appkey, threads = 10, apiurl = "http://localhost:8000", thread_timeout = 60)
168
165
  @appkey = appkey
169
- @apiversion = apiversion
170
166
  @apiformat = "json"
171
- @http = PredictionIO::Connection.new(URI(apiurl), threads)
167
+ @http = PredictionIO::Connection.new(URI(apiurl), threads, thread_timeout)
172
168
  end
173
169
 
174
170
  # Returns the number of pending requests within the current client.
@@ -196,11 +192,11 @@ module PredictionIO
196
192
  rparams = params
197
193
  rparams["pio_appkey"] = @appkey
198
194
  rparams["pio_uid"] = uid
199
- if params["pio_latitude"] != nil && params["pio_longitude"] != nil then
195
+ if params["pio_latitude"] && params["pio_longitude"]
200
196
  rparams["pio_latlng"] = "#{params["pio_latitude"]},#{params["pio_longitude"]}"
201
197
  end
202
198
 
203
- @http.apost(PredictionIO::AsyncRequest.new(versioned_path("/users.#{@apiformat}"), rparams))
199
+ @http.apost(PredictionIO::AsyncRequest.new("/users.#{@apiformat}", rparams))
204
200
  end
205
201
 
206
202
  # :category: Synchronous Methods
@@ -213,13 +209,13 @@ module PredictionIO
213
209
  # create_user(async_response)
214
210
  def create_user(*args)
215
211
  uid_or_res = args[0]
216
- if uid_or_res.is_a?(PredictionIO::AsyncResponse) then
212
+ if uid_or_res.is_a?(PredictionIO::AsyncResponse)
217
213
  response = uid_or_res.get
218
214
  else
219
215
  uid = uid_or_res
220
216
  response = acreate_user(*args).get
221
217
  end
222
- unless response.is_a?(Net::HTTPCreated) then
218
+ unless response.is_a?(Net::HTTPCreated)
223
219
  begin
224
220
  msg = response.body
225
221
  rescue Exception
@@ -240,7 +236,7 @@ module PredictionIO
240
236
  #
241
237
  # See also #get_user.
242
238
  def aget_user(uid)
243
- @http.aget(PredictionIO::AsyncRequest.new(versioned_path("/users/#{uid}.#{@apiformat}"),
239
+ @http.aget(PredictionIO::AsyncRequest.new("/users/#{uid}.#{@apiformat}",
244
240
  "pio_appkey" => @appkey,
245
241
  "pio_uid" => uid))
246
242
  end
@@ -258,14 +254,14 @@ module PredictionIO
258
254
  # get_user(uid)
259
255
  # get_user(async_response)
260
256
  def get_user(uid_or_res)
261
- if uid_or_res.is_a?(PredictionIO::AsyncResponse) then
257
+ if uid_or_res.is_a?(PredictionIO::AsyncResponse)
262
258
  response = uid_or_res.get
263
259
  else
264
260
  response = aget_user(uid_or_res).get
265
261
  end
266
- if response.is_a?(Net::HTTPOK) then
262
+ if response.is_a?(Net::HTTPOK)
267
263
  res = JSON.parse(response.body)
268
- if res["pio_latlng"] != nil then
264
+ if res["pio_latlng"]
269
265
  latlng = res["pio_latlng"]
270
266
  res["pio_latitude"] = latlng[0]
271
267
  res["pio_longitude"] = latlng[1]
@@ -288,7 +284,7 @@ module PredictionIO
288
284
  #
289
285
  # See also #delete_user.
290
286
  def adelete_user(uid)
291
- @http.adelete(PredictionIO::AsyncRequest.new(versioned_path("/users/#{uid}.#{@apiformat}"),
287
+ @http.adelete(PredictionIO::AsyncRequest.new("/users/#{uid}.#{@apiformat}",
292
288
  "pio_appkey" => @appkey,
293
289
  "pio_uid" => uid))
294
290
  end
@@ -302,12 +298,12 @@ module PredictionIO
302
298
  # delete_user(uid)
303
299
  # delete_user(async_response)
304
300
  def delete_user(uid_or_res)
305
- if uid_or_res.is_a?(PredictionIO::AsyncResponse) then
301
+ if uid_or_res.is_a?(PredictionIO::AsyncResponse)
306
302
  response = uid_or_res.get
307
303
  else
308
304
  response = adelete_user(uid_or_res).get
309
305
  end
310
- unless response.is_a?(Net::HTTPOK) then
306
+ unless response.is_a?(Net::HTTPOK)
311
307
  begin
312
308
  msg = response.body
313
309
  rescue Exception
@@ -332,17 +328,13 @@ module PredictionIO
332
328
  rescue Exception
333
329
  rparams["pio_itypes"] = itypes
334
330
  end
335
- if params["pio_latitude"] != nil && params["pio_longitude"] != nil then
331
+ if params["pio_latitude"] && params["pio_longitude"]
336
332
  rparams["pio_latlng"] = "#{params["pio_latitude"]},#{params["pio_longitude"]}"
337
333
  end
338
- if params["pio_startT"] != nil then
339
- rparams["pio_startT"] = ((params["pio_startT"].to_r) * 1000).round(0).to_s
340
- end
341
- if params["pio_endT"] != nil then
342
- rparams["pio_endT"] = ((params["pio_endT"].to_r) * 1000).round(0).to_s
343
- end
334
+ rparams["pio_startT"] = ((params["pio_startT"].to_r) * 1000).round(0).to_s if params["pio_startT"]
335
+ rparams["pio_endT"] = ((params["pio_endT"].to_r) * 1000).round(0).to_s if params["pio_endT"]
344
336
 
345
- @http.apost(PredictionIO::AsyncRequest.new(versioned_path("/items.#{@apiformat}"), rparams))
337
+ @http.apost(PredictionIO::AsyncRequest.new("/items.#{@apiformat}", rparams))
346
338
  end
347
339
 
348
340
  # :category: Synchronous Methods
@@ -355,12 +347,12 @@ module PredictionIO
355
347
  # create_item(async_response)
356
348
  def create_item(*args)
357
349
  iid_or_res = args[0]
358
- if iid_or_res.is_a?(PredictionIO::AsyncResponse) then
350
+ if iid_or_res.is_a?(PredictionIO::AsyncResponse)
359
351
  response = iid_or_res.get
360
352
  else
361
353
  response = acreate_item(*args).get
362
354
  end
363
- unless response.is_a?(Net::HTTPCreated) then
355
+ unless response.is_a?(Net::HTTPCreated)
364
356
  begin
365
357
  msg = response.body
366
358
  rescue Exception
@@ -381,7 +373,7 @@ module PredictionIO
381
373
  #
382
374
  # See also #get_item.
383
375
  def aget_item(iid)
384
- @http.aget(PredictionIO::AsyncRequest.new(versioned_path("/items/#{iid}.#{@apiformat}"),
376
+ @http.aget(PredictionIO::AsyncRequest.new("/items/#{iid}.#{@apiformat}",
385
377
  "pio_appkey" => @appkey,
386
378
  "pio_iid" => iid))
387
379
  end
@@ -399,23 +391,23 @@ module PredictionIO
399
391
  # get_item(iid)
400
392
  # get_item(async_response)
401
393
  def get_item(iid_or_res)
402
- if iid_or_res.is_a?(PredictionIO::AsyncResponse) then
394
+ if iid_or_res.is_a?(PredictionIO::AsyncResponse)
403
395
  response = iid_or_res.get
404
396
  else
405
397
  response = aget_item(iid_or_res).get
406
398
  end
407
- if response.is_a?(Net::HTTPOK) then
399
+ if response.is_a?(Net::HTTPOK)
408
400
  res = JSON.parse(response.body)
409
- if res["pio_latlng"] != nil then
401
+ if res["pio_latlng"]
410
402
  latlng = res["pio_latlng"]
411
403
  res["pio_latitude"] = latlng[0]
412
404
  res["pio_longitude"] = latlng[1]
413
405
  end
414
- if res["pio_startT"] != nil then
406
+ if res["pio_startT"]
415
407
  startT = Rational(res["pio_startT"], 1000)
416
408
  res["pio_startT"] = Time.at(startT)
417
409
  end
418
- if res["pio_endT"] != nil then
410
+ if res["pio_endT"]
419
411
  endT = Rational(res["pio_endT"], 1000)
420
412
  res["pio_endT"] = Time.at(endT)
421
413
  end
@@ -437,7 +429,7 @@ module PredictionIO
437
429
  #
438
430
  # See also #delete_item.
439
431
  def adelete_item(iid)
440
- @http.adelete(PredictionIO::AsyncRequest.new(versioned_path("/items/#{iid}.#{@apiformat}"),
432
+ @http.adelete(PredictionIO::AsyncRequest.new("/items/#{iid}.#{@apiformat}",
441
433
  "pio_appkey" => @appkey,
442
434
  "pio_iid" => iid))
443
435
  end
@@ -451,12 +443,12 @@ module PredictionIO
451
443
  # delete_item(iid)
452
444
  # delete_item(async_response)
453
445
  def delete_item(iid_or_res)
454
- if iid_or_res.is_a?(PredictionIO::AsyncResponse) then
446
+ if iid_or_res.is_a?(PredictionIO::AsyncResponse)
455
447
  response = iid_or_res.get
456
448
  else
457
449
  response = adelete_item(iid_or_res).get
458
450
  end
459
- unless response.is_a?(Net::HTTPOK) then
451
+ unless response.is_a?(Net::HTTPOK)
460
452
  begin
461
453
  msg = response.body
462
454
  rescue Exception
@@ -482,23 +474,26 @@ module PredictionIO
482
474
  rparams["pio_appkey"] = @appkey
483
475
  rparams["pio_uid"] = @apiuid
484
476
  rparams["pio_n"] = n
485
- if params["pio_itypes"] != nil &&
486
- params["pio_itypes"].kind_of?(Array) &&
487
- params["pio_itypes"].length > 0 then
488
- rparams["pio_itypes"] = params["pio_itypes"].join(",")
489
- else
490
- rparams["pio_itypes"] = params["pio_itypes"]
477
+ if params["pio_itypes"]
478
+ if params["pio_itypes"].kind_of?(Array) && params["pio_itypes"].any?
479
+ rparams["pio_itypes"] = params["pio_itypes"].join(",")
480
+ else
481
+ rparams["pio_itypes"] = params["pio_itypes"]
482
+ end
491
483
  end
492
- if params["pio_latitude"] != nil && params["pio_longitude"] != nil then
484
+ if params["pio_latitude"] && params["pio_longitude"]
493
485
  rparams["pio_latlng"] = "#{params["pio_latitude"]},#{params["pio_longitude"]}"
494
486
  end
495
- if params["pio_within"] != nil then
496
- rparams["pio_within"] = params["pio_within"]
497
- end
498
- if params["pio_unit"] != nil then
499
- rparams["pio_unit"] = params["pio_unit"]
487
+ rparams["pio_within"] = params["pio_within"] if params["pio_within"]
488
+ rparams["pio_unit"] = params["pio_unit"] if params["pio_unit"]
489
+ if params["pio_attributes"]
490
+ if params["pio_attributes"].kind_of?(Array) && params["pio_attributes"].any?
491
+ rparams["pio_attributes"] = params["pio_attributes"].join(",")
492
+ else
493
+ rparams["pio_attributes"] = params["pio_attributes"]
494
+ end
500
495
  end
501
- @http.aget(PredictionIO::AsyncRequest.new(versioned_path("/engines/itemrec/#{engine}/topn.#{@apiformat}"), rparams))
496
+ @http.aget(PredictionIO::AsyncRequest.new("/engines/itemrec/#{engine}/topn.#{@apiformat}", rparams))
502
497
  end
503
498
 
504
499
  # :category: Synchronous Methods
@@ -511,14 +506,21 @@ module PredictionIO
511
506
  # aget_itemrec_top_n(async_response)
512
507
  def get_itemrec_top_n(*args)
513
508
  uid_or_res = args[0]
514
- if uid_or_res.is_a?(PredictionIO::AsyncResponse) then
515
- response = uid_or_res.get
509
+ if uid_or_res.is_a?(PredictionIO::AsyncResponse)
510
+ response = uid_or_res
516
511
  else
517
- response = aget_itemrec_top_n(*args).get
518
- end
519
- if response.is_a?(Net::HTTPOK) then
520
- res = JSON.parse(response.body)
521
- res["pio_iids"]
512
+ response = aget_itemrec_top_n(*args)
513
+ end
514
+ http_response = response.get
515
+ if http_response.is_a?(Net::HTTPOK)
516
+ res = JSON.parse(http_response.body)
517
+ if response.request.params.has_key?('pio_attributes')
518
+ attributes = response.request.params['pio_attributes'].split(',')
519
+ list_of_attribute_values = attributes.map { |attrib| res[attrib] }
520
+ res["pio_iids"].zip(*list_of_attribute_values).map { |v| Hash[(['pio_iid'] + attributes).zip(v)] }
521
+ else
522
+ res["pio_iids"]
523
+ end
522
524
  else
523
525
  begin
524
526
  msg = response.body
@@ -540,23 +542,26 @@ module PredictionIO
540
542
  rparams["pio_appkey"] = @appkey
541
543
  rparams["pio_iid"] = iid
542
544
  rparams["pio_n"] = n
543
- if params["pio_itypes"] != nil &&
544
- params["pio_itypes"].kind_of?(Array) &&
545
- params["pio_itypes"].length > 0 then
546
- rparams["pio_itypes"] = params["pio_itypes"].join(",")
547
- else
548
- rparams["pio_itypes"] = params["pio_itypes"]
545
+ if params["pio_itypes"]
546
+ if params["pio_itypes"].kind_of?(Array) && params["pio_itypes"].any?
547
+ rparams["pio_itypes"] = params["pio_itypes"].join(",")
548
+ else
549
+ rparams["pio_itypes"] = params["pio_itypes"]
550
+ end
549
551
  end
550
- if params["pio_latitude"] != nil && params["pio_longitude"] != nil then
552
+ if params["pio_latitude"] && params["pio_longitude"]
551
553
  rparams["pio_latlng"] = "#{params["pio_latitude"]},#{params["pio_longitude"]}"
552
554
  end
553
- if params["pio_within"] != nil then
554
- rparams["pio_within"] = params["pio_within"]
555
- end
556
- if params["pio_unit"] != nil then
557
- rparams["pio_unit"] = params["pio_unit"]
555
+ rparams["pio_within"] = params["pio_within"] if params["pio_within"]
556
+ rparams["pio_unit"] = params["pio_unit"] if params["pio_unit"]
557
+ if params["pio_attributes"]
558
+ if params["pio_attributes"].kind_of?(Array) && params["pio_attributes"].any?
559
+ rparams["pio_attributes"] = params["pio_attributes"].join(",")
560
+ else
561
+ rparams["pio_attributes"] = params["pio_attributes"]
562
+ end
558
563
  end
559
- @http.aget(PredictionIO::AsyncRequest.new(versioned_path("/engines/itemsim/#{engine}/topn.#{@apiformat}"), rparams))
564
+ @http.aget(PredictionIO::AsyncRequest.new("/engines/itemsim/#{engine}/topn.#{@apiformat}", rparams))
560
565
  end
561
566
 
562
567
  # :category: Synchronous Methods
@@ -568,15 +573,22 @@ module PredictionIO
568
573
  # aget_itemsim_top_n(engine, iid, n, params = {})
569
574
  # aget_itemsim_top_n(async_response)
570
575
  def get_itemsim_top_n(*args)
571
- iid_or_res = args[0]
572
- if iid_or_res.is_a?(PredictionIO::AsyncResponse) then
573
- response = iid_or_res.get
576
+ uid_or_res = args[0]
577
+ if uid_or_res.is_a?(PredictionIO::AsyncResponse)
578
+ response = uid_or_res
574
579
  else
575
- response = aget_itemsim_top_n(*args).get
576
- end
577
- if response.is_a?(Net::HTTPOK) then
578
- res = JSON.parse(response.body)
579
- res["pio_iids"]
580
+ response = aget_itemsim_top_n(*args)
581
+ end
582
+ http_response = response.get
583
+ if http_response.is_a?(Net::HTTPOK)
584
+ res = JSON.parse(http_response.body)
585
+ if response.request.params.has_key?('pio_attributes')
586
+ attributes = response.request.params['pio_attributes'].split(',')
587
+ list_of_attribute_values = attributes.map { |attrib| res[attrib] }
588
+ res["pio_iids"].zip(*list_of_attribute_values).map { |v| Hash[(['pio_iid'] + attributes).zip(v)] }
589
+ else
590
+ res["pio_iids"]
591
+ end
580
592
  else
581
593
  begin
582
594
  msg = response.body
@@ -599,13 +611,11 @@ module PredictionIO
599
611
  rparams["pio_action"] = action
600
612
  rparams["pio_uid"] = @apiuid
601
613
  rparams["pio_iid"] = iid
602
- if params["pio_t"] != nil then
603
- rparams["pio_t"] = ((params["pio_t"].to_r) * 1000).round(0).to_s
604
- end
605
- if params["pio_latitude"] != nil && params["pio_longitude"] != nil then
614
+ rparams["pio_t"] = ((params["pio_t"].to_r) * 1000).round(0).to_s if params["pio_t"]
615
+ if params["pio_latitude"] && params["pio_longitude"]
606
616
  rparams["pio_latlng"] = "#{params["pio_latitude"]},#{params["pio_longitude"]}"
607
617
  end
608
- @http.apost(PredictionIO::AsyncRequest.new(versioned_path("/actions/u2i.#{@apiformat}"), rparams))
618
+ @http.apost(PredictionIO::AsyncRequest.new("/actions/u2i.#{@apiformat}", rparams))
609
619
  end
610
620
 
611
621
  # :category: Synchronous Methods
@@ -614,16 +624,16 @@ module PredictionIO
614
624
  # See also #arecord_action_on_item.
615
625
  #
616
626
  # call-seq:
617
- # record_action_on_item(action, uid, iid, params = {})
627
+ # record_action_on_item(action, iid, params = {})
618
628
  # record_action_on_item(async_response)
619
629
  def record_action_on_item(*args)
620
630
  action_or_res = args[0]
621
- if action_or_res.is_a?(PredictionIO::AsyncResponse) then
631
+ if action_or_res.is_a?(PredictionIO::AsyncResponse)
622
632
  response = action_or_res.get
623
633
  else
624
634
  response = arecord_action_on_item(*args).get
625
635
  end
626
- unless response.is_a?(Net::HTTPCreated) then
636
+ unless response.is_a?(Net::HTTPCreated)
627
637
  begin
628
638
  msg = response.body
629
639
  rescue Exception
@@ -632,14 +642,5 @@ module PredictionIO
632
642
  raise U2IActionNotCreatedError, msg
633
643
  end
634
644
  end
635
-
636
- # :nodoc: all
637
- private
638
-
639
- def versioned_path(path)
640
- # disabled for now
641
- # "/#{@apiversion}#{path}"
642
- path
643
- end
644
645
  end
645
646
  end
@@ -14,7 +14,7 @@ module PredictionIO
14
14
 
15
15
  # Spawns a number of threads with persistent HTTP connection to the specified URI.
16
16
  # Sets a default timeout of 5 seconds.
17
- def initialize(uri, threads = 1, timeout = 5)
17
+ def initialize(uri, threads = 1, timeout = 60)
18
18
  @packages = Queue.new
19
19
  @counter_lock = Mutex.new
20
20
  @connections = 0
metadata CHANGED
@@ -1,24 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: predictionio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
5
- prerelease:
4
+ version: 0.7.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - The PredictionIO Team
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-09-04 00:00:00.000000000 Z
11
+ date: 2014-04-01 00:00:00.000000000 Z
13
12
  dependencies: []
14
- description: ! 'PredictionIO is a prediction server for building smart applications.
15
- This gem
16
-
13
+ description: |
14
+ PredictionIO is a prediction server for building smart applications. This gem
17
15
  provides convenient access of the PredictionIO API to Ruby programmers so that
18
-
19
16
  they can focus on their application logic.
20
-
21
- '
22
17
  email: support@prediction.io
23
18
  executables: []
24
19
  extensions: []
@@ -31,26 +26,25 @@ files:
31
26
  - lib/predictionio.rb
32
27
  homepage: http://prediction.io
33
28
  licenses: []
29
+ metadata: {}
34
30
  post_install_message:
35
31
  rdoc_options: []
36
32
  require_paths:
37
33
  - lib
38
34
  required_ruby_version: !ruby/object:Gem::Requirement
39
- none: false
40
35
  requirements:
41
- - - ! '>='
36
+ - - '>='
42
37
  - !ruby/object:Gem::Version
43
38
  version: '1.9'
44
39
  required_rubygems_version: !ruby/object:Gem::Requirement
45
- none: false
46
40
  requirements:
47
- - - ! '>='
41
+ - - '>='
48
42
  - !ruby/object:Gem::Version
49
43
  version: '0'
50
44
  requirements: []
51
45
  rubyforge_project:
52
- rubygems_version: 1.8.24
46
+ rubygems_version: 2.0.3
53
47
  signing_key:
54
- specification_version: 3
48
+ specification_version: 4
55
49
  summary: PredictionIO Ruby SDK
56
50
  test_files: []