Last active
April 11, 2018 12:05
-
-
Save teswar/75485b66986cb02ea44d712dc7e375fd to your computer and use it in GitHub Desktop.
Node JS Tryouts | Fetching all search result page result, sorting by ascending and dumping to console. Node JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**** | |
* fetching all pages of search result ... | |
* Node JS tryouts.... | |
* https://repl.it/@teswar/fetchallpagesofjsonmocksearchresult | |
************/ | |
const https = require('https'); | |
/* | |
* compose url with query params appended... | |
*/ | |
function composeURL(searchText, pageNumber) { | |
return `https://jsonmock.hackerrank.com/api/movies/search/?${[['Title', searchText], ['page', pageNumber]].filter(m => (m[1])).map(m => m.join('=')).join('&')}`; | |
} | |
/* | |
* returns a promise to resolving json response ... | |
*/ | |
function fetchJson(url) { | |
return new Promise((resolve, reject) => { | |
https.get(url, res => { | |
var body = ""; | |
res.setEncoding("utf8"); | |
res.on("data", (data) => body += data); | |
res.on("end", () => resolve(JSON.parse(body))); | |
}) | |
.on('error', (e) => reject(e)) | |
}); | |
} | |
/* | |
* returns a ALWAYS successfully resolving promise. | |
* wraps rejected result to resolve null.. | |
*/ | |
function tryFetchJson(url) { | |
return fetchJson(url).then((data) => data, (error) => Promise.resolve(null)); | |
} | |
/* | |
* fetches all pages and returns a them as a promise. | |
*/ | |
async function fetchUntilDead(substr) { | |
var metadata = metadata || { | |
_isCompleted: function() { | |
return !!this.page && this.page >= this.total_pages; | |
} | |
}; | |
while(!metadata._isCompleted()) { | |
var data = null; | |
if(data = await tryFetchJson(composeURL('spider', (metadata.page || 0) + 1))) { | |
data.data = data.data.map(m => m.Title); | |
data.page = +(data.page || 0) // parsing to number.... | |
Array.prototype.push.apply((metadata.data = metadata.data || []), data.data); | |
delete data['data']; | |
Object.assign(metadata, data); | |
} | |
} | |
return metadata; | |
} | |
async function getMovieTitles(substr) { | |
console.log((await fetchUntilDead(substr)).data.sort((a, b) => a.localeCompare(b))); | |
} | |
/* | |
* invoking.... | |
*/ | |
getMovieTitles('spider'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
live @ https://repl.it/@teswar/fetchallpagesofjsonmocksearchresult