class PredictionIO::EngineClient

This class contains methods that interface with PredictionIO Engine Instances that are trained from PredictionIO built-in Engines.

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.

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 EngineClient

# Include the PredictionIO SDK
require 'predictionio'

client = PredictionIO::EngineClient.new

Send a Query to Retrieve Predictions

# PredictionIO call to record the view action
begin
  result = client.query('uid' => 'foobar')
rescue NotFoundError => e
  ...
rescue BadRequestError => e
  ...
rescue ServerError => e
  ...
end

Public Class Methods

new(apiurl = 'http://localhost:8000', threads = 1, thread_timeout = 60) click to toggle source

Create a new PredictionIO Event Client with defaults:

  • 1 concurrent HTTP(S) connections (threads)

  • API entry point at localhost:8000 (apiurl)

  • a 60-second timeout for each HTTP(S) connection (thread_timeout)

# File lib/predictionio/engine_client.rb, line 54
def initialize(apiurl = 'http://localhost:8000', threads = 1, thread_timeout = 60)
  @http = PredictionIO::Connection.new(URI(apiurl), threads, thread_timeout)
end

Public Instance Methods

get_status() click to toggle source

Returns PredictionIO's status in string.

# File lib/predictionio/engine_client.rb, line 64
def get_status
  status = @http.aget(PredictionIO::AsyncRequest.new('/')).get
  begin
    status.body
  rescue
    status
  end
end
pending_requests() click to toggle source

Returns the number of pending requests within the current client.

# File lib/predictionio/engine_client.rb, line 59
def pending_requests
  @http.packages.size
end

Protected Instance Methods

sync_events(sync_m, *args) click to toggle source

Internal helper method. Do not call directly.

# File lib/predictionio/engine_client.rb, line 76
def sync_events(sync_m, *args)
  if args[0].is_a?(PredictionIO::AsyncResponse)
    response = args[0].get
  else
    response = send(sync_m, *args).get
  end
  return JSON.parse(response.body) if response.is_a?(Net::HTTPOK)
  begin
    msg = response.body
  rescue
    raise response
  end
  if response.is_a?(Net::HTTPBadRequest)
    fail BadRequestError, msg
  elsif response.is_a?(Net::HTTPNotFound)
    fail NotFoundError, msg
  elsif response.is_a?(Net::HTTPServerError)
    fail ServerError, msg
  else
    fail msg
  end
end

Asynchronous Methods

↑ top

Public Instance Methods

asend_query(query) click to toggle source

Asynchronously sends a query and returns PredictionIO::AsyncResponse object immediately. The query should be a Ruby data structure that can be converted to a JSON object.

Corresponding REST API method: POST /

See also send_query.

# File lib/predictionio/engine_client.rb, line 109
def asend_query(query)
  @http.apost(PredictionIO::AsyncRequest.new('/queries.json', query.to_json))
end

Synchronous Methods

↑ top

Public Instance Methods

send_query(data) click to toggle source
send_query(async_response)

Synchronously sends a query and block until predictions are received.

See also asend_query.

# File lib/predictionio/engine_client.rb, line 121
def send_query(*args)
  sync_events(:asend_query, *args)
end