Created
August 14, 2017 16:31
-
-
Save lporras/48a994bbfae63d4cc635b8f0671ae32e to your computer and use it in GitHub Desktop.
webtask giphy example
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
"use latest"; | |
var handlebars = require('handlebars'); | |
var request = require('request'); | |
var View = ` | |
<html> | |
<head> | |
<title>Giphy</title> | |
</head> | |
<body> | |
<h1>Giphy test</h1> | |
{{#if image_url}} | |
<img src="{{image_url}}"/> | |
{{else}} | |
<h1>No Gif found :(</h1> | |
{{/if}} | |
</body> | |
</html> | |
`; | |
return (ctx, req, res) => { | |
const q = ctx.data.q; | |
const GiphySearchURL = "https://api.giphy.com/v1/gifs/search?" | |
const url = GiphySearchURL + "api_key=" + ctx.data.API_KEY + "&q=" + q; | |
request(url, function (error, response, body) { | |
const template = handlebars.compile(View); | |
const json_response = JSON.parse(body); | |
let view_ctx; | |
if (json_response.data.length > 0) { | |
view_ctx = { | |
image_url: json_response.data[0]["images"]["downsized_large"]["url"], | |
}; | |
} else { | |
view_ctx = { | |
image_url: null, | |
}; | |
} | |
res.writeHead(200, { 'Content-Type': 'text/html' }); | |
res.end(template(view_ctx)); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment