Skip to content

Instantly share code, notes, and snippets.

@cpsubrian
Created July 5, 2011 07:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpsubrian/1064397 to your computer and use it in GitHub Desktop.
Save cpsubrian/1064397 to your computer and use it in GitHub Desktop.
Figuring out scope
// Constants
var API_HOST = 'api.instagram.com';
var API_BASE_URL = '/v1';
var CLIENT_ID = 'the id';
var CLIENT_SECRET = 'the secret';
var OAuth= require('oauth').OAuth;
var https = require('https');
var util = require('util');
var photos = [];
// Display the latest popular photos.
var popular = exports.popular = function(req, res) {
fetchPopular(function(photos) {
res.render('index', {
title: 'Instagram',
photos: photos
});
});
}
// Fetch popular photos.
var fetchPopular = function(finish) {
if (!photos.length) {
var options = {
host: API_HOST,
path: API_BASE_URL + '/media/popular?client_id=' + CLIENT_ID,
};
https.get(options, function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
var parsed = JSON.parse(data);
for (var i in parsed.data) {
photos.push(new photo(parsed.data[i]));
}
finish(photos);
});
}).on('error', function(e) {
console.error(e);
});
}
else {
finish(photos);
}
}
// Parse the properties we want from photo data.
var photo = function(data) {
if (data.caption) {
this.caption = data.caption.text;
}
if (typeof data.images.standard_resolution != 'undefined') {
this.image = data.images.standard_resolution;
}
if (typeof data.images.thumbnail != 'undefined') {
this.thumb = data.images.thumbnail;
}
if (typeof data.link != 'undefined') {
this.link = data.link;
}
if (typeof data.user != 'undefined') {
this.user = data.user;
}
};
photo.prototype = {
render: function() {
return '<img src="' + this.image.url + '" width="' + this.image.width + '" height="' + this.image.height + '">';
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment