-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
149 lines (116 loc) · 3.19 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
var http = require('http');
var GiphyApiWrapper = function (api_key) {
this._base_url = 'api.giphy.com';
this._api_version = 'v1';
this._api_key = api_key;
};
// Endpoints //
// Limit max 100, default 25
GiphyApiWrapper.prototype.search = function (query, limit, offset, rating, callback) {
var query = query || null,
limit = limit || 25,
offset = offset || 0,
rating = rating || 'g',
path = 'gifs/search';
var args = new Array(
'q=' + query,
'limit=' + limit,
'offset=' + offset,
'rating=' + rating
).join('&');
if (query === null) {
callback(new Error('No query'), null);
return;
}
this._call(path, args, callback);
}
// Grab a single giphy image by its unique ID
GiphyApiWrapper.prototype.getById = function (id, callback) {
var path = 'gifs/' + id;
if (id === undefined) {
callback(new Error('ID is not set'), null);
return;
}
this._call(path, new Array(), callback);
}
// Grab many images by their IDs
GiphyApiWrapper.prototype.getByIds = function (ids, callback) {
var path = 'gifs',
ids = ids || null;
if (ids === null) {
callback(new Error('IDs are not set'), null);
return;
}
var args = 'ids=' + ids.join(',');
this._call(path, args, callback);
}
// ..
GiphyApiWrapper.prototype.translate = function (query, callback) {
var query = query || null,
path = 'gifs/translate';
if (query === null) {
callback(new Error('No query'), null);
return;
}
this._call(path, 's=' + query, callback);
}
// Grab a random image by the tag name
GiphyApiWrapper.prototype.random = function (tag, callback) {
var arg = 'tag=' + tag || null,
path = 'gifs/random';
this._call(path, arg, callback);
}
// Grab some trending images
GiphyApiWrapper.prototype.trending = function (limit, callback) {
var arg = 'limit=' + limit || null,
path = 'gifs/trending';
this._call(path, arg, callback);
}
// Internal //
// API call wrapper
GiphyApiWrapper.prototype._call = function (path, args, callback) {
var path = this._buildPath(path, args);
this._request(this._base_url, path, function (err, data) {
if (err) {
return callback(err, null);
}
return callback(null, JSON.parse(data));
});
}
GiphyApiWrapper.prototype._buildPath = function (path, args) {
var path = '/' + this._api_version + '/' + path;
path += '?api_key=' + this._api_key;
path += '&' + args;
return path;
}
GiphyApiWrapper.prototype._request = function (host, path, callback) {
var options = {
hostname: host,
port: 80,
path: path,
method: 'GET'
}
var req = http.request(options, function (res) {
res.setEncoding('utf8');
if (res.statusCode !== 200) {
console.log('error', res.statusCode);
return callback(new Error('Request failed'), null);
}
var data = [];
res.on('data', function (chunk) {
// Has chunk of data
data.push(chunk);
}).on('end', function () {
data_string = data.join('');
return callback(null, data_string);
});
});
// Catch error on request
req.on('error', function(e) {
return callback(new Error(e.message), null);
});
req.end();
}
module.exports = function (api_key) {
return new GiphyApiWrapper(api_key);
}