Twitcher is a tiny little JavaScript library I made for consuming the Twitter Search API.

And that’s it. It does not do anything else.

I made it for a variety of reasons, only a couple of which have anything to do with Twitter. Following along in the source code:

  1. I wanted to experiment with JavaScript patterns.

    I looked at a bunch of different libraries for inspiration (mostly jQuery). The Twitcher library is built in an anonymous function, or a closure. Everything is defined within and then a small hole is poked into the global object, like so:

    window.Twitcher = function(query, parameters) {
    	return new Twitcher(query,parameters);
    };
    

    This makes it so that calling Twitcher() instantiates a new Twitcher object without using the new constructor.

    Then, I extend the window.Twitcher object with the quarantined Twitcher:

    Twitcher.Extensions.extend(window.Twitcher, Twitcher);
    

    So we end up giving the window.Twitcher object all the power (and prototype) of the quarantined Twitcher. Not sure why I decided to do that. Seemed like a good idea at the time and I am pretty happy with the resulting API.

  2. I wanted to experiment with JSONP.

    I wanted to crack open jQuery and see how it did JSONP. The result is Twitcher. Twitcher is really just some Twitter specifics on top of a minimal JSONP interface. Here is the JSONP method, simply:

    	
    jsonp: function(url, callback, callback_name) {
      var head = document.getElementsByTagName("head")[0];
      var script = document.createElement("script");
      script.type = "text/javascript";
      script.src = url;
    
      window[callback_name] = function(data) {
        callback(data);
        window[callback_name] = undefined;
        try{ delete window[callback_name]; } catch(e){}
        head.removeChild(script);
      };
    
      head.appendChild(script);
    }
    
  3. I wanted to experiment with JavaScript testing.

    I like the idea of a strong testing suite, Behavior Driven Development, unit testing, and all that stuff that goes along with it. I use RSpec for Weary and wanted to explore doing tests in JavaScript. I decided on JSpec, only because it seemed familiar. JavaScript testing is hard. It’s hard because Browsers suck. All of this stuff gets in your way and it makes testing a mental hurdle. But once you get past that obstacle, it’s a great idea. I found that testing encouraged me to break up methods into smaller and smaller parts, which is a good thing. I’d like to test more, but I am running into the same hurdle I reached with testing for Weary: it is hard to test web services. I’ve had some ideas though and my experience with JSpec was a pleasant one.

So that is Twitcher. A library-agnostic Twitter Search API. The only thing it writes out to the DOM is the stuff it needs to perform the Ajax call. It returns data, and that’s it. I created Twitcher as a proof of concept and hope you find a use for it. Or not. You could just use jQuery.