Permalink
Browse files

small refactoring to keep things saner

  • Loading branch information...
1 parent 32bca71 commit b30f8d49ce1c100e2908cd79b36465733f34a652 @Swizec committed Mar 1, 2011
Showing with 29 additions and 30 deletions.
  1. +29 −30 index.js
View
@@ -10,15 +10,8 @@ function InstagramClient(client_id, client_secret) {
this.media = new InstagramMediaClient(this);
}
-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.parent.client_id
- };
+InstagramClient.prototype.fetch = function (options, callback) {
+ options.host = options.host || 'api.instagram.com';
https.get(options, function (res) {
var raw = "";
@@ -27,35 +20,41 @@ InstagramMediaClient.prototype.id = function (id, callback) {
});
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']);
- }
+
+ callback(response);
});
+ });
+}
+
+function InstagramMediaClient(parent) {
+ this.parent = parent;
+}
+
+InstagramMediaClient.prototype.id = function (id, callback) {
+ var options = {
+ path: '/v1/media/'+id+'?client_id='+this.parent.client_id
+ };
+
+ this.parent.fetch(options, function (response) {
+ if (response['meta']['code'] == 200) {
+ callback(response['data'], null);
+ }else{
+ callback(response['meta'], response['meta']['code']);
+ }
});
};
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']);
- }
- });
+
+ this.parent.fetch(options, function (response) {
+ if (response['meta']['code'] == 200) {
+ callback(response['data'], null);
+ }else{
+ callback(response['meta'], response['meta']['code']);
+ }
});
}

0 comments on commit b30f8d4

Please sign in to comment.