Class: PredictionIO::Client
- Inherits:
-
Object
- Object
- PredictionIO::Client
- Defined in:
- lib/predictionio/client.rb
Overview
This class contains methods that access PredictionIO via REST requests.
Many REST request methods support optional arguments. They can be supplied to these methods as Hash’es. For a complete reference, please visit prediction.io.
High-performance Asynchronous Backend
All REST request methods come in both synchronous and asynchronous flavors. Both flavors accept the same set of arguments. In addition, all synchronous request methods can instead accept a PredictionIO::AsyncResponse object generated from asynchronous request methods as its first argument. In this case, the method will block until a response is received from it.
Any network reconnection and request retry is automatically handled in the background. Exceptions will be thrown after a request times out to avoid infinite blocking.
Special Handling of Some Optional Arguments
Some optional arguments have additional special handling:
-
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.
-
For all requests that accept “pio_latlng” as input, they will also accept “pio_latitude” and “pio_longitude”. When these are supplied, they will override any existing “pio_latlng” value.
-
All time arguments (e.g. t, pio_startT, pio_endT, etc) can be supplied as either a Time or Float object. When supplied as a Float, the SDK will interpret it as a UNIX UTC timestamp in seconds. The SDK will automatically round to the nearest millisecond, e.g. 3.14159 => 3.142.
Installation
The easiest way is to use RubyGems:
gem install predictionio
Synopsis
The recommended usage of the SDK is to fire asynchronous requests as early as you can in your code, and check results later when you need them.
Instantiate PredictionIO Client
# Include the PredictionIO SDK
require "predictionio"
client = PredictionIO::Client.new(<appkey>)
Import a User Record from Your App (with asynchronous/non-blocking requests)
#
# (your user registration logic)
#
uid = get_user_from_your_db()
# PredictionIO call to create user
response = client.acreate_user(uid)
#
# (other work to do for the rest of the page)
#
begin
# PredictionIO call to retrieve results from an asynchronous response
result = client.create_user(response)
rescue UserNotCreatedError => e
log_and_email_error(...)
end
Import a User Action (Rate) from Your App (with synchronous/blocking requests)
# PredictionIO call to record the view action
begin
client.identify("foouser")
result = client.record_action_on_item("rate", "baritem", "pio_rate" => 4)
rescue U2IActionNotCreatedError => e
...
end
Retrieving Top N Recommendations for a User
# PredictionIO call to get recommendations
client.identify("foouser")
response = client.aget_itemrec_top_n("barengine", 10)
#
# work you need to do for the page (rendering, db queries, etc)
#
begin
result = client.get_itemrec_top_n(response)
# display results, store results, or your other work...
rescue ItemRecNotFoundError => e
# graceful error handling
end
Retrieving Top N Similar Items for an Item
# PredictionIO call to get similar items
response = client.aget_itemsim_top_n("barengine", "fooitem", 10)
#
# work you need to do for the page (rendering, db queries, etc)
#
begin
result = client.get_itemsim_top_n(response)
# display results, store results, or your other work...
rescue ItemSimNotFoundError => e
# graceful error handling
end
Defined Under Namespace
Classes: ItemNotCreatedError, ItemNotDeletedError, ItemNotFoundError, ItemRankNotFoundError, ItemRecNotFoundError, ItemSimNotFoundError, U2IActionNotCreatedError, UserNotCreatedError, UserNotDeletedError, UserNotFoundError
Instance Attribute Summary collapse
-
#apiformat ⇒ Object
Only JSON is currently supported as API response format.
-
#apiuid ⇒ Object
The UID used for recording user-to-item actions and retrieving recommendations.
-
#appkey ⇒ Object
Appkey can be changed on-the-fly after creation of the client.
Instance Method Summary collapse
-
#acreate_item(iid, itypes, params = {}) ⇒ Object
:category: Asynchronous Methods Asynchronously request to create an item and return a PredictionIO::AsyncResponse object immediately.
-
#acreate_user(uid, params = {}) ⇒ Object
:category: Asynchronous Methods Asynchronously request to create a user and return a PredictionIO::AsyncResponse object immediately.
-
#adelete_item(iid) ⇒ Object
:category: Asynchronous Methods Asynchronously request to delete an item and return a PredictionIO::AsyncResponse object immediately.
-
#adelete_user(uid) ⇒ Object
:category: Asynchronous Methods Asynchronously request to delete a user and return a PredictionIO::AsyncResponse object immediately.
-
#aget_item(iid) ⇒ Object
:category: Asynchronous Methods Asynchronously request to get an item and return a PredictionIO::AsyncResponse object immediately.
-
#aget_itemrank_ranked(engine, iids, params = {}) ⇒ Object
:category: Asynchronous Methods Asynchronously request to get the ranking for a user from an ItemRank engine and return a PredictionIO::AsyncResponse object immediately.
-
#aget_itemrec_top_n(engine, n, params = {}) ⇒ Object
:category: Asynchronous Methods Asynchronously request to get the top n recommendations for a user from an ItemRec engine and return a PredictionIO::AsyncResponse object immediately.
-
#aget_itemsim_top_n(engine, iid, n, params = {}) ⇒ Object
:category: Asynchronous Methods Asynchronously request to get the top n similar items for an item from an ItemSim engine and return a PredictionIO::AsyncResponse object immediately.
-
#aget_user(uid) ⇒ Object
:category: Asynchronous Methods Asynchronously request to get a user and return a PredictionIO::AsyncResponse object immediately.
-
#arecord_action_on_item(action, iid, params = {}) ⇒ Object
:category: Asynchronous Methods Asynchronously request to record an action on an item and return a PredictionIO::AsyncResponse object immediately.
-
#create_item(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to create an item and block until a response is received.
-
#create_user(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to create a user and block until a response is received.
-
#delete_item(iid_or_res) ⇒ Object
:category: Synchronous Methods Synchronously request to delete an item and block until a response is received.
-
#delete_user(uid_or_res) ⇒ Object
:category: Synchronous Methods Synchronously request to delete a user and block until a response is received.
-
#get_item(iid_or_res) ⇒ Object
:category: Synchronous Methods Synchronously request to get an item and block until a response is received.
-
#get_itemrank_ranked(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to get the ranking for a user from an ItemRank engine and block until a response is received.
-
#get_itemrec_top_n(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to get the top n recommendations for a user from an ItemRec engine and block until a response is received.
-
#get_itemsim_top_n(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to get the top n similar items for an item from an ItemSim engine and block until a response is received.
-
#get_status ⇒ Object
Returns PredictionIO’s status in string.
-
#get_user(uid_or_res) ⇒ Object
:category: Synchronous Methods Synchronously request to get a user and block until a response is received.
-
#identify(uid) ⇒ Object
Set the user ID for use in all subsequent user-to-item action recording and user recommendation retrieval.
-
#initialize(appkey, threads = 10, apiurl = "http://localhost:8000", thread_timeout = 60) ⇒ Client
constructor
Create a new PredictionIO client with default: - 10 concurrent HTTP(S) connections (threads) - API entry point at localhost:8000 (apiurl) - a 60-second timeout for each HTTP(S) connection (thread_timeout).
-
#pending_requests ⇒ Object
Returns the number of pending requests within the current client.
-
#record_action_on_item(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to record an action on an item and block until a response is received.
Constructor Details
#initialize(appkey, threads = 10, apiurl = "http://localhost:8000", thread_timeout = 60) ⇒ Client
Create a new PredictionIO client with default:
-
10 concurrent HTTP(S) connections (threads)
-
API entry point at localhost:8000 (apiurl)
-
a 60-second timeout for each HTTP(S) connection (thread_timeout)
161 162 163 164 165 |
# File 'lib/predictionio/client.rb', line 161 def initialize(appkey, threads = 10, apiurl = "http://localhost:8000", thread_timeout = 60) @appkey = appkey @apiformat = "json" @http = PredictionIO::Connection.new(URI(apiurl), threads, thread_timeout) end |
Instance Attribute Details
#apiformat ⇒ Object
Only JSON is currently supported as API response format.
122 123 124 |
# File 'lib/predictionio/client.rb', line 122 def apiformat @apiformat end |
#apiuid ⇒ Object
The UID used for recording user-to-item actions and retrieving recommendations.
125 126 127 |
# File 'lib/predictionio/client.rb', line 125 def apiuid @apiuid end |
#appkey ⇒ Object
Appkey can be changed on-the-fly after creation of the client.
119 120 121 |
# File 'lib/predictionio/client.rb', line 119 def appkey @appkey end |
Instance Method Details
#acreate_item(iid, itypes, params = {}) ⇒ Object
:category: Asynchronous Methods Asynchronously request to create an item and return a PredictionIO::AsyncResponse object immediately.
Corresponding REST API method: POST /items
See also #create_item.
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/predictionio/client.rb', line 319 def acreate_item(iid, itypes, params = {}) rparams = params rparams["pio_appkey"] = @appkey rparams["pio_iid"] = iid begin rparams["pio_itypes"] = itypes.join(",") rescue Exception rparams["pio_itypes"] = itypes end if params["pio_latitude"] && params["pio_longitude"] rparams["pio_latlng"] = "#{params["pio_latitude"]},#{params["pio_longitude"]}" end rparams["pio_startT"] = ((params["pio_startT"].to_r) * 1000).round(0).to_s if params["pio_startT"] rparams["pio_endT"] = ((params["pio_endT"].to_r) * 1000).round(0).to_s if params["pio_endT"] @http.apost(PredictionIO::AsyncRequest.new("/items.#{@apiformat}", rparams)) end |
#acreate_user(uid, params = {}) ⇒ Object
:category: Asynchronous Methods Asynchronously request to create a user and return a PredictionIO::AsyncResponse object immediately.
Corresponding REST API method: POST /users
See also #create_user.
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/predictionio/client.rb', line 188 def acreate_user(uid, params = {}) rparams = params rparams["pio_appkey"] = @appkey rparams["pio_uid"] = uid if params["pio_latitude"] && params["pio_longitude"] rparams["pio_latlng"] = "#{params["pio_latitude"]},#{params["pio_longitude"]}" end @http.apost(PredictionIO::AsyncRequest.new("/users.#{@apiformat}", rparams)) end |
#adelete_item(iid) ⇒ Object
:category: Asynchronous Methods Asynchronously request to delete an item and return a PredictionIO::AsyncResponse object immediately.
Corresponding REST API method: DELETE /items/:iid
See also #delete_item.
428 429 430 431 432 |
# File 'lib/predictionio/client.rb', line 428 def adelete_item(iid) @http.adelete(PredictionIO::AsyncRequest.new("/items/#{iid}.#{@apiformat}", "pio_appkey" => @appkey, "pio_iid" => iid)) end |
#adelete_user(uid) ⇒ Object
:category: Asynchronous Methods Asynchronously request to delete a user and return a PredictionIO::AsyncResponse object immediately.
Corresponding REST API method: DELETE /users/:uid
See also #delete_user.
283 284 285 286 287 |
# File 'lib/predictionio/client.rb', line 283 def adelete_user(uid) @http.adelete(PredictionIO::AsyncRequest.new("/users/#{uid}.#{@apiformat}", "pio_appkey" => @appkey, "pio_uid" => uid)) end |
#aget_item(iid) ⇒ Object
:category: Asynchronous Methods Asynchronously request to get an item and return a PredictionIO::AsyncResponse object immediately.
Creation time of the user will be returned as a Time object.
If the result contains a latlng key, both latitude and longitude will also be available as separate keys.
Corresponding REST API method: GET /items/:iid
See also #get_item.
372 373 374 375 376 |
# File 'lib/predictionio/client.rb', line 372 def aget_item(iid) @http.aget(PredictionIO::AsyncRequest.new("/items/#{iid}.#{@apiformat}", "pio_appkey" => @appkey, "pio_iid" => iid)) end |
#aget_itemrank_ranked(engine, iids, params = {}) ⇒ Object
:category: Asynchronous Methods Asynchronously request to get the ranking for a user from an ItemRank engine and return a PredictionIO::AsyncResponse object immediately.
Corresponding REST API method: GET /engines/itemrank/:engine/ranked
See also #get_itemrank_ranked.
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
# File 'lib/predictionio/client.rb', line 537 def aget_itemrank_ranked(engine, iids, params = {}) rparams = Hash.new rparams["pio_appkey"] = @appkey rparams["pio_uid"] = @apiuid if iids.kind_of?(Array) && iids.any? rparams["pio_iids"] = iids.join(",") else rparams["pio_iids"] = iids end if params["pio_attributes"] if params["pio_attributes"].kind_of?(Array) && params["pio_attributes"].any? rparams["pio_attributes"] = params["pio_attributes"].join(",") else rparams["pio_attributes"] = params["pio_attributes"] end end @http.aget(PredictionIO::AsyncRequest.new("/engines/itemrank/#{engine}/ranked.#{@apiformat}", rparams)) end |
#aget_itemrec_top_n(engine, n, params = {}) ⇒ Object
:category: Asynchronous Methods Asynchronously request to get the top n recommendations for a user from an ItemRec engine and return a PredictionIO::AsyncResponse object immediately.
Corresponding REST API method: GET /engines/itemrec/:engine/topn
See also #get_itemrec_top_n.
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
# File 'lib/predictionio/client.rb', line 469 def aget_itemrec_top_n(engine, n, params = {}) rparams = Hash.new rparams["pio_appkey"] = @appkey rparams["pio_uid"] = @apiuid rparams["pio_n"] = n if params["pio_itypes"] if params["pio_itypes"].kind_of?(Array) && params["pio_itypes"].any? rparams["pio_itypes"] = params["pio_itypes"].join(",") else rparams["pio_itypes"] = params["pio_itypes"] end end if params["pio_latitude"] && params["pio_longitude"] rparams["pio_latlng"] = "#{params["pio_latitude"]},#{params["pio_longitude"]}" end rparams["pio_within"] = params["pio_within"] if params["pio_within"] rparams["pio_unit"] = params["pio_unit"] if params["pio_unit"] if params["pio_attributes"] if params["pio_attributes"].kind_of?(Array) && params["pio_attributes"].any? rparams["pio_attributes"] = params["pio_attributes"].join(",") else rparams["pio_attributes"] = params["pio_attributes"] end end @http.aget(PredictionIO::AsyncRequest.new("/engines/itemrec/#{engine}/topn.#{@apiformat}", rparams)) end |
#aget_itemsim_top_n(engine, iid, n, params = {}) ⇒ Object
:category: Asynchronous Methods Asynchronously request to get the top n similar items for an item from an ItemSim engine and return a PredictionIO::AsyncResponse object immediately.
Corresponding REST API method: GET /engines/itemsim/:engine/topn
See also #get_itemsim_top_n.
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 |
# File 'lib/predictionio/client.rb', line 597 def aget_itemsim_top_n(engine, iid, n, params = {}) rparams = Hash.new rparams["pio_appkey"] = @appkey rparams["pio_iid"] = iid rparams["pio_n"] = n if params["pio_itypes"] if params["pio_itypes"].kind_of?(Array) && params["pio_itypes"].any? rparams["pio_itypes"] = params["pio_itypes"].join(",") else rparams["pio_itypes"] = params["pio_itypes"] end end if params["pio_latitude"] && params["pio_longitude"] rparams["pio_latlng"] = "#{params["pio_latitude"]},#{params["pio_longitude"]}" end rparams["pio_within"] = params["pio_within"] if params["pio_within"] rparams["pio_unit"] = params["pio_unit"] if params["pio_unit"] if params["pio_attributes"] if params["pio_attributes"].kind_of?(Array) && params["pio_attributes"].any? rparams["pio_attributes"] = params["pio_attributes"].join(",") else rparams["pio_attributes"] = params["pio_attributes"] end end @http.aget(PredictionIO::AsyncRequest.new("/engines/itemsim/#{engine}/topn.#{@apiformat}", rparams)) end |
#aget_user(uid) ⇒ Object
:category: Asynchronous Methods Asynchronously request to get a user and return a PredictionIO::AsyncResponse object immediately.
Creation time of the user will be returned as a Time object.
If the result contains a latlng key, both latitude and longitude will also be available as separate keys.
Corresponding REST API method: GET /users/:uid
See also #get_user.
235 236 237 238 239 |
# File 'lib/predictionio/client.rb', line 235 def aget_user(uid) @http.aget(PredictionIO::AsyncRequest.new("/users/#{uid}.#{@apiformat}", "pio_appkey" => @appkey, "pio_uid" => uid)) end |
#arecord_action_on_item(action, iid, params = {}) ⇒ Object
:category: Asynchronous Methods Asynchronously request to record an action on an item and return a PredictionIO::AsyncResponse object immediately.
Corresponding REST API method: POST /actions/u2i
See also #record_action_on_item.
665 666 667 668 669 670 671 672 673 674 675 676 |
# File 'lib/predictionio/client.rb', line 665 def arecord_action_on_item(action, iid, params = {}) rparams = params rparams["pio_appkey"] = @appkey rparams["pio_action"] = action rparams["pio_uid"] = @apiuid rparams["pio_iid"] = iid rparams["pio_t"] = ((params["pio_t"].to_r) * 1000).round(0).to_s if params["pio_t"] if params["pio_latitude"] && params["pio_longitude"] rparams["pio_latlng"] = "#{params["pio_latitude"]},#{params["pio_longitude"]}" end @http.apost(PredictionIO::AsyncRequest.new("/actions/u2i.#{@apiformat}", rparams)) end |
#create_item(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to create an item and block until a response is received.
See #acreate_item for a description of other accepted arguments.
call-seq: create_item(iid, itypes, params = {}) create_item(async_response)
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/predictionio/client.rb', line 345 def create_item(*args) iid_or_res = args[0] if iid_or_res.is_a?(PredictionIO::AsyncResponse) response = iid_or_res.get else response = acreate_item(*args).get end unless response.is_a?(Net::HTTPCreated) begin msg = response.body rescue Exception raise ItemNotCreatedError, response end raise ItemNotCreatedError, msg end end |
#create_user(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to create a user and block until a response is received.
See also #acreate_user.
call-seq: create_user(uid, params = {}) create_user(async_response)
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/predictionio/client.rb', line 207 def create_user(*args) uid_or_res = args[0] if uid_or_res.is_a?(PredictionIO::AsyncResponse) response = uid_or_res.get else uid = uid_or_res response = acreate_user(*args).get end unless response.is_a?(Net::HTTPCreated) begin msg = response.body rescue Exception raise UserNotCreatedError, response end raise UserNotCreatedError, msg end end |
#delete_item(iid_or_res) ⇒ Object
:category: Synchronous Methods Synchronously request to delete an item and block until a response is received.
See also #adelete_item.
call-seq: delete_item(iid) delete_item(async_response)
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/predictionio/client.rb', line 442 def delete_item(iid_or_res) if iid_or_res.is_a?(PredictionIO::AsyncResponse) response = iid_or_res.get else response = adelete_item(iid_or_res).get end unless response.is_a?(Net::HTTPOK) begin msg = response.body rescue Exception raise ItemNotDeletedError, response end raise ItemNotDeletedError, msg end end |
#delete_user(uid_or_res) ⇒ Object
:category: Synchronous Methods Synchronously request to delete a user and block until a response is received.
See also #adelete_user.
call-seq: delete_user(uid) delete_user(async_response)
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/predictionio/client.rb', line 297 def delete_user(uid_or_res) if uid_or_res.is_a?(PredictionIO::AsyncResponse) response = uid_or_res.get else response = adelete_user(uid_or_res).get end unless response.is_a?(Net::HTTPOK) begin msg = response.body rescue Exception raise UserNotDeletedError, response end raise msg end end |
#get_item(iid_or_res) ⇒ Object
:category: Synchronous Methods Synchronously request to get an item and block until a response is received.
Creation time of the item will be returned as a Time object.
If the result contains a latlng key, both latitude and longitude will also be available as separate keys.
See also #aget_item.
call-seq: get_item(iid) get_item(async_response)
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/predictionio/client.rb', line 390 def get_item(iid_or_res) if iid_or_res.is_a?(PredictionIO::AsyncResponse) response = iid_or_res.get else response = aget_item(iid_or_res).get end if response.is_a?(Net::HTTPOK) res = JSON.parse(response.body) if res["pio_latlng"] latlng = res["pio_latlng"] res["pio_latitude"] = latlng[0] res["pio_longitude"] = latlng[1] end if res["pio_startT"] startT = Rational(res["pio_startT"], 1000) res["pio_startT"] = Time.at(startT) end if res["pio_endT"] endT = Rational(res["pio_endT"], 1000) res["pio_endT"] = Time.at(endT) end res else begin msg = response.body rescue Exception raise ItemNotFoundError, response end raise ItemNotFoundError, msg end end |
#get_itemrank_ranked(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to get the ranking for a user from an ItemRank engine and block until a response is received.
See #aget_itemrank_ranked for a description of special argument handling.
call-seq: get_itemrank_ranked(engine, n, params = {}) get_itemrank_ranked(async_response)
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 |
# File 'lib/predictionio/client.rb', line 564 def get_itemrank_ranked(*args) uid_or_res = args[0] if uid_or_res.is_a?(PredictionIO::AsyncResponse) response = uid_or_res else response = aget_itemrank_ranked(*args) end http_response = response.get if http_response.is_a?(Net::HTTPOK) res = JSON.parse(http_response.body) if response.request.params.has_key?('pio_attributes') attributes = response.request.params['pio_attributes'].split(',') list_of_attribute_values = attributes.map { |attrib| res[attrib] } res["pio_iids"].zip(*list_of_attribute_values).map { |v| Hash[(['pio_iid'] + attributes).zip(v)] } else res["pio_iids"] end else begin msg = response.body rescue Exception raise ItemRankNotFoundError, response end raise ItemRankNotFoundError, msg end end |
#get_itemrec_top_n(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to get the top n recommendations for a user from an ItemRec engine and block until a response is received.
See #aget_itemrec_top_n for a description of special argument handling.
call-seq: get_itemrec_top_n(engine, n, params = {}) get_itemrec_top_n(async_response)
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/predictionio/client.rb', line 504 def get_itemrec_top_n(*args) uid_or_res = args[0] if uid_or_res.is_a?(PredictionIO::AsyncResponse) response = uid_or_res else response = aget_itemrec_top_n(*args) end http_response = response.get if http_response.is_a?(Net::HTTPOK) res = JSON.parse(http_response.body) if response.request.params.has_key?('pio_attributes') attributes = response.request.params['pio_attributes'].split(',') list_of_attribute_values = attributes.map { |attrib| res[attrib] } res["pio_iids"].zip(*list_of_attribute_values).map { |v| Hash[(['pio_iid'] + attributes).zip(v)] } else res["pio_iids"] end else begin msg = response.body rescue Exception raise ItemRecNotFoundError, response end raise ItemRecNotFoundError, msg end end |
#get_itemsim_top_n(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to get the top n similar items for an item from an ItemSim engine and block until a response is received.
See #aget_itemsim_top_n for a description of special argument handling.
call-seq: get_itemsim_top_n(engine, iid, n, params = {}) get_itemsim_top_n(async_response)
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 |
# File 'lib/predictionio/client.rb', line 632 def get_itemsim_top_n(*args) uid_or_res = args[0] if uid_or_res.is_a?(PredictionIO::AsyncResponse) response = uid_or_res else response = aget_itemsim_top_n(*args) end http_response = response.get if http_response.is_a?(Net::HTTPOK) res = JSON.parse(http_response.body) if response.request.params.has_key?('pio_attributes') attributes = response.request.params['pio_attributes'].split(',') list_of_attribute_values = attributes.map { |attrib| res[attrib] } res["pio_iids"].zip(*list_of_attribute_values).map { |v| Hash[(['pio_iid'] + attributes).zip(v)] } else res["pio_iids"] end else begin msg = response.body rescue Exception raise ItemSimNotFoundError, response end raise ItemSimNotFoundError, msg end end |
#get_status ⇒ Object
Returns PredictionIO’s status in string.
173 174 175 176 177 178 179 180 |
# File 'lib/predictionio/client.rb', line 173 def get_status status = @http.aget(PredictionIO::AsyncRequest.new("/")).get() begin status.body rescue Exception status end end |
#get_user(uid_or_res) ⇒ Object
:category: Synchronous Methods Synchronously request to get a user and block until a response is received.
Creation time of the user will be returned as a Time object.
If the result contains a latlng key, both latitude and longitude will also be available as separate keys.
See also #aget_user.
call-seq: get_user(uid) get_user(async_response)
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/predictionio/client.rb', line 253 def get_user(uid_or_res) if uid_or_res.is_a?(PredictionIO::AsyncResponse) response = uid_or_res.get else response = aget_user(uid_or_res).get end if response.is_a?(Net::HTTPOK) res = JSON.parse(response.body) if res["pio_latlng"] latlng = res["pio_latlng"] res["pio_latitude"] = latlng[0] res["pio_longitude"] = latlng[1] end res else begin msg = response.body rescue Exception raise UserNotFoundError, response end raise UserNotFoundError, msg end end |
#identify(uid) ⇒ Object
Set the user ID for use in all subsequent user-to-item action recording and user recommendation retrieval.
459 460 461 |
# File 'lib/predictionio/client.rb', line 459 def identify(uid) @apiuid = uid end |
#pending_requests ⇒ Object
Returns the number of pending requests within the current client.
168 169 170 |
# File 'lib/predictionio/client.rb', line 168 def pending_requests @http.packages.size end |
#record_action_on_item(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to record an action on an item and block until a response is received.
See also #arecord_action_on_item.
call-seq: record_action_on_item(action, iid, params = {}) record_action_on_item(async_response)
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 |
# File 'lib/predictionio/client.rb', line 686 def record_action_on_item(*args) action_or_res = args[0] if action_or_res.is_a?(PredictionIO::AsyncResponse) response = action_or_res.get else response = arecord_action_on_item(*args).get end unless response.is_a?(Net::HTTPCreated) begin msg = response.body rescue Exception raise U2IActionNotCreatedError, response end raise U2IActionNotCreatedError, msg end end |