Class: PredictionIO::EventClient

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(access_key, apiurl = 'http://localhost:7070') ⇒ 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)



85
86
87
88
89
90
# File 'lib/predictionio/event_client.rb', line 85

def initialize(access_key, apiurl = 'http://localhost:7070')
  @access_key = access_key
  @http = PredictionIO::Connection.new(URI(apiurl)) do |faraday|
   yield faraday if block_given?
 end
end

Instance Method Details

#create_event(event, entity_type, entity_id, optional = {}) ⇒ Object

Request to create an event and return the response.

Corresponding REST API method: POST /events.json



105
106
107
108
109
110
111
112
113
114
# File 'lib/predictionio/event_client.rb', line 105

def create_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.post(PredictionIO::Request.new(
    "/events.json?accessKey=#{@access_key}", h.to_json
  ))
end

#delete_event(event_id) ⇒ Object

Request to delete an event and return the response.

Corresponding REST API method: DELETE events/<your_eventId>.json



119
120
121
122
123
# File 'lib/predictionio/event_client.rb', line 119

def delete_event(event_id)
  @http.delete(PredictionIO::Request.new(
    "/events/#{event_id}.json?accessKey=#{@access_key}"
  ))
end

#delete_item(uid) ⇒ Object

Request to delete an item and return the response.

Corresponding REST API method: POST /events.json



169
170
171
# File 'lib/predictionio/event_client.rb', line 169

def delete_item(uid)
  create_event('$delete', 'item', uid)
end

#delete_user(uid) ⇒ Object

Request to delete a user and return the response.

Corresponding REST API method: POST /events.json



145
146
147
# File 'lib/predictionio/event_client.rb', line 145

def delete_user(uid)
  create_event('$delete', 'user', uid)
end

#get_statusObject

Returns PredictionIO’s status in string.



93
94
95
96
97
98
99
100
# File 'lib/predictionio/event_client.rb', line 93

def get_status
  status = @http.get(PredictionIO::Request.new('/'))
  begin
    status.body
  rescue
    status
  end
end

#record_user_action_on_item(action, uid, iid, optional = {}) ⇒ Object

Request to record an action on an item and return the response.

Corresponding REST API method: POST /events.json



176
177
178
179
180
# File 'lib/predictionio/event_client.rb', line 176

def record_user_action_on_item(action, uid, iid, optional = {})
  optional['targetEntityType'] = 'item'
  optional['targetEntityId'] = iid
  create_event(action, 'user', uid, optional)
end

#set_item(iid, optional = {}) ⇒ Object

Request to set properties of an item and return the response.

Corresponding REST API method: POST /events.json



152
153
154
# File 'lib/predictionio/event_client.rb', line 152

def set_item(iid, optional = {})
  create_event('$set', 'item', iid, optional)
end

#set_user(uid, optional = {}) ⇒ Object

Request to set properties of a user and return the response.

Corresponding REST API method: POST /events.json



128
129
130
# File 'lib/predictionio/event_client.rb', line 128

def set_user(uid, optional = {})
  create_event('$set', 'user', uid, optional)
end

#unset_item(iid, optional) ⇒ Object

Request to unset properties of an item and return the response.

properties must be a non-empty Hash.

Corresponding REST API method: POST /events.json



161
162
163
164
# File 'lib/predictionio/event_client.rb', line 161

def unset_item(iid, optional)
  check_unset_properties(optional)
  create_event('$unset', 'item', iid, optional)
end

#unset_user(uid, optional) ⇒ Object

Request to unset properties of a user and return the response.

properties must be a non-empty Hash.

Corresponding REST API method: POST /events.json



137
138
139
140
# File 'lib/predictionio/event_client.rb', line 137

def unset_user(uid, optional)
  check_unset_properties(optional)
  create_event('$unset', 'user', uid, optional)
end