<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 <title>Alex Payne</title>
 <link href="http://wonderfullyflawed.com/atom.xml" rel="self"/>
 <link href="http://wonderfullyflawed.com/"/>
 <updated>2011-07-15T09:11:54-07:00</updated>
 <id>http://wonderfullyflawed.com/</id>
 <author>
   <name>Trek Glowacki</name>
   <email>trek.glowacki@gmail.com</email>
 </author>
 
 <entry>
   <title>Books</title>
   <link href="http://wonderfullyflawed.com/2010/08/15/books.html"/>
   <updated>2010-08-15T00:00:00-07:00</updated>
   <id>http://wonderfullyflawed.com/2010/08/15/books</id>
   <content type="html">&lt;p&gt;Let’s Make a an Observer Library
Most books and articles on javascript trends towards two extremes: basic instruction in the execution and idiomatic patterns of javascript as a language and basic usage of popular browser-focused utility libraries written in the language. In the first category I group &lt;a href=&quot;http://www.amazon.com/JavaScript-Definitive-Guide-David-Flanagan/dp/0596101996/&quot;&gt;The Definitive Guide&lt;/a&gt;, Crockford's &lt;a href=&quot;http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742&quot;&gt;The Good Parts&lt;/a&gt;, Resig's &lt;a href=&quot;http://www.amazon.com/Pro-JavaScript-Techniques-John-Resig/dp/1590597273/&quot;&gt;Pro JavaScript Techniques&lt;/a&gt; and Harnes &amp;amp; Diaz's &lt;a href=&quot;http://www.amazon.com/JavaScript-Design-Patterns-Recipes-Problem-Solution/dp/159059908X&quot;&gt;Pro JavaScript Design Patterns&lt;/a&gt;; in the second, &lt;a href=&quot;http://www.amazon.com/jQuery-Novice-Ninja-Earle-Castledine/dp/0980576857/ref=sr_1_7?s=books&amp;amp;ie=UTF8&amp;amp;qid=1281890549&amp;amp;sr=1-7&quot;&gt;jQuery, Novice to Ninja&lt;/a&gt;, &lt;a href=&quot;http://www.amazon.com/Mastering-Dojo-JavaScript-Experiences-Programmers/dp/1934356115/ref=sr_1_4?s=books&amp;amp;ie=UTF8&amp;amp;qid=1281890571&amp;amp;sr=1-4&quot;&gt;Mastering Dojo&lt;/a&gt;, and &lt;a href=&quot;http://www.amazon.com/Practical-Prototype-script-aculo-us-Experts-Development/dp/1590599195/ref=sr_1_3?s=books&amp;amp;ie=UTF8&amp;amp;qid=1281890591&amp;amp;sr=1-3&quot;&gt;Practical Prototype and Scrtip.aculo.us&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In the wide gulf between these two sets there is almost nothing written. This leaves an intermediate javascript developer adrift. You understand basic language constructs, prototypal inheritance, and functional programming patterns. You've built a few browsed-based applications so you're accustomed to the DOM and user-generated event-based programming.&lt;/p&gt;

&lt;p&gt;But, like me, you're probably stumped when it comes to writing your own reusable libraries or organizing a large application. The flexibility everyone lauds in language can be an idiomatic quagmire when you're between the basics and off-the-shelf utility libraries.&lt;/p&gt;

&lt;p&gt;Given all this, I was super geeked when &lt;a href=&quot;http://alexyoung.org/&quot;&gt;Alex Young&lt;/a&gt; started a new series of articles entitled &lt;a href=&quot;http://dailyjs.com/2010/02/18/framework/&quot;&gt;Let's Make a Framework&lt;/a&gt;.  This is some of the best middle ground writing about JavaScript I've read.&lt;/p&gt;

&lt;p&gt;I'd like to add to the body of work in this area, so over the next few weeks I'll be series of posts that explore writing a javascript implementation of the observer pattern.  Like Alex's series, this goal is creating an educational exercise for the reader, not to craft a ground-breaking library.&lt;/p&gt;

&lt;p&gt;I'm going to cover&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the basic observer pattern&lt;/li&gt;
&lt;li&gt;event based programming&lt;/li&gt;
&lt;li&gt;property change observation&lt;/li&gt;
&lt;li&gt;data syncing through observation&lt;/li&gt;
&lt;li&gt;existing libraries that use these techniques&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Doing this, I'll focus on some modern development techniques:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;behavior driven development&lt;/li&gt;
&lt;li&gt;source control&lt;/li&gt;
&lt;li&gt;API design&lt;/li&gt;
&lt;/ul&gt;


&lt;h1&gt;Let’s Make a an Observer Library. Part I: Library Architecture  &lt;/h1&gt;

&lt;p&gt;Welcome to part 1 of Writing an Observer Library, a series of posts about building a JavaScript library to implement the various flavors of the observer pattern. In this part I’m going to discuss library architectures, and lay down the design for our framework.&lt;/p&gt;

&lt;p&gt;This series is inspired by &lt;a href=&quot;http://alexyoung.org/&quot;&gt;Alex Young&lt;/a&gt;'s series of articles entitled &lt;a href=&quot;http://dailyjs.com/2010/02/18/framework/&quot;&gt;Let's Make a Framework&lt;/a&gt;. Like Alex's, this series is designed to be educational. Also like Alex, we're going to define a set of practices to govern the library's crafting:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Verbose&lt;/strong&gt;: Variable and method names should be verbose so things are easy to find and understand&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Portable&lt;/strong&gt;: Browsers and console should be catered for&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Indentation&lt;/strong&gt;: Two spaces&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;API-focused&lt;/strong&gt;: a consistent, organized API is valued over other practices&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tested&lt;/strong&gt;: Everything is tested (first) in JSpec&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Versioning&lt;/strong&gt;: We'll be keeping code in github&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Documented&lt;/strong&gt;: Everything should be well documented and a usage guide will be provided&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Neighborly&lt;/strong&gt;: We'll be sure not stop on global scope toes, avoid extending the prototype of included types, and use existing libraries if they provide functionality outside the scope of the what we're writing (dependencies are ok, folks)&lt;/li&gt;
&lt;/ul&gt;

</content>
   <author>
     <name>Trek Glowacki</name>
     <uri>http://wonderfullyflawed.com/about.html</uri>
   </author>
 </entry>
 
 <entry>
   <title>Has Many, with Precedence</title>
   <link href="http://wonderfullyflawed.com/2010/06/20/has-many-with-precedence.html"/>
   <updated>2010-06-20T00:00:00-07:00</updated>
   <id>http://wonderfullyflawed.com/2010/06/20/has-many-with-precedence</id>
   <content type="html">&lt;p&gt;Every so often I have a &lt;code&gt;has_many&lt;/code&gt; ActiveRecord relationship with a twist: one (and only one) of the many possible related items is special in some way and I want to be get and set it like other associations.  Imagine a &lt;code&gt;Customer&lt;/code&gt; has several phone numbers, but only one is their primary contact number.&lt;/p&gt;

&lt;p&gt;ActiveRecord has a few strategies for multiple, similar relationships. You could use association extensions and define a sub-assocation like so:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Customer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Base&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;has_many&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:phone_numbers&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;priamry&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:conditions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:is_primary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;which will allow you to retrieve the primary phone number, but not set it:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Customer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phone_numbers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primary&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# =&amp;gt; &amp;lt;PhoneNumber id: 1 number: &amp;#39;5551234&amp;#39;, is_primary true&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phone_numbers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:number&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;5551235&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# =&amp;gt; NoMethodError: undefined method `primary=&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;ActiveRecord lets you define multiple relationships to the same class with some configuration:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Customer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Base&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;has_many&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:phone_numbers&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;has_many&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:primary_phone_numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
           &lt;span class=&quot;ss&quot;&gt;:class_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;PhoneNumber&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
           &lt;span class=&quot;ss&quot;&gt;:conditions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:is_primary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;This will let you ask for either collection, assign new or existing objects to either collection, and even use the &lt;code&gt;build&lt;/code&gt; and &lt;code&gt;create&lt;/code&gt; methods on the collection to get new objects with the attributes pre-assigned to match the &lt;code&gt;:conditions&lt;/code&gt; you supplied:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Customer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phone_numbers&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#    [&amp;lt;PhoneNumber id: 1 number: &amp;#39;5551234&amp;#39;, is_primary true,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#    &amp;lt;PhoneNumber id: 2 number: &amp;#39;5551234&amp;#39;, is_primary false &amp;gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primary_phone_numbers&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#    [&amp;lt;PhoneNumber id: 1 number: &amp;#39;5551234&amp;#39;, is_primary true]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;The problem here is that you always get a collection back, and will need additional code to enforce the existence of a single primary phone number for a customer.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Customer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phone_numbers&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#    [&amp;lt;PhoneNumber id: 1 number: &amp;#39;5551234&amp;#39;, is_primary true,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#    &amp;lt;PhoneNumber id: 2 number: &amp;#39;5551234&amp;#39;, is_primary false &amp;gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primary_phone_numbers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:number&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;555 9081&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#    [&amp;lt;PhoneNumber id: 3 number: &amp;#39;5559081&amp;#39;, is_primary true]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primary_phone_numbers&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# two primary phone nubmers!&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# [&amp;lt;PhoneNumber id: 1 number: &amp;#39;5551234&amp;#39;, is_primary true,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#   &amp;lt;PhoneNumber id: 3 number: &amp;#39;5559081&amp;#39;, is_primary true]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;You might think, at first, the solution is to has &lt;code&gt;has_one&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Customer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Base&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;has_many&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:phone_numbers&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;has_one&lt;/span&gt;  &lt;span class=&quot;ss&quot;&gt;:primary_phone_number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
           &lt;span class=&quot;ss&quot;&gt;:class_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;PhoneNumber&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
           &lt;span class=&quot;ss&quot;&gt;:conditions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:is_primary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;This will work fine for retrieving, but you'll run into trouble when trying to assign. Part of the job of a &lt;code&gt;has_one&lt;/code&gt; relationship is ensuring that the foreign key appears only once for the type of association.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Customer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phone_numbers&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#    [&amp;lt;PhoneNumber id: 1 number: &amp;#39;5551234&amp;#39;, is_primary true,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#    &amp;lt;PhoneNumber id: 2 number: &amp;#39;5551234&amp;#39;, is_primary false &amp;gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primary_phone_number&lt;/span&gt; 
&lt;span class=&quot;c1&quot;&gt;#    &amp;lt;PhoneNumber id: 1 number: &amp;#39;5551234&amp;#39;, is_primary true&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primary_phone_number&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Number&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:number&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;555 0919&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#    &amp;lt;PhoneNumber id: 3 number: &amp;#39;5550919&amp;#39;, is_primary true&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reload&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phone_numbers&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# foreign keys cleared&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# [&amp;lt;PhoneNumber id: 3 number: &amp;#39;5550919&amp;#39;, is_primary true&amp;gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;The solution I'm currently using is to flip the relationship (and where the foreign key is stored) by making a Customer belong_to primary PhoneNumber&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Customer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Base&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;has_many&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:phone_numbers&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;belongs_to&lt;/span&gt;  &lt;span class=&quot;ss&quot;&gt;:primary_phone_number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:class_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;PhoneNumber&amp;#39;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Now we can get and set a primary phone number without overwriting the old ones. Unfortunately we have the complementary problem to the one above. When we ask for &lt;code&gt;phone_numbers&lt;/code&gt; the collection won't include the primary phone number (since it lacks the &lt;code&gt;customer_id&lt;/code&gt; in the &lt;code&gt;phone_numbers&lt;/code&gt; table)&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;  &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phone_numbers&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;#    [&amp;lt;PhoneNumber id: 1 number: &amp;#39;5551234&amp;#39;&amp;gt;]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primary_phone_number&lt;/span&gt; 
  &lt;span class=&quot;c1&quot;&gt;#    nil&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;primary_phone_number&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Number&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:number&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;555 0919&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;#    &amp;lt;PhoneNumber id: 3 number: &amp;#39;5550919&amp;#39;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reload&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phone_numbers&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# doesn&amp;#39;t include the primary phone number&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# [&amp;lt;PhoneNumber id: 1 number: &amp;#39;5551234&amp;#39;&amp;gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;In the future, We'll be able to solve this with a &lt;code&gt;before_add&lt;/code&gt; callback whenever &lt;a href=&quot;https://rails.lighthouseapp.com/projects/8994/tickets/1564-add-support-for-before-and-after-addremove-callbacks-to-belongs_to-associations&quot;&gt;this patch is applied&lt;/a&gt; (right now they only work for &lt;code&gt;has_many&lt;/code&gt; associations):&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;belongs_to&lt;/span&gt;  &lt;span class=&quot;ss&quot;&gt;:primary_phone_number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
            &lt;span class=&quot;ss&quot;&gt;:class_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;PhoneNumber&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
            &lt;span class=&quot;ss&quot;&gt;:before_add&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:add_to_phone_numbers&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;add_to_phone_numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phone_number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phone_numbers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;phone_number&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Until then, you can work around the limitation with some &lt;code&gt;alias_method_chain&lt;/code&gt; trickery:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;belongs_to&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:phone_number&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;phone_number_with_callback&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phone_number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phone_numbers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;phone_number&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phone_number_without_callback&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;phone_number&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;alias_method_chain&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:phone_number&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:callback&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;



</content>
   <author>
     <name>Trek Glowacki</name>
     <uri>http://wonderfullyflawed.com/about.html</uri>
   </author>
 </entry>
 
 <entry>
   <title>Multiparameter assignment and attr_accessor</title>
   <link href="http://wonderfullyflawed.com/2010/06/16/multi-paramter-assignment-and-attr-accessor.html"/>
   <updated>2010-06-16T00:00:00-07:00</updated>
   <id>http://wonderfullyflawed.com/2010/06/16/multi-paramter-assignment-and-attr-accessor</id>
   <content type="html">&lt;p&gt;I've needed multiparamter assignment on non-persisted attributes for a few projects (usually on for &lt;code&gt;Date&lt;/code&gt; and &lt;code&gt;Time&lt;/code&gt; objects). Typically,  I cheat and send separate attribute and have a &lt;code&gt;before_validation&lt;/code&gt; callback to combine them into a date. I finally sat down and sussed out how to do it properly:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;kp&quot;&gt;attr_accessor&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:initial_billing_day&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
              &lt;span class=&quot;ss&quot;&gt;:initial_billing_month&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
              &lt;span class=&quot;ss&quot;&gt;:initial_billing_year&lt;/span&gt;
              
&lt;span class=&quot;n&quot;&gt;composed_of&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:initial_billing_date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
  &lt;span class=&quot;ss&quot;&gt;:class_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Date&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
  &lt;span class=&quot;ss&quot;&gt;:mapping&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;sx&quot;&gt;%w(initial_billing_day day)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;sx&quot;&gt;%w(initial_billing_year year)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;sx&quot;&gt;%w(initial_billing_month month)&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:constructor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;proc&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;unless&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;



</content>
   <author>
     <name>Trek Glowacki</name>
     <uri>http://wonderfullyflawed.com/about.html</uri>
   </author>
 </entry>
 
 <entry>
   <title>Format Parsing in Javascript</title>
   <link href="http://wonderfullyflawed.com/2010/06/07/format-parsing-in-javascript.html"/>
   <updated>2010-06-07T00:00:00-07:00</updated>
   <id>http://wonderfullyflawed.com/2010/06/07/format-parsing-in-javascript</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;http://github.com/jessesielaff/Riff&quot;&gt;Riff&lt;/a&gt; is a a recent spin-off project from &lt;a href=&quot;http://github.com/jessesielaff/&quot;&gt;Jesse Sielaff&lt;/a&gt;&amp;#8217;s work to bring a Ruby-like language interpreter to the browser.  Riff is a plugin for &lt;a href=&quot;http://i.loveruby.net/en/projects/racc/&quot;&gt;Racc&lt;/a&gt; (a &lt;a href=&quot;http://en.wikipedia.org/wiki/LALR_parser&quot;&gt;LARL(1) parser generator&lt;/a&gt; for Ruby).  Riff extends the output generation portions of Racc and writes javascript instead of Ruby.  If you&amp;#8217;re familiar with LALR grammars this should add a handy tool to your javascript arsenal.&lt;/p&gt;




&lt;p&gt;Jesse&amp;#8217;s been using Riff in his latest stab at Red, but we were eager to throw it at a smaller text format and see how it compared to a solid hand-written parser.  We took a weekend and paired on rewriting &lt;a href=&quot;http://github.com/janl&quot;&gt;Jan Lehnardt&lt;/a&gt;&amp;#8217;s &lt;a href=&quot;http://github.com/janl/mustache.js&quot;&gt;javascript implementation&lt;/a&gt; of &lt;a href=&quot;http://github.com/defunkt&quot;&gt;Chris Wanstrath&lt;/a&gt;&amp;#8217;s &lt;a href=&quot;http://mustache.github.com/&quot;&gt;mustache template language&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Our &lt;code&gt;mustache.js&lt;/code&gt; has three little differences from Jan&amp;#8217;s (we wanted to keep as close to Chris&amp;#8217; Ruby/Python mustache implementations as possible):&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;no explicit iterator &lt;code&gt;{{.}}&lt;/code&gt; (use &lt;code&gt;{{ toString }}&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;partial context is stored in Mustache.partials instead of passed as an argument. You can switch this context to a different object when you need to.&lt;/li&gt;
&lt;li&gt;we have &lt;code&gt;Mustache.render(template, object)&lt;/code&gt; instead of &lt;code&gt;Mustache.to_html(template, object, partials, callback)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Other than those, the two parsers are roughly identical in functionality but quite distinct in implementation. Jan&amp;#8217;s &lt;code&gt;mustache.js&lt;/code&gt; is a handwritten parser, our&amp;#8217;s is generated from &lt;code&gt;Riff&lt;/code&gt; and &lt;code&gt;Racc&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;A generated parser is broken up into two main parts: grammar definition and semantic action.  Take a look at &lt;a href=&quot;http://github.com/jessesielaff/mustache.js/blob/b8977508546067e97a9be2d13033332da902f032/compile/mustache.grammar&quot;&gt;Mustache&amp;#8217;s grammar&lt;/a&gt;.  The first part defines the grammar of the language (it&amp;#8217;s pretty small), from which a finite state machine is generated.  The part of the grammar enclosed in &lt;code&gt;{&lt;/code&gt; and &lt;code&gt;}&lt;/code&gt; defines the semantic rule conversion (e.g. what to do when a token is found).&lt;/p&gt;




&lt;p&gt;For example, &lt;a href=&quot;http://github.com/jessesielaff/mustache.js/blob/b8977508546067e97a9be2d13033332da902f032/compile/mustache.grammar#L13&quot;&gt;line 13&lt;/a&gt; of our grammar looks like this:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;&quot;{{{&quot; &quot;text&quot; &quot;}}}&quot;  { $$ = Array(4, $2); }
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;This means when you find &lt;code&gt;}&lt;/code&gt;, create a new &lt;code&gt;Array&lt;/code&gt; object with &lt;code&gt;4&lt;/code&gt; (which is the arbitrary code number we gave to &amp;#8220;escaped text&amp;#8221; nodes) as the first element and the string value of the &amp;#8220;text&amp;#8221; node as the second element.  What you choose to do in your parser depends how you&amp;#8217;d like to handle evaluation. You might create javascript literals, use a jquery class, etc. We&amp;#8217;ve opted to create a tree of nodes using &lt;code&gt;Array&lt;/code&gt; objects in javascript, but a different solution might have been:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;&quot;{{{&quot; &quot;text&quot; &quot;}}}&quot;  { $$ = { node_type:'unescaped-text', content: $2 } }
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;The grammar file is run through &lt;code&gt;Riff&lt;/code&gt; and &lt;a href=&quot;http://github.com/jessesielaff/mustache.js/blob/b8977508546067e97a9be2d13033332da902f032/mustache.js&quot;&gt;converted to generated parser&lt;/a&gt;. Everything up to &lt;a href=&quot;/blob/b8977508546067e97a9be2d13033332da902f032/&quot;&gt;line 214&lt;/a&gt; is generated, the remainder is copied over from the &lt;code&gt;---- footer&lt;/code&gt; section of the grammar file.&lt;/p&gt;




&lt;p&gt;Initially the footer for &lt;code&gt;Mustache&lt;/code&gt; contained every function necessary to parse, evaluate, and render a mustache formatted file.  Jesse later separated this into two separate sections – a parser and an evaluator – and the end results are pretty awesome (big reveal later, I promise).&lt;/p&gt;




&lt;p&gt;Starting on line 214, we have two major functions of &lt;code&gt;Mustache&lt;/code&gt;: &lt;code&gt;compile&lt;/code&gt; and &lt;code&gt;next_token&lt;/code&gt;. &lt;code&gt;compile&lt;/code&gt; sets up a state for parsing and calls &lt;code&gt;do_parse&lt;/code&gt; (which is generated by &lt;code&gt;Riff&lt;/code&gt; from the grammar, not written by the programmer). &lt;code&gt;next_token&lt;/code&gt; is called from inside &lt;code&gt;do_parse&lt;/code&gt; and walks through the input string character by character, searching for semantically meaningful elements (e.g. &lt;code&gt;{{#something}}&lt;/code&gt;).&lt;/p&gt;




&lt;p&gt;This part can be a little dense if you&amp;#8217;re unfamiliar with look-ahead parsing. If you can&amp;#8217;t visualize the pointer moving through a string, see the &lt;a href=&quot;http://github.com/jessesielaff/mustache.js/blob/StringScanner/mustache.js#L256&quot;&gt;StringScanner branch&lt;/a&gt;, where Jesse uses &lt;a href=&quot;http://github.com/jessesielaff/StringScanner.js&quot;&gt;his javascript port of Ruby&amp;#8217;s string scanner class&lt;/a&gt; and regular expressions to manipulate the string pointer.&lt;/p&gt;




&lt;p&gt;So, this code gets you a tree of Array objects. The &lt;a href=&quot;http://github.com/jessesielaff/mustache.js/blob/b8977508546067e97a9be2d13033332da902f032/mustache.evaluator.js&quot;&gt;evaluator part&lt;/a&gt; of the library takes this tree and renders it into a string with the appropriate values inserted.  The main mojo in the evaluator is the &lt;code&gt;render&lt;/code&gt; function. This is the only function you&amp;#8217;ll call in &lt;em&gt;your&lt;/em&gt; code with &lt;code&gt;Mustache.render(template, object)&lt;/code&gt;.  The evaluator is organized into one function for semantic type (e.g. &lt;code&gt;evaluate_inverse_block_node&lt;/code&gt; for &lt;code&gt;{{^foo}}&lt;/code&gt; inverse blocks)&lt;/p&gt;




&lt;p&gt;Internally, &lt;code&gt;render&lt;/code&gt; checks the type of template object you passed in. Up until now, we&amp;#8217;ve talked about templates only as strings in mustache format. If you pass in a &lt;code&gt;String&lt;/code&gt; template, it runs through &lt;code&gt;Mustache.compile&lt;/code&gt; and comes out the other end as the series of &lt;code&gt;Array&lt;/code&gt;s mentioned above, is evaluated, and finally is converted back into a &lt;code&gt;String&lt;/code&gt; with data inserted in appropriate locations.&lt;/p&gt;




&lt;p&gt;Now, imagine we could &lt;em&gt;start&lt;/em&gt; with a series of &lt;code&gt;Array&lt;/code&gt;s and skip the parsing stage.  Obviously, nobody wants to develop their templates like this:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;[0,[0,[0,[0,[0,[0,[0,[0,'&amp;lt;img src=&quot;',[3,'src']],'&quot; width=&quot;'],[3,'width']],'&quot; height=&quot;'],[3,'height']],'&quot; alt=&quot;'],[3,'name']],'&quot;&amp;gt;']
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;we want to write templates like this:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;&amp;lt;img src=&quot;{{src}}&quot; width=&quot;{{width}}&quot; height=&quot;{{height}}&quot; alt=&quot;{{name}}&quot;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;In production, of course, the computer doesn&amp;#8217;t care what your templates look like. If you&amp;#8217;re working with javascript on the server, you can pre-compile your templates with &lt;code&gt;Mustache.compile&lt;/code&gt;. Jesse also whipped up an &lt;a href=&quot;http://github.com/jessesielaff/mustache.js/blob/b8977508546067e97a9be2d13033332da902f032/mustache&quot;&gt;example pre-compiler&lt;/a&gt; for mustache in Ruby in case you have different server needs.&lt;/p&gt;




&lt;p&gt;This let&amp;#8217;s us a use &lt;code&gt;mustache.js&lt;/code&gt;, &lt;code&gt;mustache.evaluator.js&lt;/code&gt;, and &lt;code&gt;String&lt;/code&gt; templates while developing and move to &lt;em&gt;just&lt;/em&gt; &lt;code&gt;mustache.evaluator.js&lt;/code&gt; (which is a tiny, tiny file) and compiled templates when we&amp;#8217;re ready to move into production and want a speed-boost.&lt;/p&gt;




&lt;h2 id=&quot;how8217d_riff_do&quot;&gt;How&amp;#8217;d Riff do?&lt;/h2&gt;




&lt;p&gt;Not bad for a weekend pairing project. We went from nothing to a spec&amp;#8217;ed, running format parser in two afternoons.  Adjusting for comments and code style (Jesse is more liberal with his white space around curly braces), there&amp;#8217;s 20% fewer lines of code over the handwritten parser. Run through a javascript minifier (Dean Edward&amp;#8217;s in this example), Jan&amp;#8217;s parser is 8,747 bytes, Jesse&amp;#8217;s 6,292 bytes, and in production, you can get by with just the evaluator at 2,244 bytes.&lt;/p&gt;




&lt;p&gt;Organizationally, I prefer separating out language definition, parsing, and evaluation. I personally find it reads more easily than putting everything in a single structure. Plus, it gives us the benefit of using compiled templates.&lt;/p&gt;




&lt;p&gt;Speedwise, Jan&amp;#8217;s &lt;code&gt;Mustache&lt;/code&gt; ranges from identical up to 4x faster (depending the example), averaging 1.3x faster than ours. But as &lt;a href=&quot;http://twitter.com/janl/status/15443198351&quot;&gt;Jan kindly noted&lt;/a&gt; code size and simplicity are probably more important than incremental speed gains. Plus, if you&amp;#8217;re desperate for cycles, using compiled templates is 14x faster (9x faster than Jan&amp;#8217;s).&lt;/p&gt;




&lt;p&gt;Here are the &lt;a href=&quot;http://github.com/trek/mustache-speed-shootout&quot;&gt;speed test&lt;/a&gt; results (run in Firefox, on my last-gen Macbook):&lt;/p&gt;




&lt;p&gt;&lt;table&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;template&lt;/th&gt;
        &lt;th&gt;janl&lt;/th&gt;
        &lt;th&gt;jesse&lt;/th&gt;
        &lt;th&gt;jesse compiled&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;th&gt;basic&lt;/th&gt;
        &lt;td&gt;207ms&lt;/td&gt;
        &lt;td&gt;356ms&lt;/td&gt;
        &lt;td&gt;25ms&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;th&gt;complex&lt;/th&gt;
        &lt;td&gt;676ms&lt;/td&gt;
        &lt;td&gt;885ms&lt;/td&gt;
        &lt;td&gt;74ms&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;th&gt;friend&lt;/th&gt;
        &lt;td&gt;202ms&lt;/td&gt;
        &lt;td&gt;227ms&lt;/td&gt;
        &lt;td&gt;22ms&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;th&gt;long&lt;/th&gt;
        &lt;td&gt;192ms&lt;/td&gt;
        &lt;td&gt;450ms&lt;/td&gt;
        &lt;td&gt;29ms&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;th&gt;looping&lt;/th&gt;
        &lt;td&gt;278ms&lt;/td&gt;
        &lt;td&gt;354ms&lt;/td&gt;
        &lt;td&gt;25ms&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;th&gt;partial&lt;/th&gt;
        &lt;td&gt;892ms&lt;/td&gt;
        &lt;td&gt;945ms&lt;/td&gt;
        &lt;td&gt;81ms&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;&lt;/td&gt;
        &lt;td&gt;2447&lt;/td&gt;
        &lt;td&gt;3217&lt;/td&gt;
        &lt;td&gt;256&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;&lt;/p&gt;




&lt;p&gt;If you find yourself hand-parsing text in javascript, give Riff a try. We think you&amp;#8217;ll like it.&lt;/p&gt;

</content>
   <author>
     <name>Trek Glowacki</name>
     <uri>http://wonderfullyflawed.com/about.html</uri>
   </author>
 </entry>
 
 <entry>
   <title>Automating iTunes Gift Cards</title>
   <link href="http://wonderfullyflawed.com/2010/01/14/automating-itunes-gift-cards.html"/>
   <updated>2010-01-14T00:00:00-08:00</updated>
   <id>http://wonderfullyflawed.com/2010/01/14/automating-itunes-gift-cards</id>
   <content type="html">&lt;p&gt;I'm finishing up a project for the &lt;a href='http://www.sph.umich.edu/'&gt; University of Michigan School of Public Health&lt;/a&gt; that involves using iTunes Gift Cards as a participant incentive. They've pre-bought about $20,000 of Gift Cards and given me the ids.&lt;/p&gt;

&lt;p&gt;We &lt;em&gt;could&lt;/em&gt; just have participants copy/paste their code into iTunes, but I wanted to create a smooth user experience for redeeming.&lt;/p&gt;

&lt;p&gt;The base url for iTunes Redeeming is &lt;code&gt;https://buy.itunes.apple.com/WebObjects/MZFinance.woa/wa/redeemLandingPage&lt;/code&gt; (you can drag links from iTunes into a text editor to get their url) but I wanted to pre-fill the code field or automatically submit my request with the code supplied.
&lt;img src='http://s3.amazonaws.com/ember/mXilF9dfifTTnbLcDScrP8Ma0vHYBBG1_o.png' /&gt;&lt;/p&gt;

&lt;p&gt;I suspected I could send a query parameter along with that url, but not being able to view source, I had no idea what to name it.  So, I went to a place where I've seen automatic click-to-redeem before, Apple's own &lt;a href='http://www.facebook.com/iTunes?ref=ts'&gt;iTunes Facebook fan page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Sure enough their fan page just sends a request to the same url with a query parameter of &lt;code&gt;?code=123456ABCDEF&lt;/code&gt;.  Pass that along with the url and it will send your redemption code along without user intervention and will display inline error messages if your code is bunk for some reason&lt;/p&gt;

&lt;p&gt;&lt;img src='http://img.skitch.com/20100114-m7ujrbdkuatuwq7pjkmtmfrayj.jpg' /&gt;&lt;/p&gt;
</content>
   <author>
     <name>Trek Glowacki</name>
     <uri>http://wonderfullyflawed.com/about.html</uri>
   </author>
 </entry>
 
 <entry>
   <title>Rails Foundations Workshops - Winter 2010</title>
   <link href="http://wonderfullyflawed.com/2010/01/07/rails-foundations-workshops-winter-2010.html"/>
   <updated>2010-01-07T00:00:00-08:00</updated>
   <id>http://wonderfullyflawed.com/2010/01/07/rails-foundations-workshops-winter-2010</id>
   <content type="html">&lt;p&gt;If you&amp;#8217;re in South East Michigan and serious about learning (or better understanding) Rails our upcoming &lt;a href=&quot;http://backtik.github.com&quot;&gt;&lt;em&gt;Rails Foundations&lt;/em&gt;&lt;/a&gt; workshop series is for you.&lt;/p&gt;




&lt;p&gt;This season, we're trying something new: name your own price workshops&lt;/p&gt;




&lt;p&gt;In the past we've offered the workshop for free and for pay. When free, we fill up same day and usually have 20+ people on the wait list.  When we charge we end up with about half as many participants as we'd like.  In the spirit of adventure (and capitalism!) we're letting people name their own price.  $5? $50? $5000? Totally up to you.&lt;/p&gt;




&lt;p&gt;At the end of a short bidding period, we're going to take a group of top bidders, and charge them all the lowest price from the group.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Rails Foundations&lt;/em&gt; focuses on giving attendees a solid understanding of major Rails components and how they interrelate. You&amp;#8217;ll find we teach no Rails magic, trickery, or shortcuts.&lt;/p&gt;




&lt;p&gt;We have a different approach for Rails training than most shops.  If you&amp;#8217;re looking for a grand tour of Rails, we recommended &lt;a href=&quot;http://www.bignerdranch.com/classes/beginning_ruby_on_rails.shtml&quot;&gt;Big Nerd Ranch&lt;/a&gt;, the &lt;a href=&quot;http://pragmaticstudio.com/rails&quot;&gt;Pragmatic Studio&lt;/a&gt;, or fellow Michiganders &lt;a href=&quot;http://ideafoundry.info/ruby-on-rails&quot;&gt;Idea Foundry&lt;/a&gt;.  If you want to explore just the core Rails concepts in greater depth (preparing you to do wider exploration on your own), we think you&amp;#8217;ll enjoy the series.&lt;/p&gt;




&lt;p&gt;If you&amp;#8217;ve never used Rails before, the workshops are right for you if&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;You&amp;#8217;ve programmed in an OOP language&lt;/li&gt;
&lt;li&gt;You&amp;#8217;ve written for the web, but didn&amp;#8217;t really use HTTP fully&lt;/li&gt;
&lt;li&gt;You like to keep up on current trends in development&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;If you&amp;#8217;ve programmed in Rails, the workshops will clear up some misconceptions if&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;Your controllers have goofy actions like &lt;code&gt;javascript_save&lt;/code&gt; and &lt;code&gt;about_us&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Your routes don&amp;#8217;t go beyond &lt;code&gt;map.connect ':controller/:action/:id'&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Your views are so abstracted you&amp;#8217;re not sure &lt;em&gt;what&lt;/em&gt; HTML they even output anymore&lt;/li&gt;
&lt;li&gt;You didn&amp;#8217;t know that &lt;code&gt;belongs_to&lt;/code&gt; is actually a class method that takes a symbol as its first argument&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;The course starts is just under two weeks, so &lt;a href=&quot;http://backtik.github.com&quot;&gt;grab a spot quickly&lt;/a&gt;!&lt;/p&gt;

</content>
   <author>
     <name>Trek Glowacki</name>
     <uri>http://wonderfullyflawed.com/about.html</uri>
   </author>
 </entry>
 
 <entry>
   <title>Scoped DOM Selection</title>
   <link href="http://wonderfullyflawed.com/2009/08/24/scoped-dom-selection.html"/>
   <updated>2009-08-24T00:00:00-07:00</updated>
   <id>http://wonderfullyflawed.com/2009/08/24/scoped-dom-selection</id>
   <content type="html">&lt;p&gt;While Jesse operates on the next version of Red, his never-quite-ready Ruby runtime built on top of Javascript, I&amp;#8217;ve been playing with RedShift, our Ruby DOM manipulation library by exploring Ruby idioms that would make common tasks more elegant.&lt;/p&gt;




&lt;p&gt;We already have a &lt;a href=&quot;http://red-js.rubyforge.org/red/rdoc/classes/Document.html#M000657&quot;&gt;&lt;code&gt;ready?&lt;/code&gt; method&lt;/a&gt; on the &lt;code&gt;Document&lt;/code&gt; class that is called when the DOM has been parsed and is ready.  It&amp;#8217;s the equivalent of jQuery&amp;#8217;s &lt;code&gt;$(document).ready(fn)&lt;/code&gt;, Prototype&amp;#8217;s &lt;code&gt;document.observe(&quot;dom:loaded&quot;, fn)&lt;/code&gt;, and MooTools&amp;#8217; &lt;code&gt;window.addEvent('domready', fn)&lt;/code&gt; functions.&lt;/p&gt;




&lt;p&gt;I was playing with this concept of meta-events and had the idea of using them for scoping code based on selectors. When you combine all your javascript into a single file to &lt;a href=&quot;http://developer.yahoo.com/performance/rules.html#minify&quot;&gt;reduce the number of HTTP requests for a page&lt;/a&gt;, you need a nice way of trigger certain behavior only for a subset of pages.  I usually have a few &lt;code&gt;class&lt;/code&gt;es and &lt;code&gt;id&lt;/code&gt;s attached to the &lt;code&gt;body&lt;/code&gt; tag of any page:&lt;/p&gt;




&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;body&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;show&amp;#39;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;people admin-viewing&amp;#39;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;




&lt;p&gt;This presence of these identifiers will trigger a subset of all possible document behavior. To replacing many &lt;code&gt;if&lt;/code&gt; statements I extended our &lt;a href=&quot;http://red-js.rubyforge.org/red/rdoc/classes/Document.html#M000652&quot;&gt;DOM-selection method&lt;/a&gt; to encapsulate this behavior in an optional block.&lt;/p&gt;




&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;no&quot;&gt;Document&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;#show.people&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;




&lt;p&gt;Although succinct, this fails aesthetically because it doesn&amp;#8217;t obviously announce what the hell the block is for. I moved the block behavior to a specific &lt;code&gt;found?&lt;/code&gt; method to mirror the syntax of &lt;code&gt;Document.ready?&lt;/code&gt; as a kind of meta-event:&lt;/p&gt;




&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;no&quot;&gt;Document&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;#show.people&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;found?&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# code here only takes place if the selector is found&lt;/span&gt;
  &lt;span class=&quot;no&quot;&gt;Document&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;#show.people&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;a&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_class&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;whatever&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;




&lt;p&gt;Missing from &lt;em&gt;this&lt;/em&gt; example was the ability to do sub-selects from within the block (which gives a nice speed boost and reduces duplication). I updated it to take a block argument.&lt;/p&gt;




&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;no&quot;&gt;Document&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;div#navigation&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;found?&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;page_nav&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# find all the links inside the navigation div&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;page_nav&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;a&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_class&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;whatever&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;




&lt;p&gt;I&amp;#8217;m eager to try pattern on existing projects, so I &lt;a href=&quot;http://gist.github.com/174395&quot;&gt;extended jQuery&lt;/a&gt; to accomplish the same goals:&lt;/p&gt;




&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;div#navigation&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;found&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// only execute if the selector finds elements&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// is the jQuery object of $(&amp;#39;div#navigation&amp;#39;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;



</content>
   <author>
     <name>Trek Glowacki</name>
     <uri>http://wonderfullyflawed.com/about.html</uri>
   </author>
 </entry>
 
 <entry>
   <title>Rails Foundations Workshops</title>
   <link href="http://wonderfullyflawed.com/2009/07/08/rails-foundations-workshops.html"/>
   <updated>2009-07-08T00:00:00-07:00</updated>
   <id>http://wonderfullyflawed.com/2009/07/08/rails-foundations-workshops</id>
   <content type="html">&lt;p&gt;If you&amp;#8217;re in South East Michigan and serious about learning (or better understanding) Rails our upcoming &lt;a href=&quot;http://register.backtik.com/rails-july-2009/&quot;&gt;&lt;em&gt;Rails Foundations&lt;/em&gt;&lt;/a&gt; workshop series is for you.  In the past we&amp;#8217;ve only offered this workshop to our friends and colleagues or by recommendation.  Based on the feedback of some people we highly respect we&amp;#8217;ve decided to make this series generally available.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Rails Foundations&lt;/em&gt; focuses on giving attendees a solid understanding of major Rails components and how they interrelate. You&amp;#8217;ll find we teach no Rails magic, trickery, or shortcuts.&lt;/p&gt;




&lt;p&gt;We have a different approach for Rails training than most shops.  If you&amp;#8217;re looking for a grand tour of Rails, we recommended &lt;a href=&quot;http://www.bignerdranch.com/classes/beginning_ruby_on_rails.shtml&quot;&gt;Big Nerd Ranch&lt;/a&gt;, the &lt;a href=&quot;http://pragmaticstudio.com/rails&quot;&gt;Pragmatic Studio&lt;/a&gt;, or fellow Michiganders &lt;a href=&quot;http://ideafoundry.info/ruby-on-rails&quot;&gt;Idea Foundry&lt;/a&gt;.  If you want to explore just the core Rails concepts in greater depth (preparing you to do wider exploration on your own), we think you&amp;#8217;ll enjoy the series.&lt;/p&gt;




&lt;p&gt;If you&amp;#8217;ve never used Rails before, the workshops are right for you if&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;You&amp;#8217;ve programmed in an OOP language&lt;/li&gt;
&lt;li&gt;You&amp;#8217;ve written for the web, but didn&amp;#8217;t really use HTTP fully&lt;/li&gt;
&lt;li&gt;You like to keep up on current trends in development&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;If you&amp;#8217;ve programmed in Rails, the workshops will clear up some misconceptions if&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;Your controllers have goofy actions like &lt;code&gt;javascript_save&lt;/code&gt; and &lt;code&gt;about_us&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Your routes don&amp;#8217;t go beyond &lt;code&gt;map.connect ':controller/:action/:id'&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Your views are so abstracted you&amp;#8217;re not sure &lt;em&gt;what&lt;/em&gt; HTML they even output anymore&lt;/li&gt;
&lt;li&gt;You didn&amp;#8217;t know that &lt;code&gt;belongs_to&lt;/code&gt; is actually a class method that takes a symbol as its first argument&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;The course starts is just under two weeks, so &lt;a href=&quot;http://register.backtik.com/rails-july-2009/&quot;&gt;grab a spot quickly&lt;/a&gt;!&lt;/p&gt;

</content>
   <author>
     <name>Trek Glowacki</name>
     <uri>http://wonderfullyflawed.com/about.html</uri>
   </author>
 </entry>
 
 <entry>
   <title>Get Your API Right</title>
   <link href="http://wonderfullyflawed.com/2009/07/02/get-your-api-right.html"/>
   <updated>2009-07-02T00:00:00-07:00</updated>
   <id>http://wonderfullyflawed.com/2009/07/02/get-your-api-right</id>
   <content type="html">&lt;p&gt;Every project I&amp;#8217;ve worked on in the last two years has heavily involved the use of web APIs. &lt;a href=&quot;http://www.libersy.com&quot;&gt;Libersy&lt;/a&gt; at the time (no idea about now) had an architecture that was extensively API based, even for communication between internal applications (an architecture I &lt;em&gt;strongly&lt;/em&gt; argued against, bee tea dubs).  Since then I&amp;#8217;ve futzed with web APIs almost exclusively. From very narrow focused uses like University of Michigan&amp;#8217;s &lt;a href=&quot;http://bluestream.dc.umich.edu/&quot;&gt;Bluestream Service&lt;/a&gt;, to more broad but still fairly local APIs like the &lt;a href=&quot;http://www.aadl.org/&quot;&gt;Ann Arbor District Library&amp;#8217;s&lt;/a&gt; soon-to-be-updated API, all the way to APIs of major web applications like Twitter and Flickr.&lt;/p&gt;




&lt;p&gt;Constant exposure has turned me into a bit of a snob: I can&amp;#8217;t stand working with a poorly designed API!  If you&amp;#8217;re about to design or release an API for the web and want to avoid the ire of your developers, I&amp;#8217;ve summed up the best (and worst) of what I&amp;#8217;ve seen into 8 rules:&lt;/p&gt;




&lt;h2&gt;1) Use HTTP&lt;/h2&gt;




&lt;p&gt;I&amp;#8217;ll grant that HTTP isn&amp;#8217;t perfect but it&amp;#8217;s at least well understood and nearly universal. Nobody is going to want to write against your custom application layer atop UDP. Nobody cares about the RPC system you built in 1997 that is &amp;#8220;running just fine.&amp;#8221;  Unless you have some major reasons HTTP cannot work and are willing to provide solid client libraries in a dozen or more languages, stick with HTTP.&lt;/p&gt;




&lt;h2&gt;2) Use Your Verbs&lt;/h2&gt;




&lt;p&gt;Every HTTP request comes with a verb. These verbs have &lt;a href=&quot;http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html&quot;&gt;&lt;em&gt;specific&lt;/em&gt; meaning&lt;/a&gt; and should be used correctly.  Use &lt;code&gt;GET&lt;/code&gt; to retrieve items, &lt;code&gt;POST&lt;/code&gt; to add new items to your service, &lt;code&gt;PUT&lt;/code&gt; to update existing items, &lt;code&gt;DELETE&lt;/code&gt; to remove items, and &lt;code&gt;HEAD&lt;/code&gt; for uses similar to &lt;code&gt;GET&lt;/code&gt; where the programmer doesn&amp;#8217;t need body content (e.g. cache checking).&lt;/p&gt;




&lt;p&gt;If you&amp;#8217;re concerned that not all client libraries implement the HTTP verbs (web browsers, for example, can&amp;#8217;t submit &lt;code&gt;PUT&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt; requests) allow for HTTP verb faking. The emerging convention is passing a &lt;code&gt;_method&lt;/code&gt; parameter as part of the request body with the verb as a lowercase value (&lt;code&gt;put&lt;/code&gt;,&lt;code&gt;delete&lt;/code&gt;, and &lt;code&gt;head&lt;/code&gt;).&lt;/p&gt;




&lt;p&gt;If you don&amp;#8217;t use all the verbs, you won&amp;#8217;t be able to give your users access to all possible actions on your data without resorting to specialty urls.  It&amp;#8217;s possible to create an API that uses &lt;em&gt;only&lt;/em&gt; &lt;code&gt;POST&lt;/code&gt; requests:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;POST /photos/create
POST /photos/show/decafbebad
POST /photos/update/decafbebad
POST /photos/delete/decafbebad
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;By doing this you&amp;#8217;re encoding the intended action directly in the url; this is needlessly redundant. HTTP already gives to a slot for specifying action (the verb). Just use it:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;POST   /photos
GET    /photos/decafbebad
PUT    /photos/decafbebad
DELETE /photos/decafbebad
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;Twitter&amp;#8217;s API tries to have it both ways with &lt;a href=&quot;http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-favorites%C2%A0destroy&quot;&gt;destroying favorites&lt;/a&gt;. You can use either &lt;code&gt;POST&lt;/code&gt; or &lt;code&gt;DELETE&lt;/code&gt; as your software allows. To support client libraries that can&amp;#8217;t &lt;code&gt;DELETE&lt;/code&gt;, the deletion is additionally referenced in the URI (http://twitter.com/favorites/destroy/&lt;em&gt;id&lt;/em&gt;.format).&lt;/p&gt;




&lt;p&gt;&lt;a href=&quot;http://www.flickr.com/services/api/request.rest.html&quot;&gt;Flickr only uses &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;POST&lt;/code&gt;&lt;/a&gt; necessitating specialty urls.&lt;/p&gt;




&lt;p&gt;If you&amp;#8217;re not allowing clients to create new data, or update/delete existing data on your system then &lt;em&gt;you do not have an API&lt;/em&gt;. You have a feed. There&amp;#8217;s nothing wrong providing read-only access to your data (it&amp;#8217;s laudable, in fact), but I&amp;#8217;m often disappointed to hear &amp;#8220;Yeah! We have an API&amp;#8221; only to find the person &lt;em&gt;really&lt;/em&gt; meant they offered a number of customizable data feeds as &lt;code&gt;XML&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;The current &lt;a href=&quot;http://www.blyberg.net/downloads/patrest_1.3_overview.pdf&quot;&gt;Ann Arbor Library API&lt;/a&gt; is really a data feed.&lt;/p&gt;




&lt;p&gt;&lt;a href=&quot;http://api.shopify.com/&quot;&gt;Shopify&amp;#8217;s API&lt;/a&gt; gets it right.&lt;/p&gt;




&lt;h2&gt;3) Keep Your URL/URIs Consistent&lt;/h2&gt;




&lt;p&gt;One of the most important principles of  &lt;a href=&quot;http://en.wikipedia.org/wiki/Representational_State_Transfer&quot;&gt;REST&lt;/a&gt; (which is quickly becoming the preferred method of organizing web APIs) is &amp;#8220;every resource is uniquely addressable using Uniform Resource Identifiers&amp;#8221;.  In addition to making this URI unique, it&amp;#8217;s important to make them patterned and consistent. &lt;/p&gt;




&lt;p&gt;The Bluestream API, for example, uses the following four URIs to create a resource, retrieve a resource, find items related to that resource, and see the resource&amp;#8217;s history of edits.&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;POST /ams/upload
GET  /ams/rest/asset/A1001001A06B17B43948J49293
GET  /ams/rest/related/A1001001A06B17B43948J49293
GET  /ams/rest/history/A1001001A06B17B43948J49293
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;Two problems with the URIs above. First, the URI for creating new assets is wildly different than the other URIs. You&amp;#8217;re better off sticking with a pattern: &lt;code&gt;POST /ams/rest/asset/&lt;/code&gt;.  &lt;/p&gt;




&lt;p&gt;Second, the collection of assets related to &lt;code&gt;A1001001A06B17B43948J49293&lt;/code&gt; is conceptually a sub-asset.  Typically you&amp;#8217;ll see the collection nested within the full URI of its conceptual parent: &lt;code&gt;/ams/rest/asset/A1001001A06B17B43948J49293/related&lt;/code&gt;.  Same goes for the history of edits: &lt;code&gt;/ams/rest/asset/A1001001A06B17B43948J49293/history&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;Most data hierarchies will be fairly flat like &lt;a href=&quot;http://apiwiki.twitter.com/Twitter-API-Documentation&quot;&gt;Twitter&amp;#8217;s&lt;/a&gt; so you frequently won&amp;#8217;t need to expose urls with deeply nested data relationships. But if you do, many client libraries are designed to interact with REST APIs that follow a patterned URI scheme.&lt;/p&gt;




&lt;p&gt;The pattern of &lt;a href=&quot;http://apidoc.digg.com/ListStories&quot;&gt;Digg&amp;#8217;s data feeds&lt;/a&gt; is easy to understand. You barely need any more documentation than a sentence for the urls of each type. Sadly, you can&amp;#8217;t get data &lt;em&gt;in&lt;/em&gt; using these urls but &amp;#8220;&lt;a href=&quot;http://blog.digg.com/?p=817&quot;&gt;endpoints for participating&lt;/a&gt;&amp;#8221; are coming soon.&lt;/p&gt;




&lt;h2&gt;4) Use Your Status Codes&lt;/h2&gt;




&lt;p&gt;Every HTTP response comes back with a status code. Like HTTP request verbs, these responses statuses &lt;a href=&quot;http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html&quot;&gt;have specific meaning&lt;/a&gt;. Use them correctly!  Many API providers send back only two codes: &lt;code&gt;200&lt;/code&gt; for requests that worked and &lt;code&gt;500&lt;/code&gt; for requests that didn&amp;#8217;t work.&lt;/p&gt;




&lt;p&gt;I once snapped this shot while using the Ann Arbor District Library&amp;#8217;s current API:&lt;/p&gt;




&lt;p&gt;&lt;img src=&quot;http://img.skitch.com/20090702-np5p4utqx1w1w6199hfikegysm.jpg&quot; alt=&quot;'no no'&quot; width='647' height='547'&gt;&lt;/p&gt;




&lt;p&gt;Sending back &lt;code&gt;200 OK&lt;/code&gt; when you mean &lt;code&gt;404 Not Found&lt;/code&gt; is decidedly &lt;em&gt;not OK.&lt;/em&gt; &lt;/p&gt;




&lt;p&gt;So, when someone &lt;code&gt;PUT&lt;/code&gt;s to a URI that only accepts &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;POST&lt;/code&gt; send back a &lt;code&gt;405 Method Not Allowed&lt;/code&gt;. When someone attempts to access data without authenticating, send back &lt;code&gt;401 Unauthorized&lt;/code&gt;.  Most client libraries will convert these status into native errors/exceptions keeping the amount of body parsing required (I got back data with &lt;code&gt;200 OK&lt;/code&gt;, but was it a &amp;#8220;&lt;code&gt;200 OK&lt;/code&gt; here&amp;#8217;s what you wanted&amp;#8221; or a &amp;#8220;&lt;code&gt;200 OK&lt;/code&gt; shit is &lt;em&gt;not ok&lt;/em&gt;?&amp;#8221;)&lt;/p&gt;




&lt;h2&gt;5) Expose (And Accept) Multiple Data Formats&lt;/h2&gt;




&lt;p&gt;You should both expose data and accept incoming data in at least &lt;code&gt;XML&lt;/code&gt; and  &lt;code&gt;json/jsonp&lt;/code&gt;.  These two data formats represent the standard data serializations of the web. The format of these should allow for nested, related data. There is some variance in how this data should be formatted, but there is an emerging pattern&lt;/p&gt;




&lt;p&gt;For XML:&lt;/p&gt;




&lt;h3&gt;Enclose a data type in an element of it&amp;#8217;s name, not as bare data&lt;/h3&gt;




&lt;p&gt;Do this&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;&amp;lt;?xml version=&quot;1.0&quot;?&amp;gt;
&amp;lt;person&amp;gt;
  &amp;lt;name&amp;gt;...&amp;lt;/name&amp;gt;
  &amp;lt;age&amp;gt;...&amp;lt;/age&amp;gt;
&amp;lt;/person&amp;gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;not this&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;&amp;lt;?xml version=&quot;1.0&quot;?&amp;gt;
&amp;lt;name&amp;gt;...&amp;lt;/name&amp;gt;
&amp;lt;age&amp;gt;...&amp;lt;/age&amp;gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;h3&gt;Enclose a collection in a tag of its type, pluralized. Each item should be in an element of its type&lt;/h3&gt;




&lt;p&gt;Do this&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;&amp;lt;?xml version=&quot;1.0&quot;?&amp;gt;
&amp;lt;dogs&amp;gt;
  &amp;lt;dog&amp;gt;
    &amp;lt;name&amp;gt;...&amp;lt;/name&amp;gt;
  &amp;lt;/dog&amp;gt;
  &amp;lt;dog&amp;gt;
    &amp;lt;name&amp;gt;...&amp;lt;/name&amp;gt;
  &amp;lt;/dog&amp;gt;
&amp;lt;/dogs&amp;gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;not this&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;&amp;lt;?xml version=&quot;1.0&quot;?&amp;gt;
&amp;lt;dog&amp;gt;
  &amp;lt;name&amp;gt;...&amp;lt;/name&amp;gt;
&amp;lt;/dog&amp;gt;
&amp;lt;dog&amp;gt;
  &amp;lt;name&amp;gt;...&amp;lt;/name&amp;gt;
&amp;lt;/dog&amp;gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;Although most client libraries will happily parse either, the first allows you to place additional useful data about the collection as an attribute of the collection itself:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;&amp;lt;?xml version=&quot;1.0&quot;?&amp;gt;
&amp;lt;count&amp;gt;2&amp;lt;/count&amp;gt;
&amp;lt;last-added&amp;gt;...&amp;lt;/last-added&amp;gt;
&amp;lt;dogs&amp;gt;
  ...
&amp;lt;/dogs&amp;gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;h3&gt;Use elements for data, not attributes.&lt;/h3&gt;




&lt;p&gt;Client library support for XML attributes is inconsistent and what distinguishes data in elements from data in attributes isn&amp;#8217;t always clear.&lt;/p&gt;




&lt;p&gt;Do this:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;&amp;lt;asset&amp;gt;
  &amp;lt;id&amp;gt;A1001001A06C13B45909C08771&amp;lt;/id&amp;gt;
  &amp;lt;name&amp;gt;AMS_TEST_ITEM&amp;lt;/name&amp;gt;
  &amp;lt;display-name&amp;gt;AMS_TEST_ITEM&amp;lt;/display-name&amp;gt;
  &amp;lt;size&amp;gt;80638&amp;lt;/size&amp;gt;
  &amp;lt;thumbnail&amp;gt;
    &amp;lt;is-defult&amp;gt;true&amp;lt;/is-default&amp;gt;
    &amp;lt;url&amp;gt;http://www.example.com/ams/icons/gif.gif&amp;lt;/url&amp;gt;
  &amp;lt;/thumbnail&amp;gt;
&amp;lt;/asset&amp;gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;not this:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;&amp;lt;asset ID=&quot;A1001001A06C13B45909C08771&quot;&amp;gt;
  &amp;lt;entity name=&quot;AMS_TEST_ITEM&quot; display-name=&quot;AMS_TEST_ITEM&quot; /&amp;gt;
  &amp;lt;metadata name=&quot;AMS_SZ&quot; display-name=&quot;Size&quot; namespace=&quot;Info&quot;&amp;gt;80638&amp;lt;/metadata&amp;gt;
  &amp;lt;thumbnail isDefault=&quot;true&quot;&amp;gt;http://www.example.com/ams/icons/gif.gif&amp;lt;/thumbnail&amp;gt;
&amp;lt;/asset&amp;gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;One valid use of attributes is to provide metadata. A common example is including suggested type casting, since XML (unlike json) only sends string data:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;&amp;lt;asset&amp;gt;
  &amp;lt;id type='integer'&amp;gt;A1001001A06C13B45909C08771&amp;lt;/id&amp;gt;
  &amp;lt;name&amp;gt;AMS_TEST_ITEM&amp;lt;/name&amp;gt;
  &amp;lt;display-name&amp;gt;AMS_TEST_ITEM&amp;lt;/display-name&amp;gt;
  &amp;lt;size type='integer'&amp;gt;80638&amp;lt;/size&amp;gt;
  &amp;lt;thumbnail&amp;gt;
    &amp;lt;is-defult type='boolean'&amp;gt;true&amp;lt;/is-default&amp;gt;
    &amp;lt;url&amp;gt;http://www.example.com/ams/icons/gif.gif&amp;lt;/url&amp;gt;
  &amp;lt;/thumbnail&amp;gt;
&amp;lt;/asset&amp;gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;Support for this in client libraries is spotty, however.&lt;/p&gt;




&lt;p&gt;For &lt;code&gt;json&lt;/code&gt; the structure is more formalized since json parses into native javascript date types.  There are no attributes, only slots; strings, integers, booleans, nested object literals, nested arrays, and other formats can be sent as data in these slots.  There&amp;#8217;s really only one decision needed for json:&lt;/p&gt;




&lt;h3&gt;Have the outer object contained in a slot named for its type, or don&amp;#8217;t&lt;/h3&gt;




&lt;p&gt;This one varies from API to API, both formats work well:&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;{
  'name' : '...',
  'age'  : 22,
  'dogs' : [
    {'name' : 'fido', 'breed' : 'mutt', 'is_spayed' : true},
    {'name' : 'killer', 'breed' : 'poodle', 'is_spayed' : false}
  ]
}
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;or&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;{
  'person' : {
    'name': '...',
    'age' : 22,
    'dogs' : [...]
  }
}
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;The latter will allow you to send back additional data about the response&lt;/p&gt;




&lt;pre&gt;&lt;code&gt;{
  'access' : 'read-only',
  'person' : {
    'name': '...',
    'age' : 22
  }
}
&lt;/code&gt;&lt;/pre&gt;




&lt;p&gt;But either format is acceptable. &lt;/p&gt;




&lt;h3&gt;For both XML and JSON, accept images as either multipart/post or a url&lt;/h3&gt;




&lt;p&gt;Images are tricky to manage via an API over the web (check out all the trouble that &lt;a href=&quot;http://code.google.com/p/twitter-api/issues/list?can=1&amp;amp;q=image&amp;amp;colspec=ID+Stars+Type+Status+Priority+Owner+Summary+Opened+Modified+Component&amp;amp;cells=tiles&quot;&gt;Twitter has had&lt;/a&gt;).  Sending a multipart/post with the image data as content is probably the correct answer, but some http libraries (notably Ruby&amp;#8217;s) have buggy or incomplete multipart/post implementations. Do your developers a favor and accept urls referencing images in addition to data &lt;code&gt;POST&lt;/code&gt;ed as multipart body content. &lt;/p&gt;




&lt;h2&gt;6) Protect Your Users with OAuth&lt;/h2&gt;




&lt;p&gt;One thorny issue of user-specific API data is how you allow third party access.  Twitter, until recently, required users to give their twitter username and password to third parties.  You don&amp;#8217;t ever want your users handing out their passwords! Instead, like Twitter, MySpace, Google Data, Get Satisfaction, Tripit, and many others, control access to your users&amp;#8217; data with &lt;a href=&quot;http://oauth.net/&quot;&gt;OAuth&lt;/a&gt;.  OAuth (not be mistaken with the very different &lt;a href=&quot;http://openid.net/&quot;&gt;OpenID&lt;/a&gt;) lets your users give access to a third party without giving away their credentials.&lt;/p&gt;




&lt;p&gt;Libraries for adding OAuth to your API and for third-parties to consume data are available in all major languages.&lt;/p&gt;




&lt;h2&gt;7) Don&amp;#8217;t Shut Off HTTP Authentication Entirely&lt;/h2&gt;




&lt;p&gt;OAuth isn&amp;#8217;t always the answer.  For cases where the end user isn&amp;#8217;t involving a third party directly (e.g. a desktop or iPhone application that only communicates with &lt;em&gt;your&lt;/em&gt; servers), allowing the user to simply enter their user name and password provides a more desirable user experience.  At the very least, allow HTTP Authentication to obtain a token for these kinds of applications and then use that token for all other requests.&lt;/p&gt;




&lt;h2&gt;8) Document, Document, Document&lt;/h2&gt;




&lt;p&gt;Finally, fully document your API.  You&amp;#8217;ll want to demonstrate the following: valid URIs, required and optional data, HTTP status codes and what causes them, sample requests, and sample responses.&lt;/p&gt;




&lt;p&gt;Having your documentation on the web in a structured format is best. &lt;a href=&quot;https://plans.pbworks.com/index.php&quot;&gt;PBWorks&lt;/a&gt; (formerly PBWiki) is very popular for API documenting: &lt;a href=&quot;http://apiwiki.twitter.com/&quot;&gt;Twitter&lt;/a&gt;, &lt;a href=&quot;http://apidoc.digg.com/&quot;&gt;Digg&lt;/a&gt;, and a few others are using it.&lt;/p&gt;

</content>
   <author>
     <name>Trek Glowacki</name>
     <uri>http://wonderfullyflawed.com/about.html</uri>
   </author>
 </entry>
 
 <entry>
   <title>Depressed about Michigan</title>
   <link href="http://wonderfullyflawed.com/2009/06/10/depressed-about-mi.html"/>
   <updated>2009-06-10T00:00:00-07:00</updated>
   <id>http://wonderfullyflawed.com/2009/06/10/depressed-about-mi</id>
   <content type="html">&lt;p&gt;I just got word of an &lt;a href=&quot;http://srtstimulus.eventbrite.com/&quot;&gt;upcoming career development event&lt;/a&gt; taking place in south-east Michigan next week and it's made me mildly depressed about the state of the software development industry in my area.&lt;/p&gt;

&lt;p&gt;On one hand, I'm glad to see &lt;a href='http://srtsolutions.com/'&gt;someone&lt;/a&gt; offering low-cost continuing education events. On the other hand it's 2009: there should not be a developer &lt;em&gt;left&lt;/em&gt; in this industry who hasn't already investigated and/or implemented most of these techniques already on her (or his) own time or as part of a workplace requirement to remain updated on current industry trends.&lt;/p&gt;

&lt;p&gt;You &lt;em&gt;can't&lt;/em&gt; talk about &quot;version control, continuous integration, and automated testing&quot; as &quot;new techniques&quot; just like you can't talk about &quot;C#, Java, Python, Scala, GWT, Silverlight,&quot; as &quot;new languages&quot;.&lt;/p&gt;

&lt;p&gt;Sad Panda.&lt;/p&gt;
</content>
   <author>
     <name>Trek Glowacki</name>
     <uri>http://wonderfullyflawed.com/about.html</uri>
   </author>
 </entry>
 
</feed>
