Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
1157 lines (1039 loc) · 31.4 KB
/
Copy pathindex.js
File metadata and controls
1157 lines (1039 loc) · 31.4 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var InstaJS, https, q, query, vsprintf;
vsprintf = require('sprintf-js').vsprintf;
q = require('q');
https = require('https');
query = require('querystring');
module.exports = InstaJS = (function() {
/**
* @var {Object} _defaults - Default options
* @memberof InstaJS
* @access private
*/
var _API_BASE, _API_PORT, _call, _changeRelationship, _defaults, _getAccessToken, _options, _promise, _setAccessToken;
_defaults = {
limit: 10,
timeout: 15000,
access_token: void 0
};
_options = {};
/**
* @var {String} _API_BASE - Host of the Instagram API
* @memberof InstaJS
* @access private
*/
_API_BASE = 'api.instagram.com';
/**
* @var {String} _API_PORT - Port of the Instagram API
* @memberof InstaJS
* @access private
*/
_API_PORT = 443;
/**
* @method _setAccessToken
* @description Setter for the access_token
* @memberof InstaJS
*
* @access private
*/
_setAccessToken = function(token) {
if (typeof token === 'string') {
_options.access_token = token;
} else {
throw new Error('access_token must be string. [access_token is ' + typeof token + ']');
}
};
/**
* @method _getAccessToken
* @description Getter for the access_token
* @memberof InstaJS
*
* @access private
*
* @return {String} output - Access_token
*/
_getAccessToken = function() {
return _options.access_token;
};
/**
* @method _call
* @description Prepares the request and calls the Instagram api as well as it
* builds the result and errors to be returned to the promise or callback
* @memberof InstaJS
*
* @access private
*
* @param {String} method - HTTP method (GET, POST, DELETE)
* @param {String} endpoint - Instagram API endpoint to be called
* @param {Object} params - Params for the API call
* @param {Function} callback - Callback function to be called
*
* @return {Function} output - Returnes callback
*/
_call = function(method, endpoint, params, callback) {
var data, options, req, result;
if (params == null) {
params = {};
}
result = '';
params.access_token = _getAccessToken();
if (params.access_token === void 0) {
callback('Missing access_token');
}
if (!params.count) {
params.count = _options.limit;
}
options = {
host: _API_BASE,
port: _API_PORT,
method: method,
path: '/v1/' + endpoint + (method !== 'POST' ? '?' + query.stringify(params) : ''),
headers: {
'Content-Type': 'application/json'
}
};
if (method === 'POST') {
data = query.stringify(params);
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
options.headers['Content-Length'] = data.length;
}
req = https.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
result += chunk;
});
res.on('end', function() {
var err, error, limit, remaining;
limit = parseInt(res.headers['x-ratelimit-limit'], 10) || 0;
remaining = parseInt(res.headers['x-ratelimit-remaining'], 10) || 0;
try {
result = JSON.parse(result);
} catch (error) {
err = error;
return callback(err, result, limit, remaining);
}
return callback(null, result, limit, remaining);
});
});
if (data) {
req.write(data);
}
req.setTimeout(_options.timeout, function() {
req.abort();
});
req.on('error', function(err) {
return callback(err);
});
req.end();
};
/**
* @method _promise
* @description Build the promise to be returned from a API call without callback
* @memberof InstaJS
*
* @access private
*
* @param {Object} deferred
*
* @return {Function} output - Returnes promise
*/
_promise = function(deferred) {
return function(err, res, limit, remaining) {
if (!err && res.meta.code !== 200) {
deferred.reject(res.meta);
} else if (!err && res.meta) {
deferred.resolve({
code: res.meta.code,
data: res.data,
count: limit,
remaining: remaining
});
} else if (!err && !res.meta) {
deferred.reject(res);
} else {
deferred.reject(err);
}
};
};
/**
* @method _changeRelationship
* @description Changes the relationship between access_token owner and
* user by action (follow, unfollow, ignore, approve)
* @memberof InstaJS
*
* @access private
*
* @param {String} id - Can be a user id or 'self'
* @param {String} action - Can be 'follow', 'unfollow', 'ignore' and 'approve'
* @param {Function} callback - Callback function to be called
*
* @return {Function} output - Returnes callback or promise
*/
_changeRelationship = function(id, action, callback) {
var url;
callback = callback || action || id || void 0;
url = vsprintf('users/%s/relationship', [id]);
return _call('POST', url, {
action: action
}, callback);
};
/**
* @class InstaJS
* @constructs InstaJS
* @description Create a new InstaJS object.
* @classdesc Class wrapping the Instagram API into JavaSctipt Class
*
* @param {Object} opts - Options for the api calls
* @property {String} opts.access_token - Token to authenticate at
* instragam api
* @property {Number} opts.limit - The limit to use for api call results
* @property {Number} opts.timeout - Timeout for api call
*/
function InstaJS(opts) {
if (typeof opts !== 'string') {
_options = _.merge({}, _defaults, opts);
} else {
_options = _defaults;
_setAccessToken(opts);
}
}
/**
* @method getUser
* @description Get information about a user.
* @memberof InstaJS
* @access public
*
* @example <caption>Get user info</caption>
* new InstaJS().getUser('3019302352', function(err, user){
* //do something...
* });)
*
* @param {String} user - Can be a user id or 'self'
* @param {Function} callback - Callback function to be called
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.getUser = function(user, callback) {
var deferred, url;
callback = callback || user || void 0;
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = vsprintf('users/%s/', [user]);
_call('GET', url, null, callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method getTag
* @description Get information about a tag.
* @memberof InstaJS
*
* @example <caption>Get tag info</caption>
* new InstaJS().getTag('photooftheday', function(err, user){
* //do something...
* });)
*
* @param {String} tag - Tag name to get info
* @param {Function} callback - Callback function to be called
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.getTag = function(tag, callback) {
var deferred, url;
callback = callback || tag || void 0;
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = vsprintf('tags/%s/', [tag]);
_call('GET', url, null, callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method getLocation
* @description Get information about a location.
* @memberof InstaJS
*
* @example <caption>Get location info</caption>
* new InstaJS().getLocation('123', function(err, user){
* //do something...
* });)
*
* @param {String} location - Location id to get info
* @param {Function} callback - Callback function to be called
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.getLocation = function(location, callback) {
var deferred, url;
callback = callback || location || void 0;
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = vsprintf('locations/%s/', [location]);
_call('GET', url, null, callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method recentlyLiked
* @description Get recently liked media of access_token owner.
* @memberof InstaJS
*
* @example <caption>Recently liked media</caption>
* new InstaJS().recentlyLiked({count: 10}, function(err, media){
* //do something...
* });
*
* @param {Object} opts - Options for api call
* @param {Function} callback - Callback function to be called
* @property {Number} opts.count - Limit of returned media objects
* @property {Number} opts.max_like_id - Get media liked before this id
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.recentlyLiked = function(opts, callback) {
var deferred, url;
callback = callback || opts || void 0;
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = 'users/self/media/liked';
_call('GET', url, opts, callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method recentlyPosted
* @description Get recently posted media of user.
* @memberof InstaJS
*
* @example <caption>Recently posted media</caption>
* new InstaJS().recentlyPosted('1245', {count: 10}, function(err, media){
* //do something...
* });
*
* @param {String} id - Can be 'self' or a user id
* @param {Object} opts - Options for api call
* @param {Function} callback - Callback function to be called
* @property {Number} opts.count - Limit of returned media objects
* @property {Number} opts.max_id - Get media posted before this id
* @property {Number} opts.min_id - Get media posted after this id
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.recentlyPosted = function(id, opts, callback) {
var deferred, url;
callback = callback || opts || id || void 0;
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = vsprintf('users/%s/media/recent', [id]);
_call('GET', url, opts, callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method findUsersByName
* @description Search user by username.
* @memberof InstaJS
*
* @example <caption>Search a user</caption>
* new InstaJS().findUsersByName('rico_race_director', function(err, media){
* //do something...
* });)
*
* @param {String} name - Username to search for
* @param {Object} opts - Options for api call
* @param {Function} callback - Callback function to be called
* @property {Number} opts.count - Limit of returned users
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.findUsersByName = function(name, opts, callback) {
var deferred, url;
callback = callback || opts || name || void 0;
if (typeof name === 'string') {
opts.q = name;
}
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = 'users/search';
_call('GET', url, opts, callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method findTagsByName
* @description Search tags by name.
* @memberof InstaJS
*
* @example <caption>Search a tag</caption>
* new InstaJS().findTagsByName('photoof', function(err, media){
* //do something...
* });)
*
* @param {String} name - Tag name to search
* @param {Object} opts - Options for api call
* @param {Function} callback - Callback function to be called
* @property {Number} opts.count - Limit of returned tags
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.findTagsByName = function(name, opts, callback) {
var deferred, url;
if (opts == null) {
opts = {};
}
callback = callback || opts || name || void 0;
if (typeof name === 'string') {
opts.q = name;
}
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = 'tags/search';
_call('GET', url, opts, callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method findLocationsByGeo
* @description Get locations around specific geo coordinate.
* @memberof InstaJS
*
* @example <caption>Get locations</caption>
* new InstaJS().findLocationsByGeo('48.858844', '2.294351', function(err, user){
* //do something...
* });)
*
* @param {String} lat - Latitute of location
* @param {String} lng - Longitute of location
* @param {Object} opts - Options for api call
* @param {Function} callback - Callback function to be called
* @property opts {Number} distance - Area around coordinate in meters (min=0, max=750)
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.findLocationsByGeo = function(lat, lng, opts, callback) {
var deferred, url;
callback = callback || opts || lat || lng || void 0;
if (typeof lat === 'string') {
opts.lat = lat;
}
if (typeof lng === 'string') {
opts.lng = lng;
}
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = 'locations/search';
_call('GET', url, opts, callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method postsByUser
* @description Get recently posted media of a user.
* @memberof InstaJS
*
* @example <caption>Recent posts by user</caption>
* new InstaJS().postsByUser('self', {count: 10}, function(err, media){
* //do something...
* });
*
* @param {String} user - Can be 'self' or a user id
* @param {Object} opts - Options for api call
* @param {Function} callback - Callback function to be called
* @property opts {Number} count - Limit of returned media objects
* @property opts {Number} max_id - Get media before this id
* @property opts {Number} min_id - Get media after this id
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.postsByUser = function(user, opts, callback) {
var deferred, url;
callback = callback || opts || user || void 0;
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = vsprintf('users/%s/media/recent/', [user]);
_call('GET', url, opts, callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method postsByTag
* @description Get recent media by hashtag
* @memberof InstaJS
*
* @example <caption>Recent posts by hastag</caption>
* new InstaJS().postsByTag('instajs', {count: 10}, function(err, media){
* //do something...
* });
*
* @param {String} user - Can be 'self' or a user id
* @param {Object} opts - Options for api call
* @param {Function} callback - Callback function to be called
* @property opts {Number} count - Limit of returned media objects
* @property opts {Number} max_tag_id - Get media before this tag id
* @property opts {Number} min_tag_id - Get media after this tag id
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.postsByTag = function(tag, opts, callback) {
var deferred, url;
callback = callback || opts || tag || void 0;
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = vsprintf('tags/%s/media/recent', [tag]);
_call('GET', url, opts, callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method postsByLocationId
* @description Get recent media by location id
* @memberof InstaJS
*
* @example <caption>Recent posts by location</caption>
* new InstaJS().postsByLocationId('1', function(err, media){
* //do something...
* });
*
* @param {String} location - Must be location id
* @param {Object} opts - Options for api call
* @param {Function} callback - Callback function to be called
* @property opts {Number} max_id - Get media before this id
* @property opts {Number} min_id - Get media after this id
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.postsByLocationId = function(location, opts, callback) {
var deferred, url;
callback = callback || opts || location || void 0;
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = vsprintf('locations/%s/media/recent', [location]);
_call('GET', url, opts, callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method postsByGeoLocation
* @description Get recent media by geo location
* @memberof InstaJS
*
* @example <caption>Recent posts by geo location</caption>
* new InstaJS().postsByGeoLocation('48.858844', '2.294351', function(err, media){
* //do something...
* });
*
* @param {String} lat - Latitute of location
* @param {String} lng - Longitute of location
* @param {Object} opts - Options for api call
* @param {Function} callback - Callback function to be called
* @property opts {Number} distance - Area around coordinate in meters (min=0, max=750)
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.postsByGeoLocation = function(lat, lng, opts, callback) {
var deferred, url;
callback = callback || opts || lat || lng || void 0;
if (typeof opts !== 'object') {
opts = {};
}
if (typeof lat === 'string') {
opts.lat = lat;
}
if (typeof lng === 'string') {
opts.lng = lng;
}
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = 'media/search';
_call('GET', url, opts, callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method postById
* @description Get media by id
* @memberof InstaJS
*
* @example <caption>Get post by id</caption>
* new InstaJS().postById('12678491', function(err, media){
* //do something...
* });
*
* @param {String} id - Id of post
* @param {Function} callback - Callback function to be called
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.postById = function(id, callback) {
var deferred, url;
callback = callback || id || void 0;
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = vsprintf('media/%s', [id]);
_call('GET', url, null, callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method postBShortcode
* @description Get media by shortcode
* @memberof InstaJS
*
* @example <caption>Get post by shortcode</caption>
* new InstaJS().postByShortcode('tsxp1hhQTG', function(err, media){
* //do something...
* });
*
* @param {String} shortcode - Shortcode of post
* @param {Function} callback - Callback function to be called
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.postByShortcode = function(shortcode, callback) {
var deferred, url;
callback = callback || shortcode || void 0;
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = vsprintf('media/shortcode/%s', [shortcode]);
_call('GET', url, null, callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method getFollows
* @description Get list of users the access_token owner follows
* @memberof InstaJS
*
* @example <caption>Get followed users</caption>
* new InstaJS().getFollows(function(err, media){
* //do something...
* });
*
* @param {Function} callback - Callback function to be called
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.getFollows = function(callback) {
var deferred, url;
callback = callback || void 0;
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = 'users/self/follows';
_call('GET', url, null, callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method getFollowers
* @description Get list of users which follow the access_token owner
* @memberof InstaJS
*
* @example <caption>Get follow users</caption>
* new InstaJS().getFollowers(function(err, media){
* //do something...
* });
*
* @param {Function} callback - Callback function to be called
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.getFollowers = function(callback) {
var deferred, url;
callback = callback || void 0;
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = 'users/self/followed-by';
_call('GET', url, null, callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method getRequestedBy
* @description Get list of users which want to follow the access_token owner
* @memberof InstaJS
*
* @example <caption>Get open request list</caption>
* new InstaJS().getRequestedBy(function(err, media){
* //do something...
* });
*
* @param {Function} callback - Callback function to be called
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.getRequestedBy = function(callback) {
var deferred, url;
callback = callback || void 0;
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = 'users/self/requested-by';
_call('GET', url, null, callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method getRelationship
* @description Get relationship to user with given id
* @memberof InstaJS
*
* @example <caption>Get relationship with user</caption>
* new InstaJS().getRelationship('1237', function(err, media){
* //do something...
* });
*
* @param {String} id - Id of user to get relationship
* @param {Function} callback - Callback function to be called
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.getRelationship = function(id, callback) {
var deferred, url;
callback = callback || id || void 0;
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = vsprintf('users/%s/relationship', [id]);
_call('GET', url, null, callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method follow
* @description Follow user with id
* @memberof InstaJS
*
* @example <caption>Follow user</caption>
* new InstaJS().follow('131231231', function(err, media){
* //do something...
* });
*
* @param {String} id - Id of user to follow
* @param {Function} callback - Callback function to be called
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.follow = function(id, callback) {
var deferred, url;
callback = callback || id || void 0;
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = vsprintf('users/%s/relationship', [id]);
_changeRelationship(id, 'follow', callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method unfollow
* @description Unfollow user with id
* @memberof InstaJS
*
* @example <caption>Unfollow user</caption>
* new InstaJS().unfollow('131231231', function(err, media){
* //do something...
* });
*
* @param {String} id - Id of user to unfollow
* @param {Function} callback - Callback function to be called
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.unfollow = function(id, callback) {
var deferred, url;
callback = callback || id || void 0;
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = vsprintf('users/%s/relationship', [id]);
_changeRelationship(id, 'unfollow', callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method approveRequest
* @description Approve request to follow by user with id
* @memberof InstaJS
*
* @example <caption>Approve request</caption>
* new InstaJS().approveRequest('131231231', function(err, media){
* //do something...
* });
*
* @param {String} id - Id of user which requested to allow to follow
* @param {Function} callback - Callback function to be called
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.approveRequest = function(id, callback) {
var deferred, url;
callback = callback || id || void 0;
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = vsprintf('users/%s/relationship', [id]);
_changeRelationship(id, 'approve', callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method ignoreRequest
* @description Ingore request to follow by user with id
* @memberof InstaJS
*
* @example <caption>Ignore request</caption>
* new InstaJS().ignoreRequest('131231231', function(err, media){
* //do something...
* });
*
* @param {String} id - Id of user to ignore
* @param {Function} callback - Callback function to be called
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.ignoreRequest = function(id, callback) {
var deferred, url;
callback = callback || id || void 0;
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = vsprintf('users/%s/relationship', [id]);
_changeRelationship(id, 'ignore', callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method like
* @description Send a like to media by it's id
* @memberof InstaJS
*
* @example <caption>Like a post by id</caption>
* new InstaJS().like('12678491', function(err, media){
* //do something...
* });
*
* @param {String} id - Id of post to like
* @param {Function} callback - Callback function to be called
*
* @return {Function} output - Returnes callback or promise
*/
InstaJS.prototype.like = function(id, callback) {
var deferred, url;
callback = callback || id || void 0;
if (typeof callback !== 'function') {
deferred = q.defer();
callback = _promise(deferred);
}
url = vsprintf('media/%s/likes', [id]);
_call('POST', url, null, callback);
if (deferred) {
return deferred.promise;
}
};
/**
* @method getLikes
* @description Get recent likes of media by it's id
* @memberof InstaJS
*
* @example <caption>Get recebt likes of a post by id</caption>