diff --git a/js/instagram.js b/js/instagram.js index 1468acd..40643a0 100644 --- a/js/instagram.js +++ b/js/instagram.js @@ -8,7 +8,7 @@ var instagramFeed = Ractive.extend({ lazy:true, - makeQuery: function(method) { + makeQuery: function(place) { var base, endpoint, final, callback; base = "https://api.instagram.com/v1"; switch (this.data.method) { @@ -28,13 +28,14 @@ var instagramFeed = Ractive.extend({ endpoint = "locations/" + this.data.search + "/media/recent"; break; case "user": - if (typeof this.data.userId !== 'number') { + if (typeof this.data.search !== 'number') { throw new Error("No user specified. Use the 'userId' option."); } - if (typeof this.data.accessToken !== 'string') { - throw new Error("No access token. Use the 'accessToken' option."); + if (typeof this.data.clientID !== 'string') { + throw new Error("Invalid access token. Make sure you have a valid Instagram API access token."); } endpoint = "users/" + this.data.search + "/media/recent"; + console.log(endpoint); break; default: throw new Error("Invalid option for get: '" + this.data.get + "'."); @@ -46,7 +47,7 @@ var instagramFeed = Ractive.extend({ if (this.data.postsPerPage != null) { final += "&count=" + this.data.postsPerPage; } - switch(method){ + switch(place){ case 'before': callback = "&callback=instagramReceiverFrontAppend&min_id="+this.data.instagramData.pagination.min_tag_id; break; @@ -66,13 +67,17 @@ var instagramFeed = Ractive.extend({ * * Loads more instagram data. Either replaces current data, appends data before feed, or appends data after feed */ - load: function(method){ + load: function(place, callback){ + if(callback !== undefined){ + this.dataCallback = callback; + } //No older data to load. stop now. - if(method == 'after' && this.data.endOfFeed){ + if(place == 'after' && this.data.endOfFeed){ return false; } //We're already searching for something... Patience if(this.data.loading == true) { + console.log('already loading'); return false; } else { @@ -83,7 +88,7 @@ var instagramFeed = Ractive.extend({ var tag = document.createElement('script'); tag.id = 'instagram-script-loader'; tag.onerror = function(){console.log('unable to reach IG API');}; - tag.src = this.makeQuery(method); + tag.src = this.makeQuery(place); var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); }, diff --git a/js/instagram.min.js b/js/instagram.min.js index 86b8c1c..360c609 100644 --- a/js/instagram.min.js +++ b/js/instagram.min.js @@ -1,2 +1,2 @@ -/*! instagram 2014-05-24 */ -var instagramFeed=Ractive.extend({lazy:!0,makeQuery:function(a){var b,c,d,e;switch(b="https://api.instagram.com/v1",this.data.method){case"popular":c="media/popular";break;case"tags":if("string"!=typeof this.data.search)throw new Error("No tag name specified. Use the 'tagName' option.");c="tags/"+this.data.search+"/media/recent";break;case"location":if("number"!=typeof this.data.location)throw new Error("No location specified. Use the 'locationId' option.");c="locations/"+this.data.search+"/media/recent";break;case"user":if("number"!=typeof this.data.userId)throw new Error("No user specified. Use the 'userId' option.");if("string"!=typeof this.data.accessToken)throw new Error("No access token. Use the 'accessToken' option.");c="users/"+this.data.search+"/media/recent";break;default:throw new Error("Invalid option for get: '"+this.data.get+"'.")}switch(d=""+b+"/"+c,null!=this.data.clientID&&(d+="?client_id="+this.data.clientID),null!=this.data.postsPerPage&&(d+="&count="+this.data.postsPerPage),a){case"before":e="&callback=instagramReceiverFrontAppend&min_id="+this.data.instagramData.pagination.min_tag_id;break;case"after":e="&callback=instagramReceiverRearAppend&max_id="+this.data.instagramData.pagination.next_max_tag_id;break;case"replace":e="&callback=instagramReceiverReplace"}return console.log(d+e),d+e},load:function(a){if("after"==a&&this.data.endOfFeed)return!1;if(1==this.data.loading)return!1;this.set("loading",!0),console.log("calling IG API");var b=document.createElement("script");b.id="instagram-script-loader",b.onerror=function(){console.log("unable to reach IG API")},b.src=this.makeQuery(a);var c=document.getElementsByTagName("script")[0];c.parentNode.insertBefore(b,c)},getNewer:function(){this.load("before")},getOlder:function(){this.load("after")},init:function(a){return this.data.instagramData=[],this.data.current=-1,this.data.min=0,this.data.max=0,this.data.lightbox=!1,this.data.endOfFeed=!1,this.data.postsPerPage=6,this.data.method="tags",this.data.message="",void 0==a.clientID?(console.log("No Client ID Provided"),this.success=!1,!1):(this.data.clientID=a.clientID,this.dataCallback=void 0!=a.dataCallback?a.dataCallback:function(){},void 0==a.search?(console.log("No Hashtag Provided"),this.success=!1,!1):(this.data.search=this.data.searched=a.search,this.validateData=function(a){return this.set("loading",!1),400==a.meta.code?(this.error=!0,this.set("message","Invalid Client ID"),this.dataCallback(a),!1):200==a.meta.code?(this.error=!1,!0):(this.error=!0,this.set("message","Error retrieving IG data."),this.dataCallback(a),console.log(a),!1)},this.replaceData=function(a){void 0==a.data?this.set("message","Sorry no results for #"+this.data.search+" :["):(this.set("instagramData",a),this.set("searched",this.data.search),this.set("message",""),this.set("endOfFeed",!1)),this.dataCallback(a)},window.instagramReceiverReplace=function(a){return function(b){a.validateData(b)&&a.replaceData(b)}}(this),this.frontAppend=function(a){0==a.data.length?console.log("Nothing to append"):(console.log("front Append"),this.set("instagramData.data",a.data.concat(this.data.instagramData.data)),this.data.instagramData.pagination.min_tag_id=a.pagination.min_tag_id),this.dataCallback(a)},window.instagramReceiverFrontAppend=function(a){return function(b){a.validateData(b)&&a.frontAppend(b)}}(this),this.rearAppend=function(a){console.log("rear Append"),this.set("instagramData.data",this.data.instagramData.data.concat(a.data)),void 0===a.pagination.next_max_tag_id?this.set("endOfFeed",!0):this.data.instagramData.pagination.next_max_tag_id=a.pagination.next_max_tag_id,this.dataCallback(a)},window.instagramReceiverRearAppend=function(a){return function(b){a.validateData(b)&&a.rearAppend(b)}}(this),this.observe("search",function(){this.load("replace")}),void this.load("replace")))}}); \ No newline at end of file +/*! instagram 2014-06-09 */ +var instagramFeed=Ractive.extend({lazy:!0,makeQuery:function(a){var b,c,d,e;switch(b="https://api.instagram.com/v1",this.data.method){case"popular":c="media/popular";break;case"tags":if("string"!=typeof this.data.search)throw new Error("No tag name specified. Use the 'tagName' option.");c="tags/"+this.data.search+"/media/recent";break;case"location":if("number"!=typeof this.data.location)throw new Error("No location specified. Use the 'locationId' option.");c="locations/"+this.data.search+"/media/recent";break;case"user":if("number"!=typeof this.data.search)throw new Error("No user specified. Use the 'userId' option.");if("string"!=typeof this.data.clientID)throw new Error("Invalid access token. Make sure you have a valid Instagram API access token.");c="users/"+this.data.search+"/media/recent",console.log(c);break;default:throw new Error("Invalid option for get: '"+this.data.get+"'.")}switch(d=""+b+"/"+c,null!=this.data.clientID&&(d+="?client_id="+this.data.clientID),null!=this.data.postsPerPage&&(d+="&count="+this.data.postsPerPage),a){case"before":e="&callback=instagramReceiverFrontAppend&min_id="+this.data.instagramData.pagination.min_tag_id;break;case"after":e="&callback=instagramReceiverRearAppend&max_id="+this.data.instagramData.pagination.next_max_tag_id;break;case"replace":e="&callback=instagramReceiverReplace"}return console.log(d+e),d+e},load:function(a,b){if(void 0!==b&&(this.dataCallback=b),"after"==a&&this.data.endOfFeed)return!1;if(1==this.data.loading)return console.log("already loading"),!1;this.set("loading",!0),console.log("calling IG API");var c=document.createElement("script");c.id="instagram-script-loader",c.onerror=function(){console.log("unable to reach IG API")},c.src=this.makeQuery(a);var d=document.getElementsByTagName("script")[0];d.parentNode.insertBefore(c,d)},getNewer:function(){this.load("before")},getOlder:function(){this.load("after")},init:function(a){return this.data.instagramData=[],this.data.current=-1,this.data.min=0,this.data.max=0,this.data.lightbox=!1,this.data.endOfFeed=!1,this.data.postsPerPage=6,this.data.method="tags",this.data.message="",void 0==a.clientID?(console.log("No Client ID Provided"),this.success=!1,!1):(this.data.clientID=a.clientID,this.dataCallback=void 0!=a.dataCallback?a.dataCallback:function(){},void 0==a.search?(console.log("No Hashtag Provided"),this.success=!1,!1):(this.data.search=this.data.searched=a.search,this.validateData=function(a){return this.set("loading",!1),400==a.meta.code?(this.error=!0,this.set("message","Invalid Client ID"),this.dataCallback(a),!1):200==a.meta.code?(this.error=!1,!0):(this.error=!0,this.set("message","Error retrieving IG data."),this.dataCallback(a),console.log(a),!1)},this.replaceData=function(a){void 0==a.data?this.set("message","Sorry no results for #"+this.data.search+" :["):(this.set("instagramData",a),this.set("searched",this.data.search),this.set("message",""),this.set("endOfFeed",!1)),this.dataCallback(a)},window.instagramReceiverReplace=function(a){return function(b){a.validateData(b)&&a.replaceData(b)}}(this),this.frontAppend=function(a){0==a.data.length?console.log("Nothing to append"):(console.log("front Append"),this.set("instagramData.data",a.data.concat(this.data.instagramData.data)),this.data.instagramData.pagination.min_tag_id=a.pagination.min_tag_id),this.dataCallback(a)},window.instagramReceiverFrontAppend=function(a){return function(b){a.validateData(b)&&a.frontAppend(b)}}(this),this.rearAppend=function(a){console.log("rear Append"),this.set("instagramData.data",this.data.instagramData.data.concat(a.data)),void 0===a.pagination.next_max_tag_id?this.set("endOfFeed",!0):this.data.instagramData.pagination.next_max_tag_id=a.pagination.next_max_tag_id,this.dataCallback(a)},window.instagramReceiverRearAppend=function(a){return function(b){a.validateData(b)&&a.rearAppend(b)}}(this),this.observe("search",function(){this.load("replace")}),void this.load("replace")))}}); \ No newline at end of file diff --git a/test/tests.js b/test/tests.js index d05b81f..0d36446 100644 --- a/test/tests.js +++ b/test/tests.js @@ -13,6 +13,31 @@ $.get( '../templates/twitter-feed.rac').then( function ( val ) { ); });//Get Template +/** Compares the two arrays for any repeated IDs. + * + */ +function hasDuplicates(newData, oldData){ + for (var i = newData.data.length - 1; i >= 0; i--) { + for (var j = oldData.length - 1; j >= 0; j--) { + oldData[j]; + }; + }; + return false; +} + +function testLoadDuplicates(insta){ + insta.load('before', function(returnedData){ + test('Checking for duplicated in before method.', function(){ + ok(hasDuplicates(insta.data.instagramData, returnedData) == false, 'Load("Before") did not load any duplicate data.'); + }); + }); + insta.load('after', function(returnedData){ + test('Checking for loading data with Before and After methods.', function(){ + ok(hasDuplicates(insta.data.instagramData, returnedData) == false, 'Load("After") did not load any duplicate data.'); + }); + }); +} + testBadClientID = function(nextTest){ /** * Initialize our Template @@ -46,15 +71,19 @@ makeCorrectFeed = function(nextTest){ instaGood = new instagramFeed({ el: 'template-target', template: template, - clientID: 'dsa', + clientID: 'fd88310566744275a3d68092d9c175d1', search: 'dribbble', - dataCallback: function(){ - - }, complete: function(){ + var insta = this; test('Instagram object created with good Client ID.', function(){ ok(true, "Created!"); }); + window.setTimeout(function(){ + //wait for a second, then try loading more data. + testLoadDuplicates(insta); + //wait for a second, then try loading more data. + testLoadDuplicates(insta); + }, 1000) } });//Ractive init -}; +}; \ No newline at end of file