// Endpoint URL
//
//
// https://runkit.io/makevoid/jasonette-1/branches/master
//
//
//
// the json is loaded in the app from that url, the code below generates json
// it fetches images from giphy, a cool thing would be to add a search bar and let the app call the endpoint with a parameter (q?, query) so that the user can search for any gif and it will make the app more complete
//
const giphy = require( 'giphy-api' )()
const c = console
// api
const getCats = giphy.search("cats")
// Array shuffle
Array.prototype.shuffle = function () {
this.forEach(
function (v, i, a) {
let j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
);
return this;
}
// endpoint
exports.endpoint = async function(request, response) {
// lib
let cats = await getCats
cats = cats.data
// test code
c.log(cats) // full response - note that these console logs, even if you uncomment them are not executed unless if you do exports.endpoint()
let cat_gifs = []
cats.forEach((cat) => {
// cat_gifs.push(cat.images.downsized_large.url) // max res but slow
cat_gifs.push(cat.images.downsized_medium.url) // low res but fast :D
})
// c.log(cat_gifs) // only large gifs
// cat_gifs.forEach((gif) => {
// c.log(gif)
// })
// it works, we have cat_gifs from last snippet (note the line numbers)
// c.log(cat_gifs)
// limit the gifs to 7
let limit = 7
cat_gifs = cat_gifs.shuffle() // randomize the gifs order
cat_gifs = cat_gifs.slice(0, limit-1)
let gifItems = cat_gifs.map( (gif) => {
return JSON.stringify(
{"type": "image", "url": gif}
)
} ).join(", ")
// c.log(gifItems) // must be valid json (when included in {}
response.end(`{
"$jason": {
"head": {
"title": "Josefine"
},
"body": {
"sections": [{
"items": [
{"type": "label", "text": "mao"},
${gifItems}
]
}]
}
}
} `);
}
// exports.endpoint() // uncomment this if you want to see the debug console logs, decomment if you want to use the actual endpoint ( https://runkit.io/makevoid/jasonette-1/branches/master in my case )