Permalink
Browse files

fetching popular media works

  • Loading branch information...
1 parent 443d716 commit 32bca710a6b0f0f764ec4a1d04035b2e280c7690 @Swizec committed Mar 1, 2011
Showing with 41 additions and 11 deletions.
  1. +29 −10 index.js
  2. +12 −1 tests.js
View
39 index.js
@@ -3,20 +3,21 @@ var http = require('http'),
https = require('https');
-function InstagramClient() {
- this.client_id = '';
- this.client_secret = '';
+function InstagramClient(client_id, client_secret) {
+ this.client_id = client_id;
+ this.client_secret = client_secret;
- this.media = new InstagramMediaClient();
+ this.media = new InstagramMediaClient(this);
}
-function InstagramMediaClient() {
+function InstagramMediaClient(parent) {
+ this.parent = parent;
}
InstagramMediaClient.prototype.id = function (id, callback) {
var options = {
host: 'api.instagram.com',
- path: '/v1/media/'+id+'?client_id='+this.client_id
+ path: '/v1/media/'+id+'?client_id='+this.parent.client_id
};
https.get(options, function (res) {
@@ -36,12 +37,30 @@ InstagramMediaClient.prototype.id = function (id, callback) {
});
};
+InstagramMediaClient.prototype.popular = function (callback) {
+ var options = {
+ host: 'api.instagram.com',
+ path: '/v1/media/popular/?client_id='+this.parent.client_id
+ };
+ https.get(options, function (res) {
+ var raw = "";
+ res.on('data', function (chunk) {
+ raw += chunk;
+ });
+ res.on('end', function () {
+ var response = JSON.parse(raw);
+
+ if (response['meta']['code'] == 200) {
+ callback(response['data'], null);
+ }else{
+ callback(response['meta'], response['meta']['code']);
+ }
+ });
+ });
+}
exports.createClient = function (client_id, client_secret) {
- var instagram_client = new InstagramClient();
-
- instagram_client.client_id = client_id;
- instagram_client.client_secret = client_secret;
+ var instagram_client = new InstagramClient(client_id, client_secret);
return instagram_client;
}
View
13 tests.js
@@ -12,4 +12,15 @@ exports.testMediaFetchById = function (test) {
test.done();
});
-}
+};
+
+exports.testMediaFetchPopular = function (test) {
+ test.expect(2)
+
+ instagram.media.popular(function (media, error) {
+ test.ok((media.length > 0));
+ test.equal(error, null, "Returned an error");
+
+ test.done();
+ });
+};

0 comments on commit 32bca71

Please sign in to comment.