Skip to content

Commit

Permalink
refactor to use init func to act as an async constructor instead of p…
Browse files Browse the repository at this point in the history
…assing response body in every method
  • Loading branch information
Jinwook Kim committed May 21, 2020
1 parent f1231ea commit 051fb10
Showing 1 changed file with 43 additions and 66 deletions.
109 changes: 43 additions & 66 deletions src/ContributionMetadata.js
Original file line number Diff line number Diff line change
@@ -1,159 +1,149 @@
const axios = require('axios');

class ContributionMetadata {
async init(link) {
this.responseBody = await this.getResponseBody(link);
}

/**
* Gets how many points is associated with your Local Guide Profile
* @public
* @param {string} body Entire web page body
* @return {string} # of points
*/
getPoints(body) {
getPoints() {
let pattern = /((\d|,)+) Points/g;
return this.getMatch(pattern, body);
return this.getMatch(pattern);
}

/**
* Gets what level you are on your Local Guide Profile
* @public
* @param {string} body Entire web page body
* @return {string} your level
*/
getLevel(body) {
getLevel() {
let pattern = /Level (\d+) Local Guide/g;
return this.getMatch(pattern, body);
return this.getMatch(pattern);
}

/**
* Gets how many reviews you have left associated with your Local Guide Profile
* @public
* @param {string} body Entire web page body
* @return {string} # of reviews
*/
getReviews(body) {
getReviews() {
let pattern = /(\d+) reviews/g;
return this.getMatch(pattern, body);
return this.getMatch(pattern);
}

/**
* Gets how many ratings you gave associated with your Local Guide Profile
* @public
* @param {string} body Entire web page body
* @return {string} # of ratings
*/
getRatings(body) {
getRatings() {
let pattern = /(\d+) ratings/g;
return this.getMatch(pattern, body);
return this.getMatch(pattern);
}

/**
* Gets how many questions you left associated with your Local Guide Profile
* @public
* @param {string} body Entire web page body
* @return {string} # of questions
*/
getQuestions(body) {
getQuestions() {
let pattern = /(\d+) answers/g;
return this.getMatch(pattern, body);
return this.getMatch(pattern);
}

/**
* Gets how many places you added associated with your Local Guide Profile
* @public
* @param {string} body Entire web page body
* @return {string} # of places added
*/
getPlacesAdded(body) {
getPlacesAdded() {
let pattern = /(\d+) places added/g;
return this.getMatch(pattern, body);
return this.getMatch(pattern);
}

/**
* Gets how many edits you made associated with your Local Guide Profile
* @public
* @param {string} body Entire web page body
* @return {string} # of edits
*/
getEdits(body) {
getEdits() {
let pattern = /(\d+) edits/g;
return this.getMatch(pattern, body);
return this.getMatch(pattern);
}

/**
* Gets how many facts you left associated with your Local Guide Profile
* @public
* @param {string} body Entire web page body
* @return {string} # of facts
*/
getFacts(body) {
getFacts() {
let pattern = /(\d+) facts/g;
return this.getMatch(pattern, body);
return this.getMatch(pattern);
}

/**
* Gets how many videos you uploaded associated with your Local Guide Profile
* @public
* @param {string} body Entire web page body
* @return {string} # of videos
*/
getVideos(body) {
getVideos() {
let pattern = /(\d+) videos/g;
return this.getMatch(pattern, body);
return this.getMatch(pattern);
}

/**
* Gets how many Q&As you answered associated with your Local Guide Profile
* @public
* @param {string} body Entire web page body
* @return {string} # of Q&As
*/
getQA(body) {
getQA() {
let pattern = /(\d+) Q\\\\u0026A/g;
return this.getMatch(pattern, body);
return this.getMatch(pattern);
}

/**
* Gets how many roads you added associated with your Local Guide Profile
* @public
* @return {string} # of roads
*/
getRoadsAdded(body) {
getRoadsAdded() {
let pattern = /(\d+) roads added/g;
return this.getMatch(pattern, body);
return this.getMatch(pattern);
}

/**
* Gets how many lists you published associated with your Local Guide Profile
* @public
* @param {string} body Entire web page body
* @return {string} # of published lists
*/
getPublishedLists(body) {
getPublishedLists() {
let pattern = /(\d+) published lists/g;
return this.getMatch(pattern, body);
return this.getMatch(pattern);
}

/**
* Gets all the metadata in one object
* @public
* @param {string} link Link to the contribution page
* @return {JSON} metadata
*/
async getMetadata(link) {
let body = await this.getResponseBody(link);

async getMetadata() {
return {
points: this.getPoints(body),
level: this.getLevel(body),
reviews: this.getReviews(body),
ratings: this.getRatings(body),
questions: this.getQuestions(body),
placesAdded: this.getPlacesAdded(body),
edits: this.getEdits(body),
facts: this.getFacts(body),
videos: this.getVideos(body),
QA: this.getQA(body),
roadsAdded: this.getRoadsAdded(body),
listsPublished: this.getPublishedLists(body)
points: this.getPoints(),
level: this.getLevel(),
reviews: this.getReviews(),
ratings: this.getRatings(),
questions: this.getQuestions(),
placesAdded: this.getPlacesAdded(),
edits: this.getEdits(),
facts: this.getFacts(),
videos: this.getVideos(),
qa: this.getQA(),
roadsAdded: this.getRoadsAdded(),
listsPublished: this.getPublishedLists()
}
}

Expand All @@ -164,8 +154,8 @@ class ContributionMetadata {
* @param {string} body Entire web page body
* @return {string} first group match result if found, empty string if not found.
*/
getMatch(pattern, body) {
let matches = pattern.exec(body);
getMatch(pattern) {
let matches = pattern.exec(this.responseBody);
return matches.length > 0 ? matches[1] : "";
}

Expand All @@ -181,16 +171,3 @@ class ContributionMetadata {
}

module.exports = ContributionMetadata;

async function run() {
let link = "https://www.google.com/maps/contrib/107673332497849211058/photos/@47.630274,-122.2373958,12z/data=!4m3!8m2!3m1!1e1";
let foo = new ContributionMetadata();
let data = foo.getMetadata(link);

return data;
}

run().then(data => {
console.log('data: ', data);
})

0 comments on commit 051fb10

Please sign in to comment.