jasonette-random-doge

node v6.17.1
version: 30.0.0
endpointsharetweet
// jasonette-random-doge runkit Jasonette (native!) application // 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 // config // try other search terms // // const searchTerm = "wow" // const searchTerm = "cat" const searchTerm = "doge" // api const getGifs = giphy.search(searchTerm) // 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 gif_objs = await getGifs gif_objs = gif_objs.data // test code c.log(gif_objs) // full response - note that these console logs, even if you uncomment them are not executed unless if you do exports.endpoint() let gifs = [] gif_objs.forEach((gif) => { // gifs.push(gif.images.downsized_large.url) // max res but slow gifs.push(gif.images.downsized_medium.url) // low res but fast :D }) // c.log(gifs) // only large gifs // gifs.forEach((gif) => { // c.log(gif) // }) // it works, we have gifs from last snippet (note the line numbers) // c.log(gifs) // limit the gifs to 7 let limit = 7 gifs = gifs.shuffle() // randomize the gifs order gifs = gifs.slice(0, limit-1) let gifItems = 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": "RandomDoge" }, "body": { "header": { "title": "RandomDoge", "style": { "shy": true } }, "sections": [{ "items": [ {"type": "label", "text": "Welcome to the RandomDoge app! Enjoy some random Doge from Giphy."}, { "type": "button", "text": "Moar Doge!", "style": { "width": "50", "height": "50", "background": "#999EE", "spacing": "80" }, "action": { "type": "$reload" } }, ${gifItems}, {"type": "label", "text": "Built with Jasonette and Runkit in <1h - @makevoid"} ], "style": { "spacing": "80" } }] } } } `); } // 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 )
Loading…

no comments

    sign in to comment