Skip to content

Commit

Permalink
Add some refactoring and fix some bugs
Browse files Browse the repository at this point in the history
Signed-off-by: sharon-wang <[email protected]>
  • Loading branch information
sharon-wang committed Mar 21, 2021
1 parent 5123ce1 commit 887289e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 42 deletions.
19 changes: 8 additions & 11 deletions src/data/AkitaOriginData.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* See ./example_data.json to see example instances of AkitaOriginData.
*/
class AkitaOriginData {
constructor(originString) {
constructor(originString, faviconSourceString = null, originVisitData = new AkitaOriginVisitData()) {
this.origin = originString;
this.originVisitData = new AkitaOriginVisitData();
this.faviconSource = null;
this.faviconSource = faviconSourceString;
this.originVisitData = originVisitData;

// The type of each entry in paymentPointerMap is: AkitaPaymentPointerData
this.paymentPointerMap = {};
Expand All @@ -27,21 +27,18 @@ class AkitaOriginData {
* @return {AkitaOriginData} the input object as an instance of the AkitaOriginData class.
*/
static fromObject(akitaOriginDataObject) {
const newOriginData = new AkitaOriginData(akitaOriginDataObject.origin);
newOriginData.faviconSource = akitaOriginDataObject.faviconSource;
const newOriginData = new AkitaOriginData(
akitaOriginDataObject.origin,
akitaOriginDataObject.faviconSource,
AkitaOriginVisitData.fromObject(akitaOriginDataObject.originVisitData)
);

for (const paymentPointer in akitaOriginDataObject.paymentPointerMap) {
newOriginData.paymentPointerMap[paymentPointer] = AkitaPaymentPointerData.fromObject(
akitaOriginDataObject.paymentPointerMap[paymentPointer]
);
}

// Add deserialization for originVisitData
const originVisitDataDeserialized = AkitaOriginVisitData.fromObject(akitaOriginDataObject.originVisitData);
if (originVisitDataDeserialized) {
newOriginData.originVisitData = originVisitDataDeserialized;
}

return newOriginData;
}

Expand Down
18 changes: 9 additions & 9 deletions src/data/AkitaOriginStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
* - map of totalSentAssets, with an entry for each currency
*/
class AkitaOriginStats {
constructor() {
this.totalTimeSpent = 0;
this.totalMonetizedTimeSpent = 0;
this.totalVisits = 0;
constructor(totalTimeSpentNumber = 0, totalMonetizedTimeSpentNumber = 0, totalVisitsNumber = 0) {
this.totalTimeSpent = totalTimeSpentNumber;
this.totalMonetizedTimeSpent = totalMonetizedTimeSpentNumber;
this.totalVisits = totalVisitsNumber;

// The type of each entry in totalSentAssetsMap is: WebMonetizationAsset
this.totalSentAssetsMap = {};
Expand All @@ -29,11 +29,11 @@ class AkitaOriginStats {
* @return {AkitaOriginStats} the input object as an instance of the AkitaOriginStats class.
*/
static fromObject(akitaOriginStats) {
const newAkitaOriginStats = new AkitaOriginStats();

newAkitaOriginStats.totalTimeSpent = akitaOriginStats.totalTimeSpent;
newAkitaOriginStats.totalMonetizedTimeSpent = akitaOriginStats.totalMonetizedTimeSpent;
newAkitaOriginStats.totalVisits = akitaOriginStats.totalVisits;
const newAkitaOriginStats = new AkitaOriginStats(
akitaOriginStats.totalTimeSpent,
akitaOriginStats.totalMonetizedTimeSpent,
akitaOriginStats.totalVisits
);

for (const assetCode in akitaOriginStats.totalSentAssetsMap) {
newAkitaOriginStats.totalSentAssetsMap[assetCode] = WebMonetizationAsset.fromObject(
Expand Down
17 changes: 7 additions & 10 deletions src/data/AkitaOriginVisitData.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
* - number of visits recorded in Akita
*/
class AkitaOriginVisitData {
constructor() {
constructor(monetizedTimeSpentNumber = 0, numberOfVisitsNumber = 0) {
// time in milliseconds
this.monetizedTimeSpent = 0;
this.monetizedTimeSpent = monetizedTimeSpentNumber;

this.numberOfVisits = 0;
this.numberOfVisits = numberOfVisitsNumber;
}

/**
Expand All @@ -24,13 +24,10 @@ class AkitaOriginVisitData {
* @return {AkitaOriginVisitData} the input object as an instance of the AkitaOriginVisitData class.
*/
static fromObject(akitaOriginVisitDataObject) {
let newOriginVisitData = null;

if (akitaOriginVisitDataObject) {
newOriginVisitData = new AkitaOriginVisitData();
newOriginVisitData.monetizedTimeSpent = akitaOriginVisitDataObject.monetizedTimeSpent;
newOriginVisitData.numberOfVisits = akitaOriginVisitDataObject.numberOfVisits;
}
const newOriginVisitData = new AkitaOriginVisitData(
akitaOriginVisitDataObject.monetizedTimeSpent,
akitaOriginVisitDataObject.numberOfVisits
);

return newOriginVisitData;
}
Expand Down
12 changes: 8 additions & 4 deletions src/data/AkitaPaymentPointerData.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
* sent to that payment pointer. An asset is some amount of a currencies like USD, CAD or XRP.
*/
class AkitaPaymentPointerData {
constructor(paymentPointerString) {
constructor(paymentPointerString, validationTimestampNumber = null) {
this.paymentPointer = paymentPointerString;

// The most recent time (UTC timestamp) when Akita validated the payment pointer
// For more info on payment pointer validation: see ../content_origin.js, function isPaymentPointerValid
this.validationTimestamp = null;
// This is a Number, but is initialized to null to signify that the payment pointer has not yet been validated
this.validationTimestamp = validationTimestampNumber;

// The type of each entry in sentAssetsMap is: WebMonetizationAsset
this.sentAssetsMap = {};
Expand All @@ -23,14 +24,17 @@ class AkitaPaymentPointerData {
* @return {AkitaPaymentPointerData} the input object as an instance of the AkitaPaymentPointerData class.
*/
static fromObject(akitaPaymentPointerDataObject) {
const newPaymentPointerData = new AkitaPaymentPointerData(akitaPaymentPointerDataObject.paymentPointer);
newPaymentPointerData.validationTimestamp = akitaPaymentPointerDataObject.validationTimestamp;
const newPaymentPointerData = new AkitaPaymentPointerData(
akitaPaymentPointerDataObject.paymentPointer,
akitaPaymentPointerDataObject.validationTimestamp
);

for (const assetCode in akitaPaymentPointerDataObject.sentAssetsMap) {
newPaymentPointerData.sentAssetsMap[assetCode] = WebMonetizationAsset.fromObject(
akitaPaymentPointerDataObject.sentAssetsMap[assetCode]
);
}

return newPaymentPointerData;
}

Expand Down
17 changes: 9 additions & 8 deletions src/data/WebMonetizationAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
* 235 * 10**(-2) = 2.35
*/
class WebMonetizationAsset {
constructor(assetCode) {
this.assetCode = assetCode;
this.amount = 0;
this.assetCode = null;
constructor(assetCodeString, amountNumber = 0, assetScaleNumber = 0) {
this.assetCode = assetCodeString;
this.amount = amountNumber;
this.assetScale = assetScaleNumber;
}

/**
Expand All @@ -26,10 +26,11 @@ class WebMonetizationAsset {
* @return {WebMonetizationAsset} the input object as an instance of the WebMonetizationAsset class.
*/
static fromObject(webMonetizationAsset) {
const newWebMonetizationAsset = new WebMonetizationAsset(webMonetizationAsset.assetCode);

newWebMonetizationAsset.amount = webMonetizationAsset.amount;
newWebMonetizationAsset.assetScale = webMonetizationAsset.assetScale;
const newWebMonetizationAsset = new WebMonetizationAsset(
webMonetizationAsset.assetCode,
webMonetizationAsset.amount,
webMonetizationAsset.assetScale
);

return newWebMonetizationAsset;
}
Expand Down

0 comments on commit 887289e

Please sign in to comment.