Class: PredictionIO::EngineClient

Inherits:
Object
  • Object
show all
Defined in:
lib/predictionio/engine_client.rb

Overview

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

Defined Under Namespace

Classes: BadRequestError, NotFoundError, ServerError

Instance Method Summary collapse

Constructor Details

#initialize(apiurl = 'http://localhost:8000', threads = 1, thread_timeout = 60) ⇒ EngineClient

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)



54
55
56
57
# 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

Instance Method Details

#asend_query(query) ⇒ Object

:category: Asynchronous Methods 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.



110
111
112
113
# File 'lib/predictionio/engine_client.rb', line 110

def asend_query(query)
  @http.apost(PredictionIO::AsyncRequest.new('/queries.json',
                                             query.to_json))
end

#get_statusObject

Returns PredictionIO’s status in string.



65
66
67
68
69
70
71
72
# File 'lib/predictionio/engine_client.rb', line 65

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

#pending_requestsObject

Returns the number of pending requests within the current client.



60
61
62
# File 'lib/predictionio/engine_client.rb', line 60

def pending_requests
  @http.packages.size
end

#send_query(*args) ⇒ Object

:category: Synchronous Methods Synchronously sends a query and block until predictions are received.

See also #asend_query.

call-seq: send_query(data) send_query(async_response)



123
124
125
# File 'lib/predictionio/engine_client.rb', line 123

def send_query(*args)
  sync_events(:asend_query, *args)
end