-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom-giphy-image.js
65 lines (58 loc) · 1.31 KB
/
random-giphy-image.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* Core and general tools
*/
(function($, undefined) {
'use strict';
// Singleton
if (typeof window.RandomGiphyImage !== 'undefined') {
return;
}
//
// Module general vars
//
var
v = '1.00 beta',
debug = false,
data = {
api_key: 'dc6zaTOxFJmzC',
query: 'happy',
element_class: 'giphyme'
};
//
// Methods
//
// Adds Giphy gifs into the elements with class data.element_class
function giphyme() {
var elements = $('.' + data.element_class);
$.each(elements, function(key, value) {
if (this.debug) console.info(value);
updateGiphyImage($(value));
});
}
// Return Giphy gif URL string.
function updateGiphyImage(element) {
$.ajax({
method: 'GET',
url: 'http://api.giphy.com/v1/gifs/random',
data: {
api_key: data.api_key,
tag: data.query
},
success: function(response) {
if (this.debug) console.info(response.data.image_url);
element.html('<img src="' + response.data.image_url + '" alt="Gif via Giphy" />');
},
error: function() {
if (this.debug) console.info('Giphy Api call error.');
}
});
}
//
// Public methods / properties
//
window.RandomGiphyImage = {
debug: debug,
data: data,
giphyme: giphyme
};
}(jQuery));