forked from austinkelleher/giphy-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
257 lines (229 loc) · 7.76 KB
/
index.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* global process */
var http = require('http');
var queryString = require('querystring');
/**
* Hostname of the Giphy API
*/
var API_HOSTNAME = 'api.giphy.com';
/**
* Base PATH of the Giphy API
*/
var API_BASE_PATH = '/v1/';
/**
* Public API key provided by Giphy for anyone to use. This is used as a fallback
* if no API key is provided
*/
var PUBLIC_BETA_API_KEY = 'dc6zaTOxFJmzC';
/**
* Define if the environment is a browser
*/
var ENV_IS_BROWSER = process.browser || false;
/**
* @param apiKey Giphy API key. Defaults to the public beta API key
*/
var GiphyAPI = function(apiKey) {
this.apiKey = apiKey || PUBLIC_BETA_API_KEY;
};
GiphyAPI.prototype = {
/**
* Search all Giphy gifs by word or phrase
*
* @param options Giphy API search options
* q - search query term or phrase
* limit - (optional) number of results to return, maximum 100. Default 25.
* offset - (optional) results offset, defaults to 0.
* rating - limit results to those rated (y,g, pg, pg-13 or r).
* fmt - (optional) return results in html or json format (useful for viewing responses as GIFs to debug/test)
* @param callback
*/
search: function(options, callback) {
if (!options) {
if (callback) {
return callback('Search phrase cannot be empty.');
} else {
return Promise.reject('Search phrase cannot be empty.');
}
}
return this._request({
api: options.api || 'gifs',
endpoint: 'search',
query: typeof(options) === 'string' ? {
q: options
} : options
}, callback);
},
/**
* Search all Giphy gifs for a single Id or an array of Id's
*
* @param id Single Giphy gif string Id or array of string Id's
* @param callback
*/
id: function(id, callback) {
var idIsArr = Array.isArray(id);
if (!id || (idIsArr && id.length === 0)) {
if (callback) {
return callback('Id required for id API call');
} else {
return Promise.reject('Id required for id API call');
}
}
// If an array of Id's was passed, generate a comma delimited string for
// the query string.
if (idIsArr) {
id = id.join();
}
return this._request({
api: 'gifs',
query: {
ids: id
}
}, callback);
},
/**
* Search for Giphy gifs by phrase with Gify vocabulary
*
* @param options Giphy API translate options
* s - term or phrase to translate into a GIF
* rating - limit results to those rated (y,g, pg, pg-13 or r).
* fmt - (optional) return results in html or json format (useful for viewing responses as GIFs to debug/test)
*/
translate: function(options, callback) {
if (!options) {
if (callback) {
return callback('Translate phrase cannot be empty.');
} else {
return new Promise.reject('Translate phrase cannot be empty.');
}
}
return this._request({
api: options.api || 'gifs',
endpoint: 'translate',
query: typeof(options) === 'string' ? {
s: options
} : options
}, callback);
},
/**
* Fetch random gif filtered by tag
*
* @param options Giphy API random options
* tag - the GIF tag to limit randomness by
* rating - limit results to those rated (y,g, pg, pg-13 or r).
* fmt - (optional) return results in html or json format (useful for viewing responses as GIFs to debug/test)
*/
random: function(options, callback) {
var reqOptions = {
api: (options ? options.api : null) || 'gifs',
endpoint: 'random'
};
if (typeof(options) === 'string') {
reqOptions.query = {
tag: options
};
} else if (typeof(options) === 'object') {
reqOptions.query = options;
} else if (typeof(options) === 'function') {
callback = options;
}
return this._request(reqOptions, callback);
},
/**
* Fetch trending gifs
*
* @param options Giphy API random options
* limit (optional) limits the number of results returned. By default returns 25 results.
* rating - limit results to those rated (y,g, pg, pg-13 or r).
* fmt - (optional) return results in html or json format (useful for viewing responses as GIFs to debug/test)
*/
trending: function(options, callback) {
var reqOptions = {
endpoint: 'trending'
};
reqOptions.api = (options ? options.api : null) || 'gifs';
//Cleanup so we don't add this to our query
if (options) {
delete options.api;
}
if (typeof options === 'object' &&
Object.keys(options).length !== 0) {
reqOptions.query = options;
} else if (typeof options === 'function') {
callback = options;
}
return this._request(reqOptions, callback);
},
/**
* Prepares the HTTP request and query string for the Giphy API
*
* @param options
* - endpoint {{string}} The API endpoint e.g. search
* - query {{string/object}} Query string parameters. If these are left
* out then we default to an empty string. If this is passed as a string,
* we default to the 'q' query string field used by Giphy.
*/
_request: function(options, callback) {
var endpoint = '';
if (options.endpoint) {
endpoint = '/' + options.endpoint;
}
endpoint += '?';
var query;
var self = this;
if (typeof(options.query) !== 'undefined') {
if (typeof(options.query) === 'object') {
if (Object.keys(options.query).length === 0) {
if (callback) {
return callback('Options object should not be empty');
}
return Promise.reject('Options object should not be empty');
}
options.query.api_key = this.apiKey;
query = queryString.stringify(options.query);
}
} else {
query = queryString.stringify({
api_key: self.apiKey
});
}
var requestOptions = {
hostname: API_HOSTNAME,
path: API_BASE_PATH + options.api + endpoint + query,
withCredentials: !ENV_IS_BROWSER
};
var makeRequest = function(resolve, reject) {
http.get(requestOptions, function(response) {
var body = '';
response.on('data', function(d) {
body += d;
});
response.on('end', function() {
if (!options.query || options.query.fmt !== 'html') {
body = JSON.parse(body);
}
resolve(body);
});
}).on('error', function(err) {
reject(err);
});
};
if (callback) {
var resolve = function(res) {
callback(null, res);
};
var reject = function(err) {
callback(err);
};
makeRequest(resolve, reject);
} else {
if (typeof Promise === 'undefined') {
throw new Error('Callback must be provided unless Promises are available');
}
return new Promise(function(resolve, reject) {
makeRequest(resolve, reject);
});
}
}
};
module.exports = function(apiKey) {
return new GiphyAPI(apiKey);
};