1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | (function() {
|
10 | var Foursquare, qs, xhr;
|
11 |
|
12 | xhr = require('request');
|
13 |
|
14 | qs = require('querystring');
|
15 |
|
16 | Foursquare = (function() {
|
17 | var apihost, isFunction, request, version;
|
18 |
|
19 | apihost = 'https://api.foursquare.com';
|
20 |
|
21 | version = 'v2';
|
22 |
|
23 | function Foursquare(options) {
|
24 | var date, month;
|
25 | this.options = options != null ? options : {};
|
26 | date = new Date();
|
27 | month = date.getMonth() + 1;
|
28 | options.date = date.getFullYear();
|
29 | options.date += (month < 10 ? '0' : '') + month;
|
30 | options.date += (date.getDate() < 10 ? '0' : '') + date.getDate();
|
31 | }
|
32 |
|
33 | Foursquare.prototype.user = function(userid, params, fn) {
|
34 | if (!userid || parseInt(userid, 10) <= 0) {
|
35 | userid = 'self';
|
36 | }
|
37 | if (isFunction(params)) {
|
38 | fn = params;
|
39 | params = {};
|
40 | }
|
41 | this.scheme = "/users/" + userid;
|
42 | return request(this, params, fn);
|
43 | };
|
44 |
|
45 | Foursquare.prototype.checkins = function(userid, params, fn) {
|
46 | if (!userid || parseInt(userid, 10) <= 0) {
|
47 | userid = 'self';
|
48 | }
|
49 | if (isFunction(params)) {
|
50 | fn = params;
|
51 | params = {};
|
52 | }
|
53 | this.scheme = "/users/" + userid + "/checkins";
|
54 | return request(this, params, fn);
|
55 | };
|
56 |
|
57 | Foursquare.prototype.badges = function(userid, params, fn) {
|
58 | if (!userid || parseInt(userid, 10) <= 0) {
|
59 | userid = 'self';
|
60 | }
|
61 | if (isFunction(params)) {
|
62 | fn = params;
|
63 | params = {};
|
64 | }
|
65 | this.scheme = "/users/" + userid + "/badges";
|
66 | return request(this, params, fn);
|
67 | };
|
68 |
|
69 | request = function(self, query, fn) {
|
70 | var params;
|
71 | if (query == null) {
|
72 | query = {};
|
73 | }
|
74 | if (fn == null) {
|
75 | fn = function() {};
|
76 | }
|
77 | query.v = self.options.date;
|
78 | query = qs.stringify(query);
|
79 | params = {
|
80 | url: "" + apihost + "/" + version + self.scheme + "?oauth_token=" + self.options.token + "&" + query
|
81 | };
|
82 | return xhr(params, function(error, request, body) {
|
83 | var data;
|
84 | body = JSON.parse(body);
|
85 | if (body.meta.code !== 200) {
|
86 | error = body.meta;
|
87 | }
|
88 | data = body.response != null ? body.response : null;
|
89 | return fn.call(self, error, data);
|
90 | });
|
91 | };
|
92 |
|
93 | isFunction = function(object) {
|
94 | return '[object Function]' === toString.call(object);
|
95 | };
|
96 |
|
97 | return Foursquare;
|
98 |
|
99 | })();
|
100 |
|
101 | module.exports = Foursquare;
|
102 |
|
103 | }).call(this);
|