Skip to content

Commit

Permalink
Added Firebase connector (#2)
Browse files Browse the repository at this point in the history
* Added Firebase connector
* Added empty line
* Updated library to 1.1.0 version
  • Loading branch information
pamelazagatti authored and Pamela Zagatti committed Mar 8, 2019
1 parent 42cd970 commit 630ab03
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@myplanet/multivariate",
"version": "1.0.0",
"version": "1.1.0",
"description": "A Javascript multivariate testing library",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/Multivariate.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ class Multivariate {
Multivariate.IGNORED_IP_ADDRESSES = [];
Multivariate.ROBOT_REGEX = /$^|trivial|facebook|MetaURI|butterfly|google|amazon|goldfire|sleuth|xenu|msnbot|SiteUptime|Slurp|WordPress|ZIBB|ZyBorg|pingdom|bot|yahoo|slurp|java|fetch|spider|url|crawl|oneriot|abby|commentreader|twiceler/i;

module.exports = Multivariate;
module.exports = Multivariate;
64 changes: 64 additions & 0 deletions src/connector/FirebaseConnector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const Connector = require('./Connector');

class FirebaseConnector extends Connector {
constructor(firebaseClient, rootRef) {
super();
this.firebaseClient = firebaseClient;
this.rootRef = rootRef;
}

getReference(key, field) {
const path = field ? `${key}/${field}` : `${key}`;
return this.firebaseClient.ref(this.rootRef).child(path);
}

async get(type, key, field) {
const experimentsStatsRef = this.getReference(key, field);
return experimentsStatsRef.once('value')
.then(dataSnapshot => {
return dataSnapshot.val();
});
}

async set(type, key, field, value) {
return value
? this.getReference(key).update({ [field]: value })
: this.getReference(key, field).remove();
}

async increment(type, key, field, amount = 1) {
const experimentsStatsRef = this.getReference(key);
const childRef = experimentsStatsRef.child(field);
const child = await childRef.once('value');
if (child.val) {
const currentVal = child.val();
return experimentsStatsRef.update({ [field]: currentVal + amount });
} else {
return this.save(type, key, { [field]: amount });
}

}

async save(type, key, saveData) {
return this.getReference(key).set(saveData);
}

async load(type, key) {
const experimentsStatsRef = this.getReference(key);
return experimentsStatsRef.once('value')
.then(dataSnapshot => {
return dataSnapshot.val();
});
}

async reset(type, key, ...fields) {
const saveData = fields.reduce((res, el) => res[field] = 0, {});
return this.getReference(key).set(saveData);
}

async delete(type, key) {
return this.getReference(key).remove();
}
}

module.exports = FirebaseConnector;
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const Multivariate = require('./Multivariate');
const Connector = require('./connector/Connector');
const RedisConnector = require('./connector/RedisConnector');
const FirebaseConnector = require('./connector/FirebaseConnector');

module.exports = {
Multivariate: Multivariate,
Connector: Connector,
RedisConnector: RedisConnector
RedisConnector: RedisConnector,
FirebaseConnector: FirebaseConnector
}

0 comments on commit 630ab03

Please sign in to comment.