predictionio-simple 0.9.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/predictionio.rb +42 -0
- data/lib/predictionio/connection.rb +41 -0
- data/lib/predictionio/engine_client.rb +91 -0
- data/lib/predictionio/event_client.rb +178 -0
- data/lib/predictionio/file_exporter.rb +29 -0
- data/lib/predictionio/request.rb +24 -0
- data/lib/predictionio/version.rb +3 -0
- metadata +68 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2c82647e51feecbf559157484b4b3ebeb3b6f2c1
|
4
|
+
data.tar.gz: 5e08aa77e161a3192d5c33c996866ccba14135d8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a82d105160bad7f2e4867ae0e2b6a49c00301a71806acf15d02d62f3c78b11c648518db44fcf1d72229769348a14e6eb22f2ff3464cd4f1d156423b7ce7f8049
|
7
|
+
data.tar.gz: f0c4d1e64093e9d0c9bf5169a9266c1f080800c0b1427e2304e3f5cae2091ca638b584a31c2ee6c339cfaf3999e5fc6ffffed93416204d4fcd0516ffaf9403e5
|
data/lib/predictionio.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'predictionio/request'
|
3
|
+
require 'predictionio/connection'
|
4
|
+
require 'predictionio/event_client'
|
5
|
+
require 'predictionio/engine_client'
|
6
|
+
require 'predictionio/file_exporter'
|
7
|
+
require 'predictionio/version'
|
8
|
+
|
9
|
+
# The PredictionIO module contains classes that provide convenient access of
|
10
|
+
# PredictionIO Event API and Engine Instance over HTTP/HTTPS.
|
11
|
+
#
|
12
|
+
# To create an app and perform predictions, please download the PredictionIO
|
13
|
+
# suite from http://prediction.io.
|
14
|
+
#
|
15
|
+
# Most functionality is provided by PredictionIO::EventClient and
|
16
|
+
# PredictionIO::EngineClient classes.
|
17
|
+
#
|
18
|
+
# == Deprecation Notice
|
19
|
+
#
|
20
|
+
# Pre-0.7.x series support is now deprecated. All existing users are strongly
|
21
|
+
# encouraged to migrate to 0.8.x.
|
22
|
+
#
|
23
|
+
# The old Client interface is retained in case of any accidental Gem upgrade.
|
24
|
+
# It will be removed in the next minor version.
|
25
|
+
#
|
26
|
+
# == High-performance Asynchronous Backend
|
27
|
+
#
|
28
|
+
# All REST request methods come in both synchronous and asynchronous flavors.
|
29
|
+
# Both flavors accept the same set of arguments. In addition, all synchronous
|
30
|
+
# request methods can instead accept a PredictionIO::AsyncResponse object
|
31
|
+
# generated from asynchronous request methods as its first argument. In this
|
32
|
+
# case, the method will block until a response is received from it.
|
33
|
+
#
|
34
|
+
# Any network reconnection and request retry is automatically handled in the
|
35
|
+
# background. Exceptions will be thrown after a request times out to avoid
|
36
|
+
# infinite blocking.
|
37
|
+
#
|
38
|
+
# == Installation
|
39
|
+
# The easiest way is to use RubyGems:
|
40
|
+
# gem install predictionio
|
41
|
+
module PredictionIO
|
42
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
module PredictionIO
|
3
|
+
|
4
|
+
# This class handles Connections
|
5
|
+
class Connection
|
6
|
+
|
7
|
+
|
8
|
+
# Creates a connection to the given URI.
|
9
|
+
def initialize(uri)
|
10
|
+
@connection = Faraday.new(:url => uri) do |faraday|
|
11
|
+
faraday.request :url_encoded # form-encode POST params
|
12
|
+
faraday.response :logger # log requests to STDOUT
|
13
|
+
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
|
14
|
+
end
|
15
|
+
@connection.headers['Content-Type'] = 'application/json; charset=utf-8'
|
16
|
+
end
|
17
|
+
|
18
|
+
# Create a GET request and return the response.
|
19
|
+
def get(request)
|
20
|
+
@connection.get request.qpath
|
21
|
+
end
|
22
|
+
|
23
|
+
# Create a POST and return the response.
|
24
|
+
def post(request)
|
25
|
+
if request.params.is_a?(Hash)
|
26
|
+
@connection.post request.path, request.params
|
27
|
+
else
|
28
|
+
@connection.post request.path do |req|
|
29
|
+
req.body = request.params
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Create a DELETE and return the response.
|
35
|
+
def delete(request)
|
36
|
+
@connection.delete request.path do |req|
|
37
|
+
req.body = request.params
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# Ruby SDK for convenient access of PredictionIO Output API.
|
2
|
+
#
|
3
|
+
# Author:: PredictionIO Team (support@prediction.io)
|
4
|
+
# Copyright:: Copyright (c) 2014 TappingStone, Inc.
|
5
|
+
# Copyright:: Copyright (c) 2016 Perpetto BG Ltd.
|
6
|
+
# License:: Apache License, Version 2.0
|
7
|
+
|
8
|
+
module PredictionIO
|
9
|
+
# This class contains methods that interface with PredictionIO Engine
|
10
|
+
# Instances that are trained from PredictionIO built-in Engines.
|
11
|
+
#
|
12
|
+
# Many REST request methods support optional arguments. They can be supplied
|
13
|
+
# to these methods as Hash'es. For a complete reference, please visit
|
14
|
+
# http://prediction.io.
|
15
|
+
#
|
16
|
+
# == Synopsis
|
17
|
+
# In most cases, using synchronous methods. If you have a special performance
|
18
|
+
# requirement, you may want to take a look at asynchronous methods.
|
19
|
+
#
|
20
|
+
# === Instantiate an EngineClient
|
21
|
+
# # Include the PredictionIO SDK
|
22
|
+
# require 'predictionio'
|
23
|
+
#
|
24
|
+
# client = PredictionIO::EngineClient.new
|
25
|
+
#
|
26
|
+
# === Send a Query to Retrieve Predictions
|
27
|
+
# # PredictionIO call to record the view action
|
28
|
+
# begin
|
29
|
+
# result = client.query('uid' => 'foobar')
|
30
|
+
# rescue NotFoundError => e
|
31
|
+
# ...
|
32
|
+
# rescue BadRequestError => e
|
33
|
+
# ...
|
34
|
+
# rescue ServerError => e
|
35
|
+
# ...
|
36
|
+
# end
|
37
|
+
class EngineClient
|
38
|
+
# Raised when an event is not created after a synchronous API call.
|
39
|
+
class NotFoundError < StandardError; end
|
40
|
+
|
41
|
+
# Raised when the query is malformed.
|
42
|
+
class BadRequestError < StandardError; end
|
43
|
+
|
44
|
+
# Raised when the Engine Instance returns a server error.
|
45
|
+
class ServerError < StandardError; end
|
46
|
+
|
47
|
+
# Create a new PredictionIO Event Client with defaults:
|
48
|
+
# - 1 concurrent HTTP(S) connections (threads)
|
49
|
+
# - API entry point at http://localhost:8000 (apiurl)
|
50
|
+
# - a 60-second timeout for each HTTP(S) connection (thread_timeout)
|
51
|
+
def initialize(apiurl = 'http://localhost:8000')
|
52
|
+
@http = PredictionIO::Connection.new(URI(apiurl))
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
# Returns PredictionIO's status in string.
|
57
|
+
def get_status
|
58
|
+
status = @http.get(PredictionIO::Request.new('/'))
|
59
|
+
begin
|
60
|
+
status.body
|
61
|
+
rescue
|
62
|
+
status
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Sends a query and returns the response.
|
67
|
+
# The query should be a Ruby data structure that can be
|
68
|
+
# converted to a JSON object.
|
69
|
+
#
|
70
|
+
# Corresponding REST API method: POST /
|
71
|
+
def send_query(query)
|
72
|
+
response = @http.post(PredictionIO::Request.new('/queries.json', query.to_json))
|
73
|
+
return JSON.parse(response.body) if response.success?
|
74
|
+
begin
|
75
|
+
msg = response.body
|
76
|
+
rescue
|
77
|
+
raise response
|
78
|
+
end
|
79
|
+
case response.status
|
80
|
+
when 400
|
81
|
+
fail BadRequestError, msg
|
82
|
+
when 404
|
83
|
+
fail NotFoundError, msg
|
84
|
+
when 500
|
85
|
+
fail ServerError, msg
|
86
|
+
else
|
87
|
+
fail msg
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,178 @@
|
|
1
|
+
# Ruby SDK for convenient access of PredictionIO Output API.
|
2
|
+
#
|
3
|
+
# Author:: PredictionIO Team (support@prediction.io)
|
4
|
+
# Copyright:: Copyright (c) 2014 TappingStone, Inc.
|
5
|
+
# Copyright:: Copyright (c) 2016 Perpetto BG Ltd.
|
6
|
+
# License:: Apache License, Version 2.0
|
7
|
+
|
8
|
+
require 'date'
|
9
|
+
require "net/http"
|
10
|
+
|
11
|
+
module PredictionIO
|
12
|
+
# This class contains methods that interface with the PredictionIO Event
|
13
|
+
# Server via the PredictionIO Event API using REST requests.
|
14
|
+
#
|
15
|
+
# Many REST request methods support optional arguments. They can be supplied
|
16
|
+
# to these methods as Hash'es. For a complete reference, please visit
|
17
|
+
# http://prediction.io.
|
18
|
+
#
|
19
|
+
# == High-performance Asynchronous Backend
|
20
|
+
#
|
21
|
+
# All REST request methods come in both synchronous and asynchronous flavors.
|
22
|
+
# Both flavors accept the same set of arguments. In addition, all synchronous
|
23
|
+
# request methods can instead accept a PredictionIO::AsyncResponse object
|
24
|
+
# generated from asynchronous request methods as its first argument. In this
|
25
|
+
# case, the method will block until a response is received from it.
|
26
|
+
#
|
27
|
+
# Any network reconnection and request retry is automatically handled in the
|
28
|
+
# background. Exceptions will be thrown after a request times out to avoid
|
29
|
+
# infinite blocking.
|
30
|
+
#
|
31
|
+
# == Installation
|
32
|
+
# The easiest way is to use RubyGems:
|
33
|
+
# gem install predictionio
|
34
|
+
#
|
35
|
+
# == Synopsis
|
36
|
+
# In most cases, using synchronous methods. If you have a special performance
|
37
|
+
# requirement, you may want to take a look at asynchronous methods.
|
38
|
+
#
|
39
|
+
# === Instantiate an EventClient
|
40
|
+
# # Include the PredictionIO SDK
|
41
|
+
# require 'predictionio'
|
42
|
+
#
|
43
|
+
# client = PredictionIO::EventClient.new(<access_key>)
|
44
|
+
#
|
45
|
+
# === Import a User Record from Your App (with asynchronous/non-blocking
|
46
|
+
# requests)
|
47
|
+
#
|
48
|
+
# #
|
49
|
+
# # (your user registration logic)
|
50
|
+
# #
|
51
|
+
#
|
52
|
+
# uid = get_user_from_your_db()
|
53
|
+
#
|
54
|
+
# # PredictionIO call to create user
|
55
|
+
# response = client.aset_user(uid)
|
56
|
+
#
|
57
|
+
# #
|
58
|
+
# # (other work to do for the rest of the page)
|
59
|
+
# #
|
60
|
+
#
|
61
|
+
# begin
|
62
|
+
# # PredictionIO call to retrieve results from an asynchronous response
|
63
|
+
# result = client.set_user(response)
|
64
|
+
# rescue PredictionIO::EventClient::NotCreatedError => e
|
65
|
+
# log_and_email_error(...)
|
66
|
+
# end
|
67
|
+
#
|
68
|
+
# === Import a User Action (Rate) from Your App (with synchronous/blocking
|
69
|
+
# requests)
|
70
|
+
# # PredictionIO call to record the view action
|
71
|
+
# begin
|
72
|
+
# result = client.record_user_action_on_item('rate', 'foouser',
|
73
|
+
# 'baritem',
|
74
|
+
# 'rating' => 4)
|
75
|
+
# rescue PredictionIO::EventClient::NotCreatedError => e
|
76
|
+
# ...
|
77
|
+
# end
|
78
|
+
class EventClient
|
79
|
+
# Raised when an event is not created after a synchronous API call.
|
80
|
+
class NotCreatedError < StandardError; end
|
81
|
+
|
82
|
+
# Create a new PredictionIO Event Client with defaults:
|
83
|
+
# - 1 concurrent HTTP(S) connections (threads)
|
84
|
+
# - API entry point at http://localhost:7070 (apiurl)
|
85
|
+
# - a 60-second timeout for each HTTP(S) connection (thread_timeout)
|
86
|
+
def initialize(access_key, apiurl = 'http://localhost:7070')
|
87
|
+
@access_key = access_key
|
88
|
+
@http = PredictionIO::Connection.new(URI(apiurl))
|
89
|
+
end
|
90
|
+
|
91
|
+
# Returns PredictionIO's status in string.
|
92
|
+
def get_status
|
93
|
+
status = @http.get(PredictionIO::Request.new('/'))
|
94
|
+
begin
|
95
|
+
status.body
|
96
|
+
rescue
|
97
|
+
status
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# Request to create an event and return the response.
|
102
|
+
#
|
103
|
+
# Corresponding REST API method: POST /events.json
|
104
|
+
def create_event(event, entity_type, entity_id, optional = {})
|
105
|
+
h = optional
|
106
|
+
h.key?('eventTime') || h['eventTime'] = DateTime.now.to_s
|
107
|
+
h['event'] = event
|
108
|
+
h['entityType'] = entity_type
|
109
|
+
h['entityId'] = entity_id
|
110
|
+
@http.post(PredictionIO::Request.new(
|
111
|
+
"/events.json?accessKey=#{@access_key}", h.to_json
|
112
|
+
))
|
113
|
+
end
|
114
|
+
|
115
|
+
# Request to set properties of a user and return the response.
|
116
|
+
#
|
117
|
+
# Corresponding REST API method: POST /events.json
|
118
|
+
def set_user(uid, optional = {})
|
119
|
+
create_event('$set', 'user', uid, optional)
|
120
|
+
end
|
121
|
+
|
122
|
+
# Request to unset properties of a user and return the response.
|
123
|
+
#
|
124
|
+
# properties must be a non-empty Hash.
|
125
|
+
#
|
126
|
+
# Corresponding REST API method: POST /events.json
|
127
|
+
def unset_user(uid, optional)
|
128
|
+
optional.key?('properties') ||
|
129
|
+
fail(ArgumentError, 'properties must be present when event is $unset')
|
130
|
+
optional['properties'].empty? &&
|
131
|
+
fail(ArgumentError, 'properties cannot be empty when event is $unset')
|
132
|
+
create_event('$unset', 'user', uid, optional)
|
133
|
+
end
|
134
|
+
|
135
|
+
# Request to delete a user and return the response.
|
136
|
+
#
|
137
|
+
# Corresponding REST API method: POST /events.json
|
138
|
+
def delete_user(uid)
|
139
|
+
create_event('$delete', 'user', uid)
|
140
|
+
end
|
141
|
+
|
142
|
+
# Request to set properties of an item and return the response.
|
143
|
+
#
|
144
|
+
# Corresponding REST API method: POST /events.json
|
145
|
+
def set_item(iid, optional = {})
|
146
|
+
create_event('$set', 'item', iid, optional)
|
147
|
+
end
|
148
|
+
|
149
|
+
# Request to unset properties of an item and return the response.
|
150
|
+
#
|
151
|
+
# properties must be a non-empty Hash.
|
152
|
+
#
|
153
|
+
# Corresponding REST API method: POST /events.json
|
154
|
+
def unset_item(iid, optional)
|
155
|
+
optional.key?('properties') ||
|
156
|
+
fail(ArgumentError, 'properties must be present when event is $unset')
|
157
|
+
optional['properties'].empty? &&
|
158
|
+
fail(ArgumentError, 'properties cannot be empty when event is $unset')
|
159
|
+
create_event('$unset', 'item', iid, optional)
|
160
|
+
end
|
161
|
+
|
162
|
+
# Request to delete an item and return the response.
|
163
|
+
#
|
164
|
+
# Corresponding REST API method: POST /events.json
|
165
|
+
def delete_item(uid)
|
166
|
+
create_event('$delete', 'item', uid)
|
167
|
+
end
|
168
|
+
|
169
|
+
# Request to record an action on an item and return the response.
|
170
|
+
#
|
171
|
+
# Corresponding REST API method: POST /events.json
|
172
|
+
def record_user_action_on_item(action, uid, iid, optional = {})
|
173
|
+
optional['targetEntityType'] = 'item'
|
174
|
+
optional['targetEntityId'] = iid
|
175
|
+
create_event(action, 'user', uid, optional)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module PredictionIO
|
2
|
+
# This class contains methods that allow you to export data for import though:
|
3
|
+
#
|
4
|
+
# $ pio import FILENAME
|
5
|
+
|
6
|
+
class FileExporter
|
7
|
+
|
8
|
+
def initialize(filename)
|
9
|
+
@filename = filename
|
10
|
+
@file = File.open(@filename, 'w')
|
11
|
+
end
|
12
|
+
|
13
|
+
def create_event(event, entity_type, entity_id, optional = {})
|
14
|
+
|
15
|
+
h = optional
|
16
|
+
h.key?('eventTime') || h['eventTime'] = DateTime.now.to_s
|
17
|
+
h['event'] = event
|
18
|
+
h['entityType'] = entity_type
|
19
|
+
h['entityId'] = entity_id
|
20
|
+
|
21
|
+
json = h.to_json
|
22
|
+
@file.write("#{json}\n")
|
23
|
+
end
|
24
|
+
|
25
|
+
def close
|
26
|
+
@file.close
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module PredictionIO
|
2
|
+
# This class contains the URI path and query parameters that is consumed by
|
3
|
+
# PredictionIO::Connection for asynchronous HTTP requests.
|
4
|
+
class Request
|
5
|
+
|
6
|
+
# The path portion of the request URI.
|
7
|
+
attr_reader :path
|
8
|
+
|
9
|
+
# Query parameters, or form data.
|
10
|
+
attr_reader :params
|
11
|
+
|
12
|
+
# Populates the package with request URI path, and optionally query
|
13
|
+
# parameters or form data.
|
14
|
+
def initialize(path, params = {})
|
15
|
+
@params = params
|
16
|
+
@path = path
|
17
|
+
end
|
18
|
+
|
19
|
+
# Returns an URI path with query parameters encoded for HTTP GET requests.
|
20
|
+
def qpath
|
21
|
+
"#{@path}?#{URI::encode_www_form(@params)}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: predictionio-simple
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yavor Stoychev
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
description: |
|
28
|
+
PredictionIO is an open source machine learning server for developers and data
|
29
|
+
scientists to create predictive engines for production environments. This gem
|
30
|
+
provides convenient access to the PredictionIO API for Ruby programmers so that
|
31
|
+
you can focus on application logic.
|
32
|
+
email: stoychev.yavor@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- lib/predictionio.rb
|
38
|
+
- lib/predictionio/connection.rb
|
39
|
+
- lib/predictionio/engine_client.rb
|
40
|
+
- lib/predictionio/event_client.rb
|
41
|
+
- lib/predictionio/file_exporter.rb
|
42
|
+
- lib/predictionio/request.rb
|
43
|
+
- lib/predictionio/version.rb
|
44
|
+
homepage: https://github.com/voran/incubator-predictionio-sdk-ruby-simple
|
45
|
+
licenses:
|
46
|
+
- Apache-2.0
|
47
|
+
metadata: {}
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 1.9.3
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 2.5.2
|
65
|
+
signing_key:
|
66
|
+
specification_version: 4
|
67
|
+
summary: PredictionIO Ruby SDK Simplified
|
68
|
+
test_files: []
|