Skip to content

Commit ca473e4

Browse files
committed
added recommendation
1 parent e113f30 commit ca473e4

File tree

5 files changed

+252
-12
lines changed

5 files changed

+252
-12
lines changed

dist/breinify-api.js

Lines changed: 109 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* breinify-api
3-
* v1.0.7
3+
* v1.0.8
44
**/
55
/*
66
* We inject a dependencyScope variable, which will be used
@@ -12686,6 +12686,13 @@ dependencyScope.jQuery = $;;
1268612686
return value !== null && typeof value === 'string' && '' !== value.trim() && value.charAt(0) === '/';
1268712687
}
1268812688
});
12689+
attributes.add('RECOMMENDATION_ENDPOINT', {
12690+
name: 'recommendationEndpoint',
12691+
defaultValue: '/recommendation',
12692+
validate: function (value) {
12693+
return value !== null && typeof value === 'string' && '' !== value.trim() && value.charAt(0) === '/';
12694+
}
12695+
});
1268912696
attributes.add('CATEGORY', {
1269012697
name: 'category',
1269112698
defaultValue: 'other',
@@ -12728,7 +12735,7 @@ dependencyScope.jQuery = $;;
1272812735
});
1272912736

1273012737
var BreinifyConfig = function (config) {
12731-
this.version = '1.0.7';
12738+
this.version = '1.0.8';
1273212739

1273312740
/*
1273412741
* Validate the passed config-parameters.
@@ -12894,7 +12901,7 @@ dependencyScope.jQuery = $;;
1289412901

1289512902
var BreinifyUser = function (user, onReady) {
1289612903
var instance = this;
12897-
instance.version = '1.0.7';
12904+
instance.version = '1.0.8';
1289812905

1289912906
// set the values provided
1290012907
instance.setAll(user);
@@ -13132,6 +13139,7 @@ dependencyScope.jQuery = $;;
1313213139
};
1313313140
}
1313413141

13142+
//noinspection JSUnusedGlobalSymbols
1313513143
Wrapper.prototype = {
1313613144
setExcludeNullType: function (func) {
1313713145
if (typeof func == 'function') {
@@ -13184,15 +13192,15 @@ dependencyScope.jQuery = $;;
1318413192
} else if (func === null) {
1318513193
func = pointer[key];
1318613194
} else {
13187-
throw new SyntaxError('Multiple signatures for (' + types.toString() + ') found in: ' + this.toString(pointer));
13195+
throw new SyntaxError('Multiple signatures for (' + types.toString() + ') found in: ' + pointer.toString());
1318813196
}
1318913197
}
1319013198
});
1319113199
} else {
1319213200
func = pointer[types.toString()];
1319313201
}
1319413202
if (typeof func !== 'function') {
13195-
throw new SyntaxError('Invalid signature (' + types.toString() + ') found, use one of: ' + this.toString(pointer));
13203+
throw new SyntaxError('Invalid signature (' + types.toString() + ') found, use one of: ' + pointer.toString());
1319613204
}
1319713205

1319813206
return func.apply(context, args);
@@ -13249,6 +13257,10 @@ dependencyScope.jQuery = $;;
1324913257
return type + unixTimestamp + amount;
1325013258
},
1325113259

13260+
generateRecommendationMessage: function (amount, unixTimestamp) {
13261+
return unixTimestamp + amount;
13262+
},
13263+
1325213264
generateLookUpMessage: function (dimensions, unixTimestamp) {
1325313265
dimensions = $.isArray(dimensions) ? dimensions : [];
1325413266
var dimension = dimensions.length === 0 ? '' : dimensions[0];
@@ -13267,7 +13279,7 @@ dependencyScope.jQuery = $;;
1326713279
* The one and only instance of the library.
1326813280
*/
1326913281
var Breinify = {
13270-
version: '1.0.7',
13282+
version: '1.0.8',
1327113283
jQueryVersion: $.fn.jquery
1327213284
};
1327313285

@@ -13302,6 +13314,97 @@ dependencyScope.jQuery = $;;
1330213314
return _config.all();
1330313315
};
1330413316

13317+
//noinspection JSCommentMatchesSignature,JSValidateJSDoc
13318+
/**
13319+
* Sends an recommendation request to the Breinify server.
13320+
*
13321+
* @param user {object} the user-information
13322+
* @param nrOfRecommendations {number|null} the amount of recommendations to get
13323+
* @param sign {boolean|null} true if a signature should be added (needs the secret to be configured - not recommended in open systems), otherwise false (can be null or undefined)
13324+
* @param onReady {function|null} unction to be executed after triggering the recommendation request
13325+
*/
13326+
Breinify.recommendation = function () {
13327+
var url = _config.get(ATTR_CONFIG.URL) + _config.get(ATTR_CONFIG.RECOMMENDATION_ENDPOINT);
13328+
13329+
overload.overload({
13330+
'Object,Function': function (user, callback) {
13331+
Breinify.recommendationUser(user, 3, false, function (data) {
13332+
_privates.ajax(url, data, callback, callback);
13333+
});
13334+
},
13335+
'Object,Number,Function': function (user, nrOfRecommendations, callback) {
13336+
Breinify.recommendationUser(user, nrOfRecommendations, false, function (data) {
13337+
_privates.ajax(url, data, callback, callback);
13338+
});
13339+
},
13340+
'Object,Number,Boolean,Function': function (user, nrOfRecommendations, sign, callback) {
13341+
Breinify.recommendationUser(user, nrOfRecommendations, sign, function (data) {
13342+
_privates.ajax(url, data, callback, callback);
13343+
});
13344+
}
13345+
}, arguments, this);
13346+
};
13347+
13348+
/**
13349+
* Creates a user instance and executes the specified method.
13350+
*
13351+
* @param user {object} the user-information
13352+
* @param nrOfRecommendations {number|null} the amount of recommendations to get
13353+
* @param sign {boolean|null} true if a signature should be added (needs the secret to be configured - not recommended in open systems), otherwise false (can be null or undefined)
13354+
* @param onReady {function|null} function to be executed after successful user creation
13355+
*/
13356+
Breinify.recommendationUser = function (user, nrOfRecommendations, sign, onReady) {
13357+
13358+
var _onReady = function (user) {
13359+
if ($.isFunction(onReady)) {
13360+
onReady(user);
13361+
}
13362+
};
13363+
13364+
// get the user information
13365+
new BreinifyUser(user, function (user) {
13366+
13367+
if (!user.validate()) {
13368+
_onReady(null);
13369+
return;
13370+
}
13371+
13372+
// get some default values for the passed parameters - if not set
13373+
sign = typeof sign === 'boolean' ? sign : false;
13374+
13375+
// get the other values needed
13376+
var unixTimestamp = BreinifyUtil.unixTimestamp();
13377+
var signature = null;
13378+
if (sign) {
13379+
var secret = _config.get(ATTR_CONFIG.SECRET);
13380+
if (typeof secret === 'string') {
13381+
var message = _privates.generateRecommendationMessage(1, unixTimestamp, type);
13382+
signature = _privates.determineSignature(message, _config.get(ATTR_CONFIG.SECRET))
13383+
} else {
13384+
_onReady(null);
13385+
return;
13386+
}
13387+
}
13388+
13389+
// create the data set
13390+
var data = {
13391+
'user': user.all(),
13392+
13393+
'recommendation': {
13394+
'numRecommendations': nrOfRecommendations
13395+
},
13396+
13397+
'apiKey': _config.get(ATTR_CONFIG.API_KEY),
13398+
'signature': signature,
13399+
'unixTimestamp': unixTimestamp
13400+
};
13401+
13402+
if ($.isFunction(onReady)) {
13403+
_onReady(data);
13404+
}
13405+
});
13406+
};
13407+
1330513408
//noinspection JSCommentMatchesSignature,JSValidateJSDoc
1330613409
/**
1330713410
* Sends an activity to the Breinify server.
@@ -13351,7 +13454,6 @@ dependencyScope.jQuery = $;;
1335113454
}, arguments, this);
1335213455
};
1335313456

13354-
1335513457
/**
1335613458
* Creates a user instance and executes the specified method.
1335713459
*

dist/breinify-api.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/index.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ <h1>LookUp Result</h1>
137137
</tr>
138138
</table>
139139

140+
<h1>Recommendation Result</h1>
141+
<table data-sort="true">
142+
<tr>
143+
<th>Recommendations</th>
144+
</tr>
145+
<tr id="recommendation-template" style="display: none; visibility: hidden;">
146+
<td></td>
147+
<td></td>
148+
</tr>
149+
</table>
150+
140151
<h1>TemporalData Result</h1>
141152
<table data-sort="true">
142153
<tr>
@@ -338,6 +349,31 @@ <h1>Current Configuration</h1>
338349
});
339350
};
340351

352+
var recommendation = function () {
353+
Breinify.recommendation({'email': 'philipp.meisen@breinify.com'}, 1, function (data) {
354+
var recommendations;
355+
if ($.isPlainObject(data) && $.isArray(data.result)) {
356+
recommendations = data.result;
357+
} else {
358+
recommendations = [];
359+
}
360+
361+
var $template = $('#recommendation-template');
362+
363+
$.each(recommendations, function(idx, recommendation) {
364+
var $row = $template.clone()
365+
.attr("id", "")
366+
.appendTo($template.parent())
367+
.css("visibility", "visible")
368+
.show();
369+
370+
$row.children("td:nth-child(1)").html("<pre><code>" +
371+
JSON.stringify(recommendation, null, 2) + "</code></pre>");
372+
});
373+
});
374+
};
375+
376+
recommendation();
341377
temporalData();
342378
</script>
343379

src/Breinify.js

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
};
4040
}
4141

42+
//noinspection JSUnusedGlobalSymbols
4243
Wrapper.prototype = {
4344
setExcludeNullType: function (func) {
4445
if (typeof func == 'function') {
@@ -91,15 +92,15 @@
9192
} else if (func === null) {
9293
func = pointer[key];
9394
} else {
94-
throw new SyntaxError('Multiple signatures for (' + types.toString() + ') found in: ' + this.toString(pointer));
95+
throw new SyntaxError('Multiple signatures for (' + types.toString() + ') found in: ' + pointer.toString());
9596
}
9697
}
9798
});
9899
} else {
99100
func = pointer[types.toString()];
100101
}
101102
if (typeof func !== 'function') {
102-
throw new SyntaxError('Invalid signature (' + types.toString() + ') found, use one of: ' + this.toString(pointer));
103+
throw new SyntaxError('Invalid signature (' + types.toString() + ') found, use one of: ' + pointer.toString());
103104
}
104105

105106
return func.apply(context, args);
@@ -156,6 +157,10 @@
156157
return type + unixTimestamp + amount;
157158
},
158159

160+
generateRecommendationMessage: function (amount, unixTimestamp) {
161+
return unixTimestamp + amount;
162+
},
163+
159164
generateLookUpMessage: function (dimensions, unixTimestamp) {
160165
dimensions = $.isArray(dimensions) ? dimensions : [];
161166
var dimension = dimensions.length === 0 ? '' : dimensions[0];
@@ -209,6 +214,97 @@
209214
return _config.all();
210215
};
211216

217+
//noinspection JSCommentMatchesSignature,JSValidateJSDoc
218+
/**
219+
* Sends an recommendation request to the Breinify server.
220+
*
221+
* @param user {object} the user-information
222+
* @param nrOfRecommendations {number|null} the amount of recommendations to get
223+
* @param sign {boolean|null} true if a signature should be added (needs the secret to be configured - not recommended in open systems), otherwise false (can be null or undefined)
224+
* @param onReady {function|null} unction to be executed after triggering the recommendation request
225+
*/
226+
Breinify.recommendation = function () {
227+
var url = _config.get(ATTR_CONFIG.URL) + _config.get(ATTR_CONFIG.RECOMMENDATION_ENDPOINT);
228+
229+
overload.overload({
230+
'Object,Function': function (user, callback) {
231+
Breinify.recommendationUser(user, 3, false, function (data) {
232+
_privates.ajax(url, data, callback, callback);
233+
});
234+
},
235+
'Object,Number,Function': function (user, nrOfRecommendations, callback) {
236+
Breinify.recommendationUser(user, nrOfRecommendations, false, function (data) {
237+
_privates.ajax(url, data, callback, callback);
238+
});
239+
},
240+
'Object,Number,Boolean,Function': function (user, nrOfRecommendations, sign, callback) {
241+
Breinify.recommendationUser(user, nrOfRecommendations, sign, function (data) {
242+
_privates.ajax(url, data, callback, callback);
243+
});
244+
}
245+
}, arguments, this);
246+
};
247+
248+
/**
249+
* Creates a user instance and executes the specified method.
250+
*
251+
* @param user {object} the user-information
252+
* @param nrOfRecommendations {number|null} the amount of recommendations to get
253+
* @param sign {boolean|null} true if a signature should be added (needs the secret to be configured - not recommended in open systems), otherwise false (can be null or undefined)
254+
* @param onReady {function|null} function to be executed after successful user creation
255+
*/
256+
Breinify.recommendationUser = function (user, nrOfRecommendations, sign, onReady) {
257+
258+
var _onReady = function (user) {
259+
if ($.isFunction(onReady)) {
260+
onReady(user);
261+
}
262+
};
263+
264+
// get the user information
265+
new BreinifyUser(user, function (user) {
266+
267+
if (!user.validate()) {
268+
_onReady(null);
269+
return;
270+
}
271+
272+
// get some default values for the passed parameters - if not set
273+
sign = typeof sign === 'boolean' ? sign : false;
274+
275+
// get the other values needed
276+
var unixTimestamp = BreinifyUtil.unixTimestamp();
277+
var signature = null;
278+
if (sign) {
279+
var secret = _config.get(ATTR_CONFIG.SECRET);
280+
if (typeof secret === 'string') {
281+
var message = _privates.generateRecommendationMessage(1, unixTimestamp, type);
282+
signature = _privates.determineSignature(message, _config.get(ATTR_CONFIG.SECRET))
283+
} else {
284+
_onReady(null);
285+
return;
286+
}
287+
}
288+
289+
// create the data set
290+
var data = {
291+
'user': user.all(),
292+
293+
'recommendation': {
294+
'numRecommendations': nrOfRecommendations
295+
},
296+
297+
'apiKey': _config.get(ATTR_CONFIG.API_KEY),
298+
'signature': signature,
299+
'unixTimestamp': unixTimestamp
300+
};
301+
302+
if ($.isFunction(onReady)) {
303+
_onReady(data);
304+
}
305+
});
306+
};
307+
212308
//noinspection JSCommentMatchesSignature,JSValidateJSDoc
213309
/**
214310
* Sends an activity to the Breinify server.
@@ -258,7 +354,6 @@
258354
}, arguments, this);
259355
};
260356

261-
262357
/**
263358
* Creates a user instance and executes the specified method.
264359
*

src/BreinifyConfig.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@
4444
return value !== null && typeof value === 'string' && '' !== value.trim() && value.charAt(0) === '/';
4545
}
4646
});
47+
attributes.add('RECOMMENDATION_ENDPOINT', {
48+
name: 'recommendationEndpoint',
49+
defaultValue: '/recommendation',
50+
validate: function (value) {
51+
return value !== null && typeof value === 'string' && '' !== value.trim() && value.charAt(0) === '/';
52+
}
53+
});
4754
attributes.add('CATEGORY', {
4855
name: 'category',
4956
defaultValue: 'other',

0 commit comments

Comments
 (0)