Skip to content

Instantly share code, notes, and snippets.

@lporras
Created August 14, 2017 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lporras/48a994bbfae63d4cc635b8f0671ae32e to your computer and use it in GitHub Desktop.
Save lporras/48a994bbfae63d4cc635b8f0671ae32e to your computer and use it in GitHub Desktop.
webtask giphy example
"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