Skip to content

Commit e947697

Browse files
committed
added override
1 parent 994f6b8 commit e947697

File tree

5 files changed

+335
-30
lines changed

5 files changed

+335
-30
lines changed

dist/breinify-api.js

Lines changed: 146 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13111,6 +13111,96 @@ dependencyScope.jQuery = $;;
1311113111

1311213112
var ATTR_CONFIG = BreinifyConfig.ATTRIBUTES;
1311313113

13114+
/**
13115+
* As JS don't supports data types, implements an overload method. This implementation is inspired by
13116+
* https://github.com/myfingersarebroken/aer
13117+
*/
13118+
function Wrapper() {
13119+
this.excludeNullType = function (type) {
13120+
return false;
13121+
};
13122+
13123+
this.toString = function (pointer) {
13124+
var output = '';
13125+
13126+
var keys = Object.keys(pointer);
13127+
keys.forEach(function (key) {
13128+
output += '[' + key + '] ';
13129+
});
13130+
13131+
return output;
13132+
};
13133+
}
13134+
13135+
Wrapper.prototype = {
13136+
setExcludeNullType: function (func) {
13137+
if (typeof func == 'function') {
13138+
this.excludeNullType = func;
13139+
}
13140+
},
13141+
13142+
overload: function (o, args, context) {
13143+
return this._$overload(o, args, context, this);
13144+
},
13145+
13146+
_$overload: function (pointer, args, context, wrapper) {
13147+
var regex = /function\s+(\w+)s*/;
13148+
var types = [];
13149+
13150+
// create a string to identify the structure of the signature
13151+
var containsRegEx = false;
13152+
for (var i = 0; i < args.length; i++) {
13153+
var arg = args[i];
13154+
13155+
var type;
13156+
if (arg === null) {
13157+
type = '([A-Za-z0-9_\\-]+)';
13158+
containsRegEx = true;
13159+
} else {
13160+
type = regex.exec(arg.constructor.toString())[1];
13161+
}
13162+
13163+
types.push(type);
13164+
}
13165+
13166+
// check which one of the functions can be used
13167+
var func = null;
13168+
if (containsRegEx) {
13169+
var typeRegEx = new RegExp(types.toString(), 'i');
13170+
13171+
Object.keys(pointer).forEach(function (key) {
13172+
var matches = typeRegEx.exec(key);
13173+
if (matches != null) {
13174+
var exclude = false;
13175+
for (var i = 1; i < matches.length; i++) {
13176+
if (wrapper.excludeNullType(matches[i])) {
13177+
exclude = true;
13178+
break;
13179+
}
13180+
}
13181+
13182+
if (exclude) {
13183+
// nothing to do
13184+
} else if (func === null) {
13185+
func = pointer[key];
13186+
} else {
13187+
throw new SyntaxError('Multiple signatures for (' + types.toString() + ') found in: ' + this.toString(pointer));
13188+
}
13189+
}
13190+
});
13191+
} else {
13192+
func = pointer[types.toString()];
13193+
}
13194+
if (typeof func !== 'function') {
13195+
throw new SyntaxError('Invalid signature (' + types.toString() + ') found, use one of: ' + this.toString(pointer));
13196+
}
13197+
13198+
return func.apply(context, args);
13199+
}
13200+
};
13201+
13202+
var overload = new Wrapper();
13203+
1311413204
/*
1311513205
* The internally used configuration used for all calls.
1311613206
*/
@@ -13212,6 +13302,7 @@ dependencyScope.jQuery = $;;
1321213302
return _config.all();
1321313303
};
1321413304

13305+
//noinspection JSCommentMatchesSignature,JSValidateJSDoc
1321513306
/**
1321613307
* Sends an activity to the Breinify server.
1321713308
*
@@ -13223,12 +13314,41 @@ dependencyScope.jQuery = $;;
1322313314
* @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)
1322413315
* @param onReady {function|null} function to be executed after triggering the activity
1322513316
*/
13226-
Breinify.activity = function (user, type, category, description, tags, sign, onReady) {
13317+
Breinify.activity = function () {
13318+
var url = _config.get(ATTR_CONFIG.URL) + _config.get(ATTR_CONFIG.ACTIVITY_ENDPOINT);
1322713319

13228-
Breinify.activityUser(user, type, category, description, tags, sign, function (data) {
13229-
var url = _config.get(ATTR_CONFIG.URL) + _config.get(ATTR_CONFIG.ACTIVITY_ENDPOINT);
13230-
_privates.ajax(url, data, onReady, onReady);
13231-
});
13320+
overload.overload({
13321+
'Object,String': function (user, type) {
13322+
Breinify.activityUser(user, type, null, null, null, false, function (data) {
13323+
_privates.ajax(url, data);
13324+
});
13325+
},
13326+
'Object,String,Object': function (user, type, tags) {
13327+
Breinify.activityUser(user, type, null, null, tags, false, function (data) {
13328+
_privates.ajax(url, data);
13329+
});
13330+
},
13331+
'Object,String,String,Object': function (user, type, description, tags) {
13332+
Breinify.activityUser(user, type, null, description, tags, false, function (data) {
13333+
_privates.ajax(url, data);
13334+
});
13335+
},
13336+
'Object,String,String,String,Object': function (user, type, category, description, tags) {
13337+
Breinify.activityUser(user, type, category, description, tags, false, function (data) {
13338+
_privates.ajax(url, data);
13339+
});
13340+
},
13341+
'Object,String,String,String,Object,Function': function (user, type, category, description, tags, callback) {
13342+
Breinify.activityUser(user, type, category, description, tags, false, function (data) {
13343+
_privates.ajax(url, data, callback, callback);
13344+
});
13345+
},
13346+
'Object,String,String,String,Object,Boolean,Function': function (user, type, category, description, tags, sign, callback) {
13347+
Breinify.activityUser(user, type, category, description, tags, sign, function (data) {
13348+
_privates.ajax(url, data, callback, callback);
13349+
});
13350+
}
13351+
}, arguments, this);
1323213352
};
1323313353

1323413354

@@ -13302,19 +13422,34 @@ dependencyScope.jQuery = $;;
1330213422
});
1330313423
};
1330413424

13425+
//noinspection JSCommentMatchesSignature,JSValidateJSDoc
1330513426
/**
1330613427
* Sends an temporalData request to the Breinify backend.
1330713428
*
1330813429
* @param user {object} the user-information
1330913430
* @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)
1331013431
* @param onReady {function|null} function to be executed after triggering the temporalData request
1331113432
*/
13312-
Breinify.temporalData = function (user, sign, onReady) {
13433+
Breinify.temporalData = function () {
13434+
var url = _config.get(ATTR_CONFIG.URL) + _config.get(ATTR_CONFIG.TEMPORAL_DATA_ENDPOINT);
1331313435

13314-
Breinify.temporalDataUser(user, sign, function (data) {
13315-
var url = _config.get(ATTR_CONFIG.URL) + _config.get(ATTR_CONFIG.TEMPORAL_DATA_ENDPOINT);
13316-
_privates.ajax(url, data, onReady, onReady);
13317-
});
13436+
overload.overload({
13437+
'Function': function (callback) {
13438+
Breinify.temporalDataUser({}, false, function (data) {
13439+
_privates.ajax(url, data, callback, callback);
13440+
});
13441+
},
13442+
'Boolean,Function': function (sign, callback) {
13443+
Breinify.temporalDataUser({}, sign, function (data) {
13444+
_privates.ajax(url, data, callback, callback);
13445+
});
13446+
},
13447+
'Object,Boolean,Function': function (user, sign, callback) {
13448+
Breinify.temporalDataUser(user, sign, function (data) {
13449+
_privates.ajax(url, data, callback, callback);
13450+
});
13451+
}
13452+
}, arguments, this);
1331813453
};
1331913454

1332013455
/**
@@ -13356,7 +13491,7 @@ dependencyScope.jQuery = $;;
1335613491

1335713492
var message = _privates.generateTemporalDataMessage(unixTimestamp, localDateTime, timezone);
1335813493
console.log(message);
13359-
console.log( _config.get(ATTR_CONFIG.SECRET));
13494+
console.log(_config.get(ATTR_CONFIG.SECRET));
1336013495
signature = _privates.determineSignature(message, _config.get(ATTR_CONFIG.SECRET))
1336113496
} else {
1336213497
_onReady(null);

dist/breinify-api.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

documentation/api.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This documentation is organized as following:
1818
* Breinify.setConfig(config)
1919
* [API](#api)
2020
* Breinify.activity(user, type, category, description, tags, sign, onReady)
21+
* Breinify.temporaldata(user, sign, onReady)
2122
* Breinify.lookup(user, dimensions, sign, onLookUp)
2223
* [Utilities (UTL)](#utilities-utl)
2324
* [Breinify.UTL (general functions)](#breinifyutl-general-functions)
@@ -27,6 +28,9 @@ This documentation is organized as following:
2728
* Breinify.UTL.md5(value)
2829
* Breinify.UTL.isEmpty(value)
2930
* Breinify.UTL.isSimpleObject(obj)
31+
* Breinify.UTL.unixTimestamp()
32+
* Breinify.UTL.timezone()
33+
* Breinify.UTL.localDateTime()
3034
* [Breinify.UTL.events](#breinifyutlevents)
3135
* Breinify.UTL.events.click(selector, func, onlyOnce)
3236
* Breinify.UTL.events.pageloaded(func)
@@ -79,6 +83,8 @@ This documentation is organized as following:
7983

8084
{string} **apiKey**: The API-key to be used (mandatory).
8185

86+
{string} **temporaldataEndpoint**: The end-point of the API to retrieve temporal-data results.
87+
8288
{string} **lookupEndpoint**: The end-point of the API to retrieve lookup results.
8389

8490
{string} **secret**: The secret attached to the API-key (should always be null utilizing this type of library).
@@ -129,7 +135,7 @@ This documentation is organized as following:
129135

130136
{boolean|null} **sign**: A boolean value specifying if the call should be signed, which is only available if the *secret* is configured. It is strongly advised not to use a signed call when utilizing this library.
131137

132-
{function|null} **onReady**: A function which is triggered after the activity was sent to the user. The function has the information sent as the first parameter.
138+
{function|null} **onReady**: A function which is triggered after the activity was sent. The function has the retrieved answer as the first parameter.
133139

134140
**Example Usage**:
135141
```javascript
@@ -145,6 +151,26 @@ This documentation is organized as following:
145151
```
146152
<br/>
147153

154+
* **Breinify.temporaldata(user, sign, onReady)**:<br/>
155+
Retrieves temporal information about the passed user information.
156+
157+
**Parameters**:
158+
159+
{object} **user**: A plain object specifying the user information the temporal data should be retrieved for. More information about the structure can be found [here](./user.md).
160+
161+
{boolean|null} **sign**: A boolean value specifying if the call should be signed, which is only available if the *secret* is configured. It is strongly advised not to use a signed call when utilizing this library.
162+
163+
{function|null} **onReady**: A function which is triggered after the answer of the call was received. The function has the retrieved information as first parameter.
164+
165+
**Example Usage**:
166+
```
167+
Breinify.temporalData({}, false, function (data) {
168+
console.log(data);
169+
});
170+
```
171+
<br/>
172+
173+
148174
* **Breinify.lookup(user, dimensions, sign, onLookUp)**:<br/>
149175
Retrieves a lookup result from the engine. The function needs a valid API-key to be configured to succeed.
150176

@@ -269,6 +295,15 @@ The utility library provides general functionality, which makes it easy to retri
269295
'null': null
270296
}
271297
```
298+
299+
* {number} **Breinify.UTL.unixTimestamp()**:<br/>
300+
Returns the current unix time-stamp (also called epoch time).
301+
302+
* {string} **Breinify.UTL.timezone()**:<br/>
303+
Determines the clients timezone.
304+
305+
* {string} **Breinify.UTL.localDateTime()**:<br/>
306+
Creates a string representing the current local date and time.
272307

273308
##### Breinify.UTL.events
274309

sample/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ <h1>Current Configuration</h1>
266266
var product = Breinify.UTL.text(Breinify.UTL.select(this, 'td:first'), false);
267267
Breinify.activity({
268268
'email': userEmail
269-
}, 'selectProduct', null, product, false, function () {
269+
}, 'selectProduct', null, product, null, false, function () {
270270
show('Sent activity "selectProduct" with product "' + product + '".');
271271
});
272272
}
@@ -287,7 +287,7 @@ <h1>Current Configuration</h1>
287287
if (!Breinify.UTL.isEmpty(userEmail)) {
288288
Breinify.activity({
289289
'email': userEmail
290-
}, 'login', null, null, false, function () {
290+
}, 'login', null, null, null, false, function () {
291291
lookUp(userEmail);
292292
show('Sent activity "login" with user "' + userEmail + '".');
293293
});
@@ -314,10 +314,10 @@ <h1>Current Configuration</h1>
314314
Breinify.UTL.setText('#dimension-email td:nth-child(2)', val);
315315
});
316316
}
317-
}
317+
};
318318

319319
var temporalData = function () {
320-
Breinify.temporalData({}, false, function (data) {
320+
Breinify.temporalData({}, null, function (data) {
321321

322322
var $template = $('#temporal-data-template');
323323

0 commit comments

Comments
 (0)