├── .gitignore ├── .npmignore ├── DOCS.md ├── README.md ├── bin └── predictionio ├── index.js ├── lib ├── items.js ├── main.js └── users.js ├── package.json └── test └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | prediction.sublime-project 2 | prediction.sublime-workspace 3 | node_modules 4 | testfile.js 5 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | prediction.sublime-project 2 | prediction.sublime-workspace 3 | testfile.js 4 | -------------------------------------------------------------------------------- /DOCS.md: -------------------------------------------------------------------------------- 1 | ##Documentation 2 | 3 | The code within this module maps to utilise endpoints explained here: [PredictionIO](http://docs.prediction.io/current/apis/index.html) 4 | 5 | Using the module is straightforward: 6 | 7 | ```javascript 8 | var prediction = require('predictionio')({ 9 | key: YOUR_APP_KEY 10 | }) 11 | ``` 12 | 13 | By default the module will communicate with the PredictionIO server running locally on port 8000 14 | 15 | You can also specify to work against a different host 16 | 17 | ```javascript 18 | var prediction = require('predictionio')({ 19 | key: YOUR_APP_KEY 20 | , baseUrl: 'http://url.to.work.against' 21 | }) 22 | ``` 23 | 24 | The module works using both the traditional callback style and with promises. 25 | 26 | 27 | ###Create a User 28 | 29 | ```javascript 30 | prediction.users.create({ 31 | pio_uid: '14' 32 | , pio_inactive: false 33 | , pio_latlng: '51.5072,0.1275' 34 | , pio_any_string: 'Test' 35 | }, function (err, res) { 36 | console.log(err, res) 37 | }) 38 | 39 | prediction.users.create({ 40 | pio_uid: '14' 41 | , pio_inactive: false 42 | , pio_latlng: '51.5072,0.1275' 43 | , pio_any_string: 'Test' 44 | }).then(function (res) { 45 | console.log(res) 46 | }) 47 | ``` 48 | 49 | ###Fetch a User 50 | 51 | ```javascript 52 | prediction.users.get(14, function (err, res) { 53 | console.log(err, res) 54 | }) 55 | 56 | prediction.users.get(14).then(function (res) { 57 | console.log(res) 58 | }) 59 | ``` 60 | 61 | ###Remove a User 62 | 63 | ```javascript 64 | prediction.users.remove(14, function (err, res) { 65 | console.log(err, res) 66 | }) 67 | 68 | prediction.users.remove(14).then(function (res) { 69 | console.log(res) 70 | }) 71 | ``` 72 | 73 | ###Create an Item 74 | 75 | ```javascript 76 | prediction.items.create({ 77 | pio_iid: '15' 78 | , pio_itypes: 'Beep, Boop, Boom' 79 | , pio_latlng: '51.5072,0.1275' 80 | , pio_inactive: true 81 | , pio_startT: 1234567890000 82 | , pio_endT: 1234567890000 83 | , pio_price: 1 84 | , pio_profit: 1 85 | , pio_any_string: 'Test' 86 | }, function (err, res) { 87 | console.log(err, res) 88 | }) 89 | 90 | prediction.items.create({ 91 | pio_iid: '14' 92 | , pio_itypes: 'Beep, Boop, Boom' 93 | , pio_latlng: '51.5072,0.1275' 94 | , pio_inactive: true 95 | , pio_startT: 1234567890000 96 | , pio_endT: 1234567890000 97 | , pio_price: 1 98 | , pio_profit: 1 99 | , pio_any_string: 'Test' 100 | }).then(function (res) { 101 | console.log(res) 102 | }) 103 | ``` 104 | 105 | ###Fetch an Item 106 | 107 | ```javascript 108 | prediction.items.get(14, function (err, res) { 109 | console.log(err, res) 110 | }) 111 | 112 | prediction.items.get(14).then(function (res) { 113 | console.log(res) 114 | }) 115 | ``` 116 | 117 | ###Remove an Item 118 | 119 | ```javascript 120 | prediction.items.remove(14, function (err, res) { 121 | console.log(err, res) 122 | }) 123 | 124 | prediction.items.remove(14).then(function (res) { 125 | console.log(res) 126 | }) 127 | ``` 128 | 129 | ###Create a User to Item action 130 | 131 | ```javascript 132 | prediction.users.createAction({ 133 | pio_engine: 'test' 134 | , pio_uid: 14 135 | , pio_iid: 14 136 | , pio_action: 'like' 137 | , pio_rate: '5' 138 | , pio_latlng: '51.5072,0.1275' 139 | , pio_t: 1234567890000 140 | , pio_any_string: 'Test' 141 | }, function (err, res) { 142 | console.log(err, res) 143 | }) 144 | 145 | prediction.users.createAction({ 146 | pio_uid: 14 147 | , pio_iid: 14 148 | , pio_action: 'rate' 149 | , pio_rate: '5' 150 | , pio_latlng: '51.5072,0.1275' 151 | , pio_t: 1234567890000 152 | , pio_any_string: 'Test' 153 | }).then(function (res) { 154 | console.log(res) 155 | }) 156 | ``` 157 | 158 | ###Fetch User Recommendations 159 | 160 | ```javascript 161 | prediction.items.recommendation({ 162 | pio_uid: 14 163 | , pio_n: 20 164 | , pio_itypes: '' 165 | , pio_latlng: '' 166 | , pio_within: '' 167 | , pio_unit: '' 168 | , pio_attributes: '' 169 | }, function (err, res) { 170 | console.log(err, res) 171 | }) 172 | 173 | prediction.items.recommendation({ 174 | pio_uid: 14 175 | , pio_n: 20 176 | , pio_itypes: '' 177 | , pio_latlng: '' 178 | , pio_within: '' 179 | , pio_unit: '' 180 | , pio_attributes: '' 181 | }).then(function (res) { 182 | console.log(res) 183 | }) 184 | ``` 185 | 186 | ### Fetch Similar for User 187 | 188 | ```javascript 189 | prediction.items.similarity({ 190 | pio_uid: 14 191 | , pio_n: 20 192 | , pio_itypes: '' 193 | , pio_latlng: '' 194 | , pio_within: '' 195 | , pio_unit: '' 196 | , pio_attributes: '' 197 | }, function (err, res) { 198 | console.log(err, res) 199 | }) 200 | 201 | prediction.items.similarity({ 202 | pio_uid: 14 203 | , pio_n: 20 204 | , pio_itypes: '' 205 | , pio_latlng: '' 206 | , pio_within: '' 207 | , pio_unit: '' 208 | , pio_attributes: '' 209 | }).then(function (res) { 210 | console.log(res) 211 | }) 212 | ``` 213 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PredictionIO-NODE-SDK 2 | ===================== 3 | 4 | PredictionIO Node SDK 5 | 6 | ## Getting Started 7 | 8 | Install the module with: 9 | 10 | ```javascript 11 | npm install predictionio 12 | ``` 13 | 14 | ##Documentation 15 | 16 | See [Docs](DOCS.md) 17 | 18 | ## Contributing 19 | 20 | In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. 21 | 22 | ## Release History 23 | 24 | 0.0.2 Work with promises and callbacks, added basic tests 25 | 26 | 0.0.1 Basic functionality 27 | -------------------------------------------------------------------------------- /bin/predictionio: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | 4 | var prediction = require('../index.js') 5 | , path = require('path') 6 | , program = require('commander') 7 | , pkg = require('../package.json') 8 | , version = pkg.version 9 | 10 | 11 | program 12 | .version(version) 13 | .option('-d, --data ', 'Use data') 14 | .option('-k, --key ', 'App Key') 15 | 16 | 17 | program 18 | .command('createUser [uid]') 19 | .description('Create a new user') 20 | .option("-u, --uid [uid]", "UID for new user") 21 | .option("-i, --inactive [inactive]", "Inactive flag for user") 22 | .option("-l, --latlon [latlon]", "Lat/Lon pair for user") 23 | .option("-e, --extras [extras]", "Extra data to be stored with user") 24 | .action(function (env, options) { 25 | 26 | if (!options.parent.key) return console.log('App Key required') 27 | 28 | if (!options.uid) return console.log('UID is required') 29 | 30 | var api = new prediction(options.parent.key) 31 | , obj = {} 32 | 33 | obj.pio_uid = options.uid 34 | 35 | if (options.inactive) obj.pio_inactive = options.inactive 36 | 37 | if (options.latlon) obj.pio_latlng = options.latlon 38 | 39 | if (options.extras) obj.pio_any_string = options.extras 40 | 41 | api.users.create(obj, function (err, res) { 42 | 43 | console.log(err, res) 44 | 45 | }) 46 | 47 | }) 48 | 49 | 50 | program 51 | .command('getUser [uid]') 52 | .description('Fetch a user') 53 | .option("-u, --uid [uid]", "UID for user to fetch") 54 | .action(function (env, options) { 55 | 56 | if (!options.parent.key) return console.log('App Key required') 57 | 58 | if (!options.uid) return console.log('UID is required') 59 | 60 | var api = new prediction(options.parent.key) 61 | 62 | api.users.get(options.uid, function (err, res) { 63 | 64 | console.log(err, res) 65 | 66 | }) 67 | 68 | }) 69 | 70 | 71 | program 72 | .command('deleteUser [uid]') 73 | .description('Delete a user') 74 | .option("-u, --uid [uid]", "UID for user to delete") 75 | .action(function (env, options) { 76 | 77 | if (!options.parent.key) return console.log('App Key required') 78 | 79 | if (!options.uid) return console.log('UID is required') 80 | 81 | var api = new prediction(options.parent.key) 82 | 83 | api.users.remove(options.uid, function (err, res) { 84 | 85 | console.log(err, res) 86 | 87 | }) 88 | 89 | }) 90 | 91 | 92 | program 93 | .command('createItem [uid]') 94 | .description('Create a new item') 95 | .option("-u, --uid [uid]", "UID for new item") 96 | .option("-t, --types [types]", "Types for item") 97 | .option("-i, --inactive [inactive]", "Inactive flag for item") 98 | .option("-l, --latlon [latlon]", "Lat/Lon pair for item") 99 | .option("-x, --extras [extras]", "Extra data to be stored with item") 100 | .option("-s, --start [startdate]", "Start date for item") 101 | .option("-e, --end [enddate]", "End date for item") 102 | .option("-p, --price [price]", "Price for item") 103 | .option("--profit [profit]", "Profit for item") 104 | .action(function (env, options) { 105 | 106 | if (!options.parent.key) return console.log('App Key required') 107 | 108 | if (!options.uid) return console.log('UID is required') 109 | 110 | var api = new prediction(options.parent.key) 111 | , obj = {} 112 | 113 | obj.pio_uid = options.uid 114 | 115 | if (options.types) obj.pio_itypes = options.types 116 | 117 | if (options.inactive) obj.pio_inactive = options.inactive 118 | 119 | if (options.latlon) obj.pio_latlng = options.latlon 120 | 121 | if (options.extras) obj.pio_any_string = options.extras 122 | 123 | if (options.startdate) obj.pio_startT = options.startdate 124 | 125 | if (options.enddate) obj.pio_endT = options.enddate 126 | 127 | if (options.price) obj.pio_price = options.price 128 | 129 | if (options.profit) obj.pio_profit = options.profit 130 | 131 | api.items.create(obj, function (err, res) { 132 | 133 | console.log(err, res) 134 | 135 | }) 136 | 137 | }) 138 | 139 | 140 | program 141 | .command('getItem [uid]') 142 | .description('Fetch a user') 143 | .option("-u, --uid [uid]", "UID for item to fetch") 144 | .action(function (env, options) { 145 | 146 | if (!options.parent.key) return console.log('App Key required') 147 | 148 | if (!options.uid) return console.log('UID is required') 149 | 150 | var api = new prediction(options.parent.key) 151 | 152 | api.items.get(options.uid, function (err, res) { 153 | 154 | console.log(err, res) 155 | 156 | }) 157 | 158 | }) 159 | 160 | 161 | program 162 | .command('deleteItem [uid]') 163 | .description('Delete an item') 164 | .option("-u, --uid [uid]", "UID for item to delete") 165 | .action(function (env, options) { 166 | 167 | if (!options.parent.key) return console.log('App Key required') 168 | 169 | if (!options.uid) return console.log('UID is required') 170 | 171 | var api = new prediction(options.parent.key) 172 | 173 | api.items.remove(options.uid, function (err, res) { 174 | 175 | console.log(err, res) 176 | 177 | }) 178 | 179 | }) 180 | 181 | 182 | program 183 | .command('createAction [uid]') 184 | .description('Create a new action') 185 | .option("-u, --uid [uid]", "UID for new action") 186 | .option("-i, --itemuid [itemuid]", "ID of item") 187 | .option("-a, --action [action]", "Name of action") 188 | .option("-r, --rating [rating]", "Rating of action") 189 | .option("-l, --latlon [latlon]", "Lat/Lon pair for action") 190 | .option("-e, --extras [extras]", "Extra data to be stored with action") 191 | .option("-t, --timestamp [timestamp]", "Timestamp of action") 192 | .action(function (env, options) { 193 | 194 | if (!options.parent.key) return console.log('App Key required') 195 | 196 | if (!options.uid) return console.log('UID of user is required') 197 | 198 | if (!options.itemuid) return console.log('UID of item is required') 199 | 200 | var api = new prediction(options.parent.key) 201 | , obj = {} 202 | 203 | obj.pio_uid = options.uid 204 | 205 | if (options.itemuid) obj.pio_uid = options.uid 206 | 207 | if (options.action) obj.pio_action = options.action 208 | 209 | if (options.rating) obj.pio_rating = options.rating 210 | 211 | if (options.latlon) obj.pio_latlng = options.latlon 212 | 213 | if (options.timestamp) obj.pio_timestamp = options.timestamp 214 | 215 | if (options.extras) obj.pio_any_string = options.extras 216 | 217 | api.users.createAction(obj, function (err, res) { 218 | 219 | console.log(err, res) 220 | 221 | }) 222 | 223 | }) 224 | 225 | 226 | program 227 | .command('getRecommendations') 228 | .description('Create a new action') 229 | .option("-u, --uid [uid]", "UID for user") 230 | .option("-n, --results [results]", "Number of results") 231 | .option("-t, --types [types]", "Types") 232 | .option("-l, --latlon [latlon]", "Lat/Lon") 233 | .option("-w, --within [within]", "Within radius") 234 | .option("-u, --unit [unit]", "Unit") 235 | .option("-a, --attributes [attributes]", "Attributes") 236 | .action(function (env, options) { 237 | 238 | if (!options.parent.key) return console.log('App Key required') 239 | 240 | if (!options.uid) return console.log('UID of user is required') 241 | 242 | var api = new prediction(options.parent.key) 243 | , obj = {} 244 | 245 | obj.pio_uid = options.uid 246 | 247 | obj.pio_n = options.results || 10 248 | 249 | if (options.types) obj.pio_itypes = options.types 250 | 251 | if (options.latlon) obj.pio_latlng = options.latlon 252 | 253 | if (options.within) obj.pio_within = options.within 254 | 255 | if (options.unit) obj.pio_unit = options.unit 256 | 257 | if (options.attributes) obj.pio_attributes = options.attributes 258 | 259 | api.items.recommendation(obj, function (err, res) { 260 | 261 | console.log(err, res) 262 | 263 | }) 264 | 265 | }) 266 | 267 | 268 | program 269 | .command('getSimilar') 270 | .description('Get a similiar result set') 271 | .option("-u, --uid [uid]", "UID for user") 272 | .option("-n, --results [results]", "Number of results") 273 | .option("-t, --types [types]", "Types") 274 | .option("-l, --latlon [latlon]", "Lat/Lon") 275 | .option("-w, --within [within]", "Within radius") 276 | .option("-u, --unit [unit]", "Unit") 277 | .option("-a, --attributes [attributes]", "Attributes") 278 | .action(function (env, options) { 279 | 280 | if (!options.parent.key) return console.log('App Key required') 281 | 282 | if (!options.uid) return console.log('UID of user is required') 283 | 284 | var api = new prediction(options.parent.key) 285 | , obj = {} 286 | 287 | obj.pio_uid = options.uid 288 | 289 | obj.pio_n = options.results || 10 290 | 291 | if (options.types) obj.pio_itypes = options.types 292 | 293 | if (options.latlon) obj.pio_latlng = options.latlon 294 | 295 | if (options.within) obj.pio_within = options.within 296 | 297 | if (options.unit) obj.pio_unit = options.unit 298 | 299 | if (options.attributes) obj.pio_attributes = options.attributes 300 | 301 | api.items.recommendation(obj, function (err, res) { 302 | 303 | console.log(err, res) 304 | 305 | }) 306 | 307 | }) 308 | 309 | 310 | program.parse(process.argv) 311 | 312 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = function (config, version) { 2 | return require('./lib/main')(config, version) 3 | } 4 | -------------------------------------------------------------------------------- /lib/items.js: -------------------------------------------------------------------------------- 1 | var request = require('superagent') 2 | , when = require('when') 3 | 4 | 5 | function Items (obj) { 6 | 7 | this._config = obj._config 8 | this._createDeferred = obj._createDeferred 9 | 10 | } 11 | 12 | 13 | Items.prototype = { 14 | 15 | create: function (params, fn) { 16 | 17 | var deferred = this._createDeferred(fn) 18 | params.pio_appkey = this._config.key 19 | 20 | request 21 | .post(this._config.baseUrl + '/items.json') 22 | .send(params) 23 | .end(function (err, res) { 24 | 25 | if (err || !res || !res.body) deferred.reject(err) 26 | else deferred.resolve(res.body) 27 | 28 | }) 29 | 30 | return deferred.promise 31 | 32 | } 33 | 34 | , get: function (uid, fn) { 35 | 36 | var deferred = this._createDeferred(fn) 37 | , endpoint = this._config.baseUrl + '/items/' + uid + '.json' 38 | 39 | request 40 | .get(endpoint) 41 | .query({ pio_appkey: this._config.key }) 42 | .end(function (err, res) { 43 | 44 | if (err || !res || !res.body) deferred.reject(err) 45 | else deferred.resolve(res.body) 46 | 47 | }) 48 | 49 | return deferred.promise 50 | 51 | } 52 | 53 | , remove: function (uid, fn) { 54 | 55 | var deferred = this._createDeferred(fn) 56 | , endpoint = this._config.baseUrl + '/items/' + uid + '.json' 57 | 58 | request 59 | .del(endpoint) 60 | .send({ pio_appkey: this._config.key }) 61 | .end(function (err, res) { 62 | 63 | if (err || !res || !res.body) deferred.reject(err) 64 | else deferred.resolve(res.body) 65 | 66 | }) 67 | 68 | return deferred.promise 69 | 70 | } 71 | 72 | , similarity: function (params, fn) { 73 | 74 | var deferred = this._createDeferred(fn) 75 | 76 | if (!params.pio_engine) { 77 | deferred.reject('Engine must be supplied') 78 | return deferred.promise 79 | } 80 | 81 | params.pio_appkey = this._config.key 82 | 83 | var endpoint = this._config.baseUrl + '/engines/itemsim/' + params.pio_engine + '/topn.json' 84 | 85 | request 86 | .get(endpoint) 87 | .query({ 88 | pio_appkey: this._config.key 89 | , pio_iid: params.pio_iid 90 | , pio_n: params.pio_n || 10 91 | , pio_itypes: params.pio_itypes || '' 92 | , pio_latlng: params.pio_latlng || '' 93 | , pio_within: params.pio_within || '' 94 | , pio_unit: params.pio_unit || '' 95 | , pio_attributes: params.pio_attributes || '' 96 | }) 97 | .end(function (err, res) { 98 | 99 | if (err || !res || !res.body) deferred.reject(err) 100 | else deferred.resolve(res.body) 101 | 102 | }) 103 | 104 | return deferred.promise 105 | 106 | } 107 | 108 | , recommendation: function (params, fn) { 109 | 110 | var deferred = this._createDeferred(fn) 111 | 112 | if (!params.pio_engine) { 113 | deferred.reject('Engine must be supplied') 114 | return deferred.promise 115 | } 116 | 117 | params.pio_appkey = this._config.key 118 | 119 | var endpoint = this._config.baseUrl + '/engines/itemrec/' + params.pio_engine + '/topn.json' 120 | 121 | request 122 | .get(endpoint) 123 | .query({ 124 | pio_appkey: this._config.key 125 | , pio_uid: params.pio_uid 126 | , pio_n: params.pio_n || 10 127 | , pio_itypes: params.pio_itypes || '' 128 | , pio_latlng: params.pio_latlng || '' 129 | , pio_within: params.pio_within || '' 130 | , pio_unit: params.pio_unit || '' 131 | , pio_attributes: params.pio_attributes || '' 132 | }) 133 | .end(function (err, res) { 134 | 135 | if (err || !res || !res.body) deferred.reject(err) 136 | else deferred.resolve(res.body) 137 | 138 | }) 139 | 140 | return deferred.promise 141 | 142 | } 143 | 144 | } 145 | 146 | 147 | module.exports = Items 148 | -------------------------------------------------------------------------------- /lib/main.js: -------------------------------------------------------------------------------- 1 | var when = require('when') 2 | , resources = { 3 | 4 | users: require('./users') 5 | , items: require('./items') 6 | 7 | } 8 | 9 | 10 | function PredictionIO (config, version) { 11 | 12 | if (!config || !config.key) return new Error('App Key must be supplied') 13 | 14 | if (!(this instanceof PredictionIO)) { 15 | return new PredictionIO(config, version) 16 | } 17 | 18 | this._config = config 19 | 20 | if (!this._config.baseUrl || this._config.baseUrl === 'localhost') { 21 | this._config.baseUrl = 'http://127.0.0.1:8000' 22 | } 23 | 24 | this._prep() 25 | 26 | } 27 | 28 | 29 | PredictionIO.prototype = { 30 | 31 | _prep: function () { 32 | 33 | for (var name in resources) { 34 | 35 | this[ 36 | name[0].toLowerCase() + name.substring(1) 37 | ] = new resources[name](this) 38 | 39 | } 40 | 41 | } 42 | 43 | , _createDeferred: function(callback) { 44 | 45 | var deferred = when.defer() 46 | 47 | if (callback) { 48 | deferred.promise.then(function (res) { 49 | setTimeout(function(){ callback(null, res) }, 0) 50 | }, function (err) { 51 | setTimeout(function(){ callback(err, null) }, 0) 52 | }); 53 | } 54 | 55 | return deferred 56 | 57 | } 58 | 59 | } 60 | 61 | 62 | module.exports = PredictionIO 63 | -------------------------------------------------------------------------------- /lib/users.js: -------------------------------------------------------------------------------- 1 | var request = require('superagent') 2 | , when = require('when') 3 | 4 | 5 | function Users (obj) { 6 | 7 | this._config = obj._config 8 | this._createDeferred = obj._createDeferred 9 | 10 | } 11 | 12 | 13 | Users.prototype = { 14 | 15 | create: function (params, fn) { 16 | 17 | var deferred = this._createDeferred(fn) 18 | params.pio_appkey = this._config.key 19 | 20 | request 21 | .post(this._config.baseUrl + '/users.json') 22 | .send(params) 23 | .end(function (err, res) { 24 | 25 | if (err || !res || !res.body) deferred.reject(err) 26 | else deferred.resolve(res.body) 27 | 28 | }) 29 | 30 | return deferred.promise 31 | 32 | } 33 | 34 | , get: function (uid, fn) { 35 | 36 | var deferred = this._createDeferred(fn) 37 | , endpoint = this._config.baseUrl + '/users/' + uid + '.json' 38 | 39 | request 40 | .get(endpoint) 41 | .query({ pio_appkey: this._config.key }) 42 | .end(function (err, res) { 43 | 44 | if (err || !res || !res.body) deferred.reject(err) 45 | else deferred.resolve(res.body) 46 | 47 | }) 48 | 49 | return deferred.promise 50 | 51 | } 52 | 53 | , remove: function (uid, fn) { 54 | 55 | var deferred = this._createDeferred(fn) 56 | , endpoint = this._config.baseUrl + '/users/' + uid + '.json' 57 | 58 | request 59 | .del(endpoint) 60 | .send({ 61 | pio_appkey: this._config.key 62 | }) 63 | .end(function (err, res) { 64 | 65 | if (err || !res || !res.body) deferred.reject(err) 66 | else deferred.resolve(res.body) 67 | 68 | }) 69 | 70 | return deferred.promise 71 | 72 | } 73 | 74 | , createAction: function (params, fn) { 75 | 76 | var deferred = this._createDeferred(fn) 77 | params.pio_appkey = this._config.key 78 | 79 | request 80 | .post(this._config.baseUrl + '/actions/u2i.json') 81 | .send(params) 82 | .end(function (err, res) { 83 | 84 | if (err || !res || !res.body) deferred.reject(err) 85 | else deferred.resolve(res.body) 86 | 87 | }) 88 | 89 | return deferred.promise 90 | } 91 | 92 | } 93 | 94 | 95 | module.exports = Users 96 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "predictionio", 3 | "version": "0.0.7", 4 | "description": "PredictionIO Node SDK", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha --reporter spec" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/ShaunBaker/PredictionIO-Node-SDK.git" 12 | }, 13 | "homepage": "http://prediction.io", 14 | "author": "ShaunBaker", 15 | "license": "BSD-2-Clause", 16 | "bugs": { 17 | "url": "https://github.com/ShaunBaker/PredictionIO-Node-SDK/issues" 18 | }, 19 | "directories": { 20 | "test": "test" 21 | }, 22 | "devDependencies": { 23 | "mocha": "~1.13.0", 24 | "chai": "~1.8.0", 25 | "chai-as-promised": "~4.0.0", 26 | "mocha-as-promised": "~1.4.0" 27 | }, 28 | "dependencies": { 29 | "superagent": "~0.15.7", 30 | "commander": "~2.0.0", 31 | "when": "~2.5.1" 32 | }, 33 | "bin": { 34 | "predictionio": "./bin/predictionio" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | require('mocha-as-promised')() 2 | 3 | var chai = require('chai').use(require('chai-as-promised')) 4 | , prediction = require('../index.js') 5 | , key = 'DE3ymARmjiUKf2vL6fshw4nSzVqE3uiicKcejzpgWNKTVUJFs0IWoQmMV19atb9I' 6 | , domain 7 | , when = require('when') 8 | , expect = chai.expect 9 | , should = chai.should 10 | 11 | 12 | describe('Testing PredictionIO', function () { 13 | 14 | it('prediction should be a function', function () { 15 | expect(prediction).to.be.a('function') 16 | }) 17 | 18 | }) 19 | 20 | 21 | describe('Lets test PredicitonIO with App Key, but without a baseUrl', function () { 22 | 23 | var boot = new prediction({ 24 | key: key 25 | }) 26 | 27 | it('prediction should be a object', function () { 28 | expect(boot).to.be.an('object') 29 | }) 30 | 31 | describe('Lets test the configuration of PredicitonIO', function () { 32 | 33 | it('prediction should have a config object', function () { 34 | expect(boot._config).to.be.an('object') 35 | }) 36 | 37 | it('prediction._config.key should be a string', function () { 38 | expect(boot._config.key).to.be.an('string') 39 | }) 40 | 41 | it('prediction._config.key should be the same as the key used when initializing', function () { 42 | expect(boot._config.key).to.eql(key) 43 | }) 44 | 45 | }) 46 | 47 | }) 48 | 49 | 50 | describe('Lets test PredicitonIO with an App Key and baseUrl == localhost', function () { 51 | 52 | var boot = new prediction({ 53 | key: key 54 | , baseUrl: 'localhost' 55 | }) 56 | 57 | it('prediction should be a object', function () { 58 | expect(boot).to.be.an('object') 59 | }) 60 | 61 | describe('Lets test the configuration of PredicitonIO', function () { 62 | 63 | it('prediction should have a config object', function () { 64 | expect(boot._config).to.be.an('object') 65 | }) 66 | 67 | it('prediction._config.key should be a string', function () { 68 | expect(boot._config.key).to.be.an('string') 69 | }) 70 | 71 | it('prediction._config.key should be the same as the key used when initializing', function () { 72 | expect(boot._config.key).to.eql(key) 73 | }) 74 | 75 | it('prediction._config.baseUrl should not be the same as the URL used when initializing', function () { 76 | expect(boot._config.baseUrl).to.not.eql('localhost') 77 | expect(boot._config.baseUrl).to.eql('http://127.0.0.1:8000') 78 | }) 79 | 80 | }) 81 | 82 | }) 83 | 84 | 85 | describe('Lets test PredicitonIO with an App Key and baseUrl != localhost', function () { 86 | 87 | var boot = new prediction({ 88 | key: key 89 | , baseUrl: 'http://127.0.0.0:8000' 90 | }) 91 | 92 | it('prediction should be a object', function () { 93 | expect(boot).to.be.an('object') 94 | }) 95 | 96 | describe('Lets test the configuration of PredicitonIO', function () { 97 | 98 | it('prediction should have a config object', function () { 99 | expect(boot._config).to.be.an('object') 100 | }) 101 | 102 | it('prediction._config.key should be a string', function () { 103 | expect(boot._config.key).to.be.an('string') 104 | }) 105 | 106 | it('prediction._config.key should be the same as the key used when initializing', function () { 107 | expect(boot._config.key).to.eql(key) 108 | }) 109 | 110 | it('prediction._config.baseUrl should be the same as the URL used when initializing', function () { 111 | expect(boot._config.baseUrl).to.eql('http://127.0.0.0:8000') 112 | }) 113 | 114 | }) 115 | 116 | }) 117 | 118 | 119 | describe('Test User functions', function () { 120 | 121 | var boot = new prediction({ 122 | key: key 123 | , baseUrl: 'http://127.0.0.1:8000' 124 | }) 125 | 126 | it('prediction.users should be an object', function () { 127 | expect(boot.users).to.be.an('object') 128 | }) 129 | 130 | it('prediction.users.get should be a function', function () { 131 | expect(boot.users.get).to.be.a('function') 132 | }) 133 | 134 | it('prediction.users.remove should be a function', function () { 135 | expect(boot.users.remove).to.be.a('function') 136 | }) 137 | 138 | it('prediction.users.create should be a function', function () { 139 | expect(boot.users.create).to.be.a('function') 140 | }) 141 | 142 | describe('Test creating a user', function (done) { 143 | 144 | it('Run prediction.users.create (callback)', function (done) { 145 | 146 | boot.users.create({ 147 | pio_uid: '999' 148 | , pio_inactive: false 149 | , pio_latlng: '51.5072,0.1275' 150 | , pio_any_string: 'Test' 151 | }, function (err, res) { 152 | 153 | expect(err).to.be.null 154 | expect(res).to.be.an('object') 155 | expect(res.message).to.be.ok 156 | expect(res.message).to.be.a('string') 157 | done() 158 | 159 | }) 160 | 161 | }) 162 | 163 | it('Run prediction.users.create (promise)', function (done) { 164 | 165 | boot.users.create({ 166 | pio_uid: '999' 167 | , pio_inactive: false 168 | , pio_latlng: '51.5072,0.1275' 169 | , pio_any_string: 'Test' 170 | }).then(function (res) { 171 | 172 | expect(res).to.be.an('object') 173 | expect(res.message).to.be.ok 174 | expect(res.message).to.be.a('string') 175 | done() 176 | 177 | }) 178 | 179 | }) 180 | 181 | }) 182 | 183 | describe('Test fetching a user', function (done) { 184 | 185 | it('Run prediction.users.get (callback)', function (done) { 186 | 187 | boot.users.get(999, function (err, res) { 188 | 189 | expect(err).to.be.null 190 | expect(res).to.be.an('object') 191 | done() 192 | 193 | }) 194 | 195 | }) 196 | 197 | it('Run prediction.users.get (promise)', function (done) { 198 | 199 | boot.users.get(999).then(function (res) { 200 | 201 | expect(res).to.be.an('object') 202 | done() 203 | 204 | }) 205 | 206 | }) 207 | 208 | }) 209 | 210 | describe('Test removing a user', function (done) { 211 | 212 | it('Run prediction.users.remove (callback)', function (done) { 213 | 214 | boot.users.remove(14, function (err, res) { 215 | 216 | expect(err).to.be.null 217 | expect(res).to.be.an('object') 218 | done() 219 | 220 | }) 221 | 222 | }) 223 | 224 | it('Run prediction.users.remove (promise)', function (done) { 225 | 226 | boot.users.remove(14).then(function (res) { 227 | 228 | expect(res).to.be.an('object') 229 | done() 230 | 231 | }) 232 | 233 | }) 234 | 235 | }) 236 | 237 | describe('Test creating a user action', function (done) { 238 | 239 | it('Run prediction.users.createAction (callback)', function (done) { 240 | 241 | boot.users.createAction({ 242 | pio_uid: 999 243 | , pio_iid: 999 244 | , pio_action: 'rate' 245 | , pio_rate: '5' 246 | , pio_latlng: '51.5072,0.1275' 247 | , pio_t: 1234567890000 248 | , pio_any_string: 'Test' 249 | }, function (err, res) { 250 | 251 | expect(err).to.be.null 252 | expect(res).to.be.an('object') 253 | expect(res.message).to.be.ok 254 | expect(res.message).to.be.a('string') 255 | done() 256 | 257 | }) 258 | 259 | }) 260 | 261 | it('Run prediction.users.createAction (promise)', function (done) { 262 | 263 | boot.users.createAction({ 264 | pio_engine: 'test' 265 | , pio_uid: 999 266 | , pio_iid: 999 267 | , pio_action: 'rate' 268 | , pio_rate: '5' 269 | , pio_latlng: '51.5072,0.1275' 270 | , pio_t: 1234567890000 271 | , pio_any_string: 'Test' 272 | }).then(function (res) { 273 | 274 | expect(res).to.be.an('object') 275 | expect(res.message).to.be.ok 276 | expect(res.message).to.be.a('string') 277 | done() 278 | 279 | }) 280 | 281 | }) 282 | 283 | }) 284 | 285 | }) 286 | 287 | 288 | describe('Test Item functions', function () { 289 | 290 | var boot = new prediction({ 291 | key: key 292 | , baseUrl: 'http://127.0.0.0:8000' 293 | }) 294 | 295 | it('prediction.items should be an object', function () { 296 | expect(boot.items).to.be.an('object') 297 | }) 298 | 299 | it('prediction.items.get should be a function', function () { 300 | expect(boot.items.get).to.be.a('function') 301 | }) 302 | 303 | it('prediction.items.remove should be a function', function () { 304 | expect(boot.items.remove).to.be.a('function') 305 | }) 306 | 307 | it('prediction.items.create should be a function', function () { 308 | expect(boot.items.create).to.be.a('function') 309 | }) 310 | 311 | it('prediction.items.recommendation should be a function', function () { 312 | expect(boot.items.recommendation).to.be.a('function') 313 | }) 314 | 315 | it('prediction.items.similarity should be a function', function () { 316 | expect(boot.items.similarity).to.be.a('function') 317 | }) 318 | 319 | describe('Test creating an item', function (done) { 320 | 321 | var boot = new prediction({ 322 | key: key 323 | , baseUrl: 'http://127.0.0.1:8000' 324 | }) 325 | 326 | it('Run prediction.items.create (callback)', function (done) { 327 | 328 | boot.items.create({ 329 | pio_iid: '999' 330 | , pio_itypes: 'Beep, Boop, Boom' 331 | , pio_latlng: '51.5072,0.1275' 332 | , pio_inactive: true 333 | , pio_startT: 1234567890000 334 | , pio_endT: 1234567890000 335 | , pio_price: 1 336 | , pio_profit: 1 337 | , pio_any_string: 'Test' 338 | }, function (err, res) { 339 | 340 | expect(err).to.be.null 341 | expect(res).to.be.an('object') 342 | expect(res.message).to.be.ok 343 | expect(res.message).to.be.a('string') 344 | done() 345 | 346 | }) 347 | 348 | }) 349 | 350 | it('Run prediction.items.create (promise)', function (done) { 351 | 352 | boot.items.create({ 353 | pio_iid: '999' 354 | , pio_itypes: 'Beep, Boop, Boom' 355 | , pio_latlng: '51.5072,0.1275' 356 | , pio_inactive: true 357 | , pio_startT: 1234567890000 358 | , pio_endT: 1234567890000 359 | , pio_price: 1 360 | , pio_profit: 1 361 | , pio_any_string: 'Test' 362 | }).then(function (res) { 363 | 364 | expect(res).to.be.an('object') 365 | expect(res.message).to.be.ok 366 | expect(res.message).to.be.a('string') 367 | done() 368 | 369 | }) 370 | 371 | }) 372 | 373 | }) 374 | 375 | describe('Test fetching an item', function (done) { 376 | 377 | var boot = new prediction({ 378 | key: key 379 | , baseUrl: 'http://127.0.0.1:8000' 380 | }) 381 | 382 | it('Run prediction.items.get (callback)', function (done) { 383 | 384 | boot.items.get(999, function (err, res) { 385 | 386 | expect(err).to.be.null 387 | expect(res).to.be.an('object') 388 | done() 389 | 390 | }) 391 | 392 | }) 393 | 394 | it('Run prediction.items.get (promise)', function (done) { 395 | 396 | boot.items.get(999).then(function (res) { 397 | 398 | expect(res).to.be.an('object') 399 | done() 400 | 401 | }) 402 | 403 | }) 404 | 405 | }) 406 | 407 | describe('Test removing an item', function (done) { 408 | 409 | var boot = new prediction({ 410 | key: key 411 | , baseUrl: 'http://127.0.0.1:8000' 412 | }) 413 | 414 | it('Run prediction.items.remove (callback)', function (done) { 415 | 416 | boot.items.remove(999, function (err, res) { 417 | 418 | expect(err).to.be.null 419 | expect(res).to.be.an('object') 420 | done() 421 | 422 | }) 423 | 424 | }) 425 | 426 | it('Run prediction.items.remove (promise)', function (done) { 427 | 428 | boot.items.remove(999).then(function (res) { 429 | 430 | expect(res).to.be.an('object') 431 | done() 432 | 433 | }) 434 | 435 | }) 436 | 437 | }) 438 | 439 | 440 | describe('Test fetching recommendations', function (done) { 441 | 442 | var boot = new prediction({ 443 | key: key 444 | , baseUrl: 'http://127.0.0.1:8000' 445 | }) 446 | 447 | it('Run prediction.items.recommendation (callback)', function (done) { 448 | 449 | boot.items.recommendation({ 450 | pio_engine: 'test' 451 | , pio_uid: 999 452 | , pio_n: 20 453 | , pio_itypes: '' 454 | , pio_latlng: '' 455 | , pio_within: '' 456 | , pio_unit: '' 457 | , pio_attributes: '' 458 | }, function (err, res) { 459 | 460 | expect(err).to.be.null 461 | expect(res).to.be.an('object') 462 | done() 463 | 464 | }) 465 | 466 | }) 467 | 468 | it('Run prediction.items.recommendation (promise)', function (done) { 469 | 470 | boot.items.recommendation({ 471 | pio_engine: 'test' 472 | , pio_uid: 999 473 | , pio_n: 20 474 | , pio_itypes: '' 475 | , pio_latlng: '' 476 | , pio_within: '' 477 | , pio_unit: '' 478 | , pio_attributes: '' 479 | }).then(function (res) { 480 | 481 | expect(res).to.be.an('object') 482 | done() 483 | 484 | }) 485 | 486 | }) 487 | 488 | }) 489 | 490 | describe('Test fetching similarity', function (done) { 491 | 492 | var boot = new prediction({ 493 | key: key 494 | , baseUrl: 'http://127.0.0.1:8000' 495 | }) 496 | 497 | it('Run prediction.items.similarity (callback)', function (done) { 498 | 499 | boot.items.similarity({ 500 | pio_engine: 'test' 501 | , pio_iid: 999 502 | , pio_n: 20 503 | , pio_itypes: '' 504 | , pio_latlng: '' 505 | , pio_within: '' 506 | , pio_unit: '' 507 | , pio_attributes: '' 508 | }, function (err, res) { 509 | 510 | expect(err).to.be.null 511 | expect(res).to.be.an('object') 512 | done() 513 | 514 | }) 515 | 516 | }) 517 | 518 | it('Run prediction.items.similarity (promise)', function (done) { 519 | 520 | boot.items.similarity({ 521 | pio_engine: 'test' 522 | , pio_iid: 999 523 | , pio_n: 20 524 | , pio_itypes: '' 525 | , pio_latlng: '' 526 | , pio_within: '' 527 | , pio_unit: '' 528 | , pio_attributes: '' 529 | }).then(function (res) { 530 | 531 | expect(res).to.be.an('object') 532 | done() 533 | 534 | }) 535 | 536 | }) 537 | 538 | }) 539 | 540 | }) 541 | 542 | --------------------------------------------------------------------------------