Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 3a276ef1919f681626bcf6fb8a824a1de8c843ec @mckelvey committed Apr 7, 2011
@@ -0,0 +1,14 @@
+# Edits/Editors #
+#################
+._*
+*~
+*BAK
+.project
+
+# OS generated files #
+######################
+.DS_Store?
+ehthumbs.db
+Icon?
+Thumbs.db
+.AppleDouble
@@ -0,0 +1,4 @@
+
+# Remove Coffeescript #
+#######################
+*.coffee
@@ -0,0 +1,112 @@
+
+querystring = require 'querystring'
+
+class InstagramAPI
+ constructor: ->
+ @_api_version = 'v1'
+ @_client = require 'https'
+ @_config =
+ client_id: if process.env['CLIENT_ID']? then process.env['CLIENT_ID'] else 'CLIENT-ID'
+ client_secret: if process.env['CLIENT_SECRET']? then process.env['CLIENT_SECRET'] else 'CLIENT-SECRET'
+ callback_url: if process.env['CALLBACK_URL']? then process.env['CALLBACK_URL'] else ''
+ @_options =
+ host: 'api.instagram.com'
+ port: null
+ method: "GET"
+ path: ''
+ headers: {
+ 'User-Agent': 'Instagram JS Lib 0.0.1'
+ 'Accept': 'application/json'
+ 'Content-Length': 0
+ }
+ for module in ['media', 'tags', 'users', 'locations', 'subscriptions']
+ moduleClass = require "./class.instagram.#{module}"
+ @[module] = new moduleClass.module @
+
+ ###
+ Generic Response Methods
+ ###
+
+ _error: (e, value, caller) ->
+ value = '[undefined]' if value is undefined
+ value = '[null]' if value is null
+ message = "#{e} occurred: #{value} in #{caller}"
+ console.log(message)
+ message
+
+ _complete: (data) ->
+ for i of data
+ console.log data[i].id
+
+ ###
+ Shared Data Manipulation Methods
+ ###
+
+ _to_querystring: (params) ->
+ obj = {}
+ for i of params
+ obj[i] = params[i] if i isnt 'complete' and i isnt 'error'
+ querystring.stringify obj
+
+ _merge: (obj1, obj2) ->
+ for i of obj2
+ obj1[i] = obj2[i]
+ obj1
+
+ _to_value: (key, value) ->
+ return value if typeof value isnt 'object'
+ return value[key] if value[key]?
+ null
+
+ _to_object: (key, value) ->
+ return value if typeof value is 'object' and value[key]?
+ obj = {}
+ obj[key] = value
+
+ _clone: (from_object) ->
+ to_object = {}
+ for property of from_object
+ to_object[property] = from_object[property]
+ to_object
+
+ ###
+ Generic Methods
+ ###
+
+ set: (property, value) ->
+ @_config[property] = value if @_config[property] isnt undefined
+ @_options[property] = value if @_options[property] isnt undefined
+
+ ###
+ Shared Request Methods
+ ###
+
+ _request: (params) ->
+ options = @_clone(@_options)
+ options['path'] = params['path'] if params['path']?
+ options['method'] = params['method'] if params['method']?
+ complete = params['complete'] ?= @_complete
+ error = params['error'] ?= @_error
+ if options['method'] isnt "GET" and params['post_data']?
+ post_data = @_to_querystring params['post_data']
+ else
+ post_data = ''
+ options['headers']['Content-Length'] = post_data.length
+ request = @_client.request options, (response) ->
+ data = ''
+ response.setEncoding 'utf8'
+ response.on 'data', (chunk) ->
+ data += chunk
+ response.on 'end', ->
+ try
+ parsedResponse = JSON.parse data
+ catch e
+ error e, data, '_request'
+ error parsedResponse['meta']['error_type'], parsedResponse['meta']['error_message'], "_request" if parsedResponse? and parsedResponse['meta']? and parsedResponse['meta']['code'] isnt 200
+ complete parsedResponse['data']
+ if post_data?
+ request.write post_data
+ request.end()
+
+APIClient = new InstagramAPI
+exports.API = APIClient
@@ -0,0 +1,160 @@
+(function() {
+ var APIClient, InstagramAPI, querystring;
+ querystring = require('querystring');
+ InstagramAPI = (function() {
+ function InstagramAPI() {
+ var module, moduleClass, _i, _len, _ref;
+ this._api_version = 'v1';
+ this._client = require('https');
+ this._config = {
+ client_id: process.env['CLIENT_ID'] != null ? process.env['CLIENT_ID'] : 'CLIENT-ID',
+ client_secret: process.env['CLIENT_SECRET'] != null ? process.env['CLIENT_SECRET'] : 'CLIENT-SECRET',
+ callback_url: process.env['CALLBACK_URL'] != null ? process.env['CALLBACK_URL'] : ''
+ };
+ this._options = {
+ host: 'api.instagram.com',
+ port: null,
+ method: "GET",
+ path: '',
+ headers: {
+ 'User-Agent': 'Instagram JS Lib 0.0.1',
+ 'Accept': 'application/json',
+ 'Content-Length': 0
+ }
+ };
+ _ref = ['media', 'tags', 'users', 'locations', 'subscriptions'];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ module = _ref[_i];
+ moduleClass = require("./class.instagram." + module);
+ this[module] = new moduleClass.module(this);
+ }
+ }
+ /*
+ Generic Response Methods
+ */
+ InstagramAPI.prototype._error = function(e, value, caller) {
+ var message;
+ if (value === void 0) {
+ value = '[undefined]';
+ }
+ if (value === null) {
+ value = '[null]';
+ }
+ message = "" + e + " occurred: " + value + " in " + caller;
+ console.log(message);
+ return message;
+ };
+ InstagramAPI.prototype._complete = function(data) {
+ var i, _results;
+ _results = [];
+ for (i in data) {
+ _results.push(console.log(data[i].id));
+ }
+ return _results;
+ };
+ /*
+ Shared Data Manipulation Methods
+ */
+ InstagramAPI.prototype._to_querystring = function(params) {
+ var i, obj;
+ obj = {};
+ for (i in params) {
+ if (i !== 'complete' && i !== 'error') {
+ obj[i] = params[i];
+ }
+ }
+ return querystring.stringify(obj);
+ };
+ InstagramAPI.prototype._merge = function(obj1, obj2) {
+ var i;
+ for (i in obj2) {
+ obj1[i] = obj2[i];
+ }
+ return obj1;
+ };
+ InstagramAPI.prototype._to_value = function(key, value) {
+ if (typeof value !== 'object') {
+ return value;
+ }
+ if (value[key] != null) {
+ return value[key];
+ }
+ return null;
+ };
+ InstagramAPI.prototype._to_object = function(key, value) {
+ var obj;
+ if (typeof value === 'object' && (value[key] != null)) {
+ return value;
+ }
+ obj = {};
+ return obj[key] = value;
+ };
+ InstagramAPI.prototype._clone = function(from_object) {
+ var property, to_object;
+ to_object = {};
+ for (property in from_object) {
+ to_object[property] = from_object[property];
+ }
+ return to_object;
+ };
+ /*
+ Generic Methods
+ */
+ InstagramAPI.prototype.set = function(property, value) {
+ if (this._config[property] !== void 0) {
+ this._config[property] = value;
+ }
+ if (this._options[property] !== void 0) {
+ return this._options[property] = value;
+ }
+ };
+ /*
+ Shared Request Methods
+ */
+ InstagramAPI.prototype._request = function(params) {
+ var complete, error, options, post_data, request, _ref, _ref2;
+ options = this._clone(this._options);
+ if (params['path'] != null) {
+ options['path'] = params['path'];
+ }
+ if (params['method'] != null) {
+ options['method'] = params['method'];
+ }
+ complete = (_ref = params['complete']) != null ? _ref : params['complete'] = this._complete;
+ error = (_ref2 = params['error']) != null ? _ref2 : params['error'] = this._error;
+ if (options['method'] !== "GET" && (params['post_data'] != null)) {
+ post_data = this._to_querystring(params['post_data']);
+ } else {
+ post_data = '';
+ }
+ options['headers']['Content-Length'] = post_data.length;
+ request = this._client.request(options, function(response) {
+ var data;
+ data = '';
+ response.setEncoding('utf8');
+ response.on('data', function(chunk) {
+ return data += chunk;
+ });
+ return response.on('end', function() {
+ var parsedResponse;
+ try {
+ parsedResponse = JSON.parse(data);
+ } catch (e) {
+ error(e, data, '_request');
+ }
+ if ((parsedResponse != null) && (parsedResponse['meta'] != null) && parsedResponse['meta']['code'] !== 200) {
+ error(parsedResponse['meta']['error_type'], parsedResponse['meta']['error_message'], "_request");
+ }
+ return complete(parsedResponse['data']);
+ });
+ });
+ if (post_data != null) {
+ request.write(post_data);
+ }
+ return request.end();
+ };
+ return InstagramAPI;
+ })();
+ APIClient = new InstagramAPI;
+ exports.API = APIClient;
+}).call(this);
@@ -0,0 +1,31 @@
+
+class InstagramLocations
+ constructor: (parent) ->
+ @parent = parent
+
+ info: (params) ->
+ params['path'] = "/#{@parent._api_version}/locations/#{params['location_id']}?client_id=#{@parent._config.client_id}"
+ @parent._request params
+
+ recent: (params) ->
+ params['client_id'] = @parent._config.client_id
+ params['path'] = "/#{@parent._api_version}/locations/#{params['location_id']}/media/recent?#{@parent._to_querystring(params)}"
+ @parent._request params
+
+ search: (params) ->
+ params['client_id'] = @parent._config.client_id
+ params['path'] = "/#{@parent._api_version}/locations/search?#{@parent._to_querystring(params)}"
+ @parent._request params
+
+ subscribe: (params) ->
+ params['object'] = 'location'
+ @parent.subscriptions._subscribe params
+
+ unsubscribe: (params) ->
+ @parent.subscriptions._unsubscribe params
+
+ unsubscribe_all: (params) ->
+ params['object'] = 'location'
+ @parent.subscriptions._unsubscribe params
+
+exports.module = InstagramLocations
@@ -0,0 +1,35 @@
+(function() {
+ var InstagramLocations;
+ InstagramLocations = (function() {
+ function InstagramLocations(parent) {
+ this.parent = parent;
+ }
+ InstagramLocations.prototype.info = function(params) {
+ params['path'] = "/" + this.parent._api_version + "/locations/" + params['location_id'] + "?client_id=" + this.parent._config.client_id;
+ return this.parent._request(params);
+ };
+ InstagramLocations.prototype.recent = function(params) {
+ params['client_id'] = this.parent._config.client_id;
+ params['path'] = "/" + this.parent._api_version + "/locations/" + params['location_id'] + "/media/recent?" + (this.parent._to_querystring(params));
+ return this.parent._request(params);
+ };
+ InstagramLocations.prototype.search = function(params) {
+ params['client_id'] = this.parent._config.client_id;
+ params['path'] = "/" + this.parent._api_version + "/locations/search?" + (this.parent._to_querystring(params));
+ return this.parent._request(params);
+ };
+ InstagramLocations.prototype.subscribe = function(params) {
+ params['object'] = 'location';
+ return this.parent.subscriptions._subscribe(params);
+ };
+ InstagramLocations.prototype.unsubscribe = function(params) {
+ return this.parent.subscriptions._unsubscribe(params);
+ };
+ InstagramLocations.prototype.unsubscribe_all = function(params) {
+ params['object'] = 'location';
+ return this.parent.subscriptions._unsubscribe(params);
+ };
+ return InstagramLocations;
+ })();
+ exports.module = InstagramLocations;
+}).call(this);
Oops, something went wrong. Retry.

0 comments on commit 3a276ef

Please sign in to comment.