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') ⇒ EngineClient

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)



51
52
53
54
55
# File 'lib/predictionio/engine_client.rb', line 51

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

Instance Method Details

#get_statusObject

Returns PredictionIO’s status in string.



59
60
61
62
63
64
65
66
# File 'lib/predictionio/engine_client.rb', line 59

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

#send_query(query) ⇒ Object

Sends a query and returns the response. The query should be a Ruby data structure that can be converted to a JSON object.

Corresponding REST API method: POST /



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/predictionio/engine_client.rb', line 73

def send_query(query)
  response = @http.post(PredictionIO::Request.new('/queries.json', query.to_json))
  return JSON.parse(response.body) if response.success?
  begin
    msg = response.body
  rescue
    raise response
  end
  case response.status
  when 400
    fail BadRequestError, msg
  when 404
    fail NotFoundError, msg
  when 500
    fail ServerError, msg
  else
    fail msg
  end
end