-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgimmebar.js
87 lines (77 loc) · 1.95 KB
/
gimmebar.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
/* Code from GimmePlop. http://www.gimmeplop.com/ https://github.com/fictivekin/gimmeplop.com */
var request = require('request');
var qs = require('querystring');
var APIversion = 'v1';
var baseURL = 'https://gimmebar.com/api/' + APIversion;
function Gimme (username) {
if (username) {
this.username = username;
} else {
this.username = 'andreas';
}
}
Gimme.prototype = {
setUsername: function (username) {
this.username = username || 'andreas';
},
setMaxRecords: function (total) {
this.maxRecords = total || 0;
},
getPublicAssets: function (callback, opts) {
var opts = extendObj({
'limit': 50,
'skip': 0,
'type': 'embed'
}, opts);
var url = baseURL + '/public/assets/' + this.username + '?' + qs.stringify(opts);
console.log(url);
request.get({ 'url': url }, function (error, r, body) {
if (error) {
callback(error, null);
} else {
callback(null, JSON.parse(body));
}
});
},
getAsset: function (callback, opts) {
var url = baseURL + '/public/asset/' + opts.id;
request.get({ 'url': url }, function (error, r, body) {
if (error) {
callback(error, null);
} else {
callback(null, JSON.parse(body));
}
});
},
getAssetFromCollection: function(callback, opts) {
// This gets and asset from the Library, not the collection for the time being
var collection_slug = opts.slug;
var opts = extendObj({
'limit': 50,
'skip': 0,
'type': 'embed'
}, opts);
var url = baseURL + '/public/assets/' + this.username + '/' + collection_slug + '?' + qs.stringify(opts);
console.log(url);
request.get({ 'url': url }, function (error, r, body){
if (error) {
callback(error, null);
} else {
callback(null, JSON.parse(body));
}
});
}
};
function extendObj (org, overwrite) {
var extended = org;
if (overwrite) {
for (var key in org) {
if (overwrite[key]) {
extended[key] = overwrite[key];
}
}
}
return extended;
}
exports.extendObj = extendObj;
exports.Gimme = Gimme;