-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreddit.js
More file actions
55 lines (42 loc) · 1.29 KB
/
reddit.js
File metadata and controls
55 lines (42 loc) · 1.29 KB
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
// Use request JSON for quick JSON loading
var requestJson = require('request-json');
// Create a new Meetup API client
var client = requestJson.createClient("https://reddit.com");
module.exports = {
// Returns a base parameter object with smart defaults
// These can be overridden on the routing level
baseParameter : function() {
return {
sub : "all",
sort: "top",
limit : "20",
};
},
posts : function(param, callback) {
// Generate our API query string
var queryString = _buildQueryStringFromParameter(param);
console.log(client);
// Send the request to receive all available events
client.get(queryString,
function(err, res, body) {
// Call the callback in our original area
// callback(body);
var results = body.data.children.splice(0, param.limit);
callback(results);
}
);
},
};
// Given a JSON object, build the associated parameter string
function _buildQueryStringFromParameter(param) {
var str = [];
var query = "/r/";
if (param.sub) {
query += param.sub;
}
if (param.sort) {
query += "/" + param.sort;
}
query += ".json";
return query;
}