Class: PredictionIO::EventClient
- Inherits:
-
Object
- Object
- PredictionIO::EventClient
- Defined in:
- lib/predictionio/event_client.rb
Overview
This class contains methods that interface with the PredictionIO Event Server via the PredictionIO Event API using 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.
Installation
The easiest way is to use RubyGems:
gem install predictionio
Synopsis
In most cases, using synchronous methods. If you have a special performance requirement, you may want to take a look at asynchronous methods.
Instantiate an EventClient
# Include the PredictionIO SDK
require 'predictionio'
client = PredictionIO::EventClient.new(<access_key>)
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.aset_user(uid)
#
# (other work to do for the rest of the page)
#
begin
# PredictionIO call to retrieve results from an asynchronous response
result = client.set_user(response)
rescue PredictionIO::EventClient::NotCreatedError => 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
result = client.record_user_action_on_item('rate', 'foouser',
'baritem',
'rating' => 4)
rescue PredictionIO::EventClient::NotCreatedError => e
...
end
Defined Under Namespace
Classes: NotCreatedError
Instance Method Summary collapse
-
#acreate_event(event, entity_type, entity_id, optional = {}) ⇒ Object
:category: Asynchronous Methods Asynchronously request to create an event and return a PredictionIO::AsyncResponse object immediately.
-
#adelete_item(uid) ⇒ 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.
-
#arecord_user_action_on_item(action, uid, iid, optional = {}) ⇒ Object
:category: Asynchronous Methods Asynchronously request to record an action on an item and return a PredictionIO::AsyncResponse object immediately.
-
#aset_item(iid, optional = {}) ⇒ Object
:category: Asynchronous Methods Asynchronously request to set properties of an item and return a PredictionIO::AsyncResponse object immediately.
-
#aset_user(uid, optional = {}) ⇒ Object
:category: Asynchronous Methods Asynchronously request to set properties of a user and return a PredictionIO::AsyncResponse object immediately.
-
#aunset_item(iid, optional) ⇒ Object
:category: Asynchronous Methods Asynchronously request to unset properties of an item and return a PredictionIO::AsyncResponse object immediately.
-
#aunset_user(uid, optional) ⇒ Object
:category: Asynchronous Methods Asynchronously request to unset properties of a user and return a PredictionIO::AsyncResponse object immediately.
-
#create_event(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to create an event and block until a response is received.
-
#delete_item(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to delete an item and block until a response is received.
-
#delete_user(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to delete a user and block until a response is received.
-
#get_status ⇒ Object
Returns PredictionIO’s status in string.
-
#initialize(access_key, apiurl = 'http://localhost:7070', threads = 1, thread_timeout = 60) ⇒ EventClient
constructor
Create a new PredictionIO Event Client with defaults: - 1 concurrent HTTP(S) connections (threads) - API entry point at localhost:7070 (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_user_action_on_item(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to record an action on an item and block until a response is received.
-
#set_item(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to set properties of an item and block until a response is received.
-
#set_user(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to set properties of a user and block until a response is received.
-
#unset_item(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to unset properties of an item and block until a response is received.
-
#unset_user(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to unset properties of a user and block until a response is received.
Constructor Details
#initialize(access_key, apiurl = 'http://localhost:7070', threads = 1, thread_timeout = 60) ⇒ EventClient
Create a new PredictionIO Event Client with defaults:
-
1 concurrent HTTP(S) connections (threads)
-
API entry point at localhost:7070 (apiurl)
-
a 60-second timeout for each HTTP(S) connection (thread_timeout)
84 85 86 87 |
# File 'lib/predictionio/event_client.rb', line 84 def initialize(access_key, apiurl = 'http://localhost:7070', threads = 1, thread_timeout = 60) @access_key = access_key @http = PredictionIO::Connection.new(URI(apiurl), threads, thread_timeout) end |
Instance Method Details
#acreate_event(event, entity_type, entity_id, optional = {}) ⇒ Object
:category: Asynchronous Methods Asynchronously request to create an event and return a PredictionIO::AsyncResponse object immediately.
Corresponding REST API method: POST /events.json
See also #create_event.
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/predictionio/event_client.rb', line 111 def acreate_event(event, entity_type, entity_id, optional = {}) h = optional h.key?('eventTime') || h['eventTime'] = DateTime.now.to_s h['event'] = event h['entityType'] = entity_type h['entityId'] = entity_id @http.apost(PredictionIO::AsyncRequest.new( "/events.json?accessKey=#{@access_key}", h.to_json )) end |
#adelete_item(uid) ⇒ Object
:category: Asynchronous Methods Asynchronously request to delete an item and return a PredictionIO::AsyncResponse object immediately.
Corresponding REST API method: POST /events.json
See also #delete_item.
274 275 276 |
# File 'lib/predictionio/event_client.rb', line 274 def adelete_item(uid) acreate_event('$delete', 'item', uid) 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: POST /events.json
See also #delete_user.
196 197 198 |
# File 'lib/predictionio/event_client.rb', line 196 def adelete_user(uid) acreate_event('$delete', 'user', uid) end |
#arecord_user_action_on_item(action, uid, iid, optional = {}) ⇒ 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 /events.json
See also #record_user_action_on_item.
298 299 300 301 302 |
# File 'lib/predictionio/event_client.rb', line 298 def arecord_user_action_on_item(action, uid, iid, optional = {}) optional['targetEntityType'] = 'item' optional['targetEntityId'] = iid acreate_event(action, 'user', uid, optional) end |
#aset_item(iid, optional = {}) ⇒ Object
:category: Asynchronous Methods Asynchronously request to set properties of an item and return a PredictionIO::AsyncResponse object immediately.
Corresponding REST API method: POST /events.json
See also #set_item.
220 221 222 |
# File 'lib/predictionio/event_client.rb', line 220 def aset_item(iid, optional = {}) acreate_event('$set', 'item', iid, optional) end |
#aset_user(uid, optional = {}) ⇒ Object
:category: Asynchronous Methods Asynchronously request to set properties of a user and return a PredictionIO::AsyncResponse object immediately.
Corresponding REST API method: POST /events.json
See also #set_user.
142 143 144 |
# File 'lib/predictionio/event_client.rb', line 142 def aset_user(uid, optional = {}) acreate_event('$set', 'user', uid, optional) end |
#aunset_item(iid, optional) ⇒ Object
:category: Asynchronous Methods Asynchronously request to unset properties of an item and return a PredictionIO::AsyncResponse object immediately.
properties must be a non-empty Hash.
Corresponding REST API method: POST /events.json
See also #unset_item.
246 247 248 249 250 251 252 |
# File 'lib/predictionio/event_client.rb', line 246 def aunset_item(iid, optional) optional.key?('properties') || fail(ArgumentError, 'properties must be present when event is $unset') optional['properties'].empty? && fail(ArgumentError, 'properties cannot be empty when event is $unset') acreate_event('$unset', 'item', iid, optional) end |
#aunset_user(uid, optional) ⇒ Object
:category: Asynchronous Methods Asynchronously request to unset properties of a user and return a PredictionIO::AsyncResponse object immediately.
properties must be a non-empty Hash.
Corresponding REST API method: POST /events.json
See also #unset_user.
168 169 170 171 172 173 174 |
# File 'lib/predictionio/event_client.rb', line 168 def aunset_user(uid, optional) optional.key?('properties') || fail(ArgumentError, 'properties must be present when event is $unset') optional['properties'].empty? && fail(ArgumentError, 'properties cannot be empty when event is $unset') acreate_event('$unset', 'user', uid, optional) end |
#create_event(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to create an event and block until a response is received.
See also #acreate_event.
call-seq: create_event(event, entity_type, entity_id, optional = {}) create_event(async_response)
131 132 133 |
# File 'lib/predictionio/event_client.rb', line 131 def create_event(*args) sync_events(:acreate_event, *args) end |
#delete_item(*args) ⇒ 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(uid) delete_item(async_response)
287 288 289 |
# File 'lib/predictionio/event_client.rb', line 287 def delete_item(*args) sync_events(:adelete_item, *args) end |
#delete_user(*args) ⇒ 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)
209 210 211 |
# File 'lib/predictionio/event_client.rb', line 209 def delete_user(*args) sync_events(:adelete_user, *args) end |
#get_status ⇒ Object
Returns PredictionIO’s status in string.
95 96 97 98 99 100 101 102 |
# File 'lib/predictionio/event_client.rb', line 95 def get_status status = @http.aget(PredictionIO::AsyncRequest.new('/')).get begin status.body rescue status end end |
#pending_requests ⇒ Object
Returns the number of pending requests within the current client.
90 91 92 |
# File 'lib/predictionio/event_client.rb', line 90 def pending_requests @http.packages.size end |
#record_user_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_user_action_on_item.
call-seq: record_user_action_on_item(action, uid, iid, optional = {}) record_user_action_on_item(async_response)
313 314 315 |
# File 'lib/predictionio/event_client.rb', line 313 def record_user_action_on_item(*args) sync_events(:arecord_user_action_on_item, *args) end |
#set_item(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to set properties of an item and block until a response is received.
See also #aset_item.
call-seq: set_item(iid, properties = {}, optional = {}) set_item(async_response)
233 234 235 |
# File 'lib/predictionio/event_client.rb', line 233 def set_item(*args) sync_events(:aset_item, *args) end |
#set_user(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to set properties of a user and block until a response is received.
See also #aset_user.
call-seq: set_user(uid, optional = {}) set_user(async_response)
155 156 157 |
# File 'lib/predictionio/event_client.rb', line 155 def set_user(*args) sync_events(:aset_user, *args) end |
#unset_item(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to unset properties of an item and block until a response is received.
See also #aunset_item.
call-seq: unset_item(iid, properties, optional = {}) unset_item(async_response)
263 264 265 |
# File 'lib/predictionio/event_client.rb', line 263 def unset_item(*args) sync_events(:aunset_item, *args) end |
#unset_user(*args) ⇒ Object
:category: Synchronous Methods Synchronously request to unset properties of a user and block until a response is received.
See also #aunset_user.
call-seq: unset_user(uid, optional) unset_user(async_response)
185 186 187 |
# File 'lib/predictionio/event_client.rb', line 185 def unset_user(*args) sync_events(:aunset_user, *args) end |