<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>http://blog.roberthahn.ca/</id>
  <title>Robert Hahn: A darn good web developer</title>
  <updated>2009-04-08T02:15:59Z</updated>
  <link href="http://blog.roberthahn.ca" rel="alternate"/>
  <link href="http://blog.roberthahn.ca/xml/atom/feed.xml" rel="self"/>
  <author>
    <name>Robert Hahn</name>
    <uri>http://www.roberthahn.ca/</uri>
  </author>
  <entry>
    <id>tag:blog.roberthahn.ca,2009-04-08:/articles/2009/04/08/spring_cleaning/</id>
    <title type="html">Spring Cleaning</title>
    <published>2009-04-08T02:15:59Z</published>
    <updated>2009-04-08T02:17:40Z</updated>
    <link href="http://blog.roberthahn.ca/articles/2009/04/08/spring_cleaning/" rel="alternate"/>
    <content type="html">&lt;h1&gt;Spring Cleaning (yes, I&amp;#8217;m back)&lt;/h1&gt;

&lt;p&gt;Um, wow. It&amp;#8217;s been awhile since I last posted anything!&lt;/p&gt;

&lt;p&gt;During my quiet time, I&amp;#8217;ve been busy; I&amp;#8217;ve had a few web contracts that I worked on at night. I went to work at a cool new company called &lt;a href="http://primalfusion.com/"&gt;Primal Fusion&lt;/a&gt; as a software developer. You&amp;#8217;ll want to check that out!&lt;/p&gt;

&lt;p&gt;Things have finally settled down enough that I wanted to do some serious housecleaning on my site.  I was getting irritated with the blogging engine I was using (Typo), irritated about the HTML I used for the beachboy theme (it never looked fine on all browsers) and I was yearning to return to the world of static website generators.&lt;/p&gt;

&lt;p&gt;So, this site has been redesigned and re-staged. I&amp;#8217;ve tried as much as possible to keep the links the same, but if you&amp;#8217;ve got a 404 on a page that used to be there, please let me know about it.&lt;/p&gt;

&lt;p&gt;The new publishing engine is a ruby-based static site generator called &lt;a href="http://nanoc.stoneship.org/"&gt;nanoc&lt;/a&gt;, which so far I&amp;#8217;ve been very pleased with.  The system provides me with a solid set of features for building a static site, and was easy to extend with features I&amp;#8217;ve wanted to build in.&lt;/p&gt;

&lt;p&gt;Take the homepage for example. You&amp;#8217;ll see a mashup of my &lt;a href="http://delicious.com/roberthahn"&gt;Delicious&lt;/a&gt; feed, &lt;a href="http://twitter.com/hahnrobert/"&gt;Twitter&lt;/a&gt; feed, and my article feed sorted in order. It was only a few lines of code to build, including caching the feeds.&lt;/p&gt;

&lt;p&gt;As per my usual, I&amp;#8217;ve returned to a low-key presentation &amp;#8211; the intial launch of this site doesn&amp;#8217;t even have any javascript!&lt;/p&gt;

&lt;p&gt;So if you&amp;#8217;ve been subscribed to me, thank you for waiting so long!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <id>tag:blog.roberthahn.ca,2007-08-16:/articles/2007/08/17/the-ideal-rest-framework/</id>
    <title type="html">The Ideal REST Framework.</title>
    <published>2007-08-17T02:11:39Z</published>
    <updated>2009-03-21T21:14:58Z</updated>
    <link href="http://blog.roberthahn.ca/articles/2007/08/17/the-ideal-rest-framework/" rel="alternate"/>
    <content type="html">&lt;h1&gt;The Ideal REST Framework.&lt;/h1&gt;


&lt;p&gt;Granted, I haven&#8217;t been spending all my time looking at everyone&#8217;s favorite framework, but from the ones I do know about, it seems really odd that we have an impedance mismatch between HTTP and the classes/methods we use in our framework.  I want to spend a few posts exploring how I think a framework should be put together, so that it conforms as much as possible to REST.  My code samples will be in Ruby, but I don&#8217;t see any reason why you can&#8217;t build this in any other language.&lt;/p&gt;


&lt;p&gt;Naturally, I start where the HTTP request gets handed off to what we think of as the controller in most frameworks.  It seems as though we ought to have a superclass that defines the uniform interface. It would look like this:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;class HTTPResource
    def get; end
    def post; end
    def put; end
    def delete; end
    def method_missing m
        @status = 405
        "Method Not Allowed"
    end
end
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;With it, you would instantiate the resources you want to expose.  In this case, I want to build, um, how about a Media Release administration tool, where you can add, edit, and post PRs.  Let&#8217;s expose a resource called &lt;code&gt;Release&lt;/code&gt;. &lt;/p&gt;


&lt;pre&gt;&lt;code&gt;class Release &amp;lt; HTTPResource
  def get; end
  def put; end
  def delete; end

  private

  def post;end
end
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Well, I&#8217;m sure you&#8217;re tempted to say that you can &lt;code&gt;post&lt;/code&gt; a &lt;code&gt;Release&lt;/code&gt;, and I&#8217;d agree with you, but I wanted to set it up this way to show you something cool:  by making &lt;code&gt;post&lt;/code&gt; private, any calls to that method would instead be handled by the superclass&#8217; &lt;code&gt;method_missing&lt;/code&gt; method, and return a 405 Method Not Allowed.  Wow, that&#8217;s convenient.&lt;/p&gt;


&lt;p&gt;There&#8217;s a lot of stuff missing. &lt;code&gt;HTTPResource&lt;/code&gt; really ought to get some context together, like a collection of HTTP headers and ready access to query variables. I haven&#8217;t yet thought about how that&#8217;s going to work yet &#8211; it could be that we need instance variables, or it could be that we need classes.&lt;/p&gt;


&lt;p&gt;But one thing I thought was pretty cool about this is that this structure seems so simple and natural, you could easily write a commandline script that included these class definitions, and manipulate your app as easily from a script as from a browser. I&#8217;m sure you could do it with other frameworks too, but it &lt;em&gt;looks&lt;/em&gt; obvious how you&#8217;d write such a tool.  That seems like a win when it comes times to write tests &#8211; just call &lt;code&gt;Release.new.get&lt;/code&gt;, for example, and test what gets returned.&lt;/p&gt;

</content>
  </entry>
  <entry>
    <id>tag:blog.roberthahn.ca,2007-07-22:/articles/2007/07/23/three-methods-for-simulating-render_component-in-camping/</id>
    <title type="html">Three Methods for simulating render_component in Camping</title>
    <published>2007-07-23T02:27:00Z</published>
    <updated>2009-03-21T21:14:58Z</updated>
    <link href="http://blog.roberthahn.ca/articles/2007/07/23/three-methods-for-simulating-render_component-in-camping/" rel="alternate"/>
    <content type="html">&lt;h1&gt;Three Methods for simulating render_component in Camping&lt;/h1&gt;


&lt;p&gt;If you have used the Camping microframework for any length of time, you might have quickly found out that there&#8217;s no feature available like &lt;code&gt;render_component&lt;/code&gt;, which is found in Ruby on Rails.  &lt;code&gt;render_component&lt;/code&gt; allows you to call a controller method (with its associated view) for the purposes of rendering a portion of your page.  For example, you might want a page that primarily displays data for one employee, and show a list of employees in a side column - ideally, you&#8217;d like the controller responsible for generating a list of employees to continue to be responsible for that, even if you&#8217;re in the controller for displaying employee data.  So I ended up working out three different methods for solving this problem.  &lt;/p&gt;


&lt;p&gt;Let&#8217;s go back to my running example here:  I&#8217;ve got this employee management app, where people might want to add or delete employees, and on almost every page, I need to see a list of employees&#8217; names, each linked to their profiles, on a side column.  The controller class for retrieving this list could look something like this:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;class Employees &amp;lt; R
  def get
    @employees = Employees.find :all, :order =&amp;gt; "name asc"
    render :employee_nav_list
  end
end
&lt;/code&gt;&lt;/pre&gt;


&lt;h2&gt;Method 1: Calling methods from other classes&lt;/h2&gt;


&lt;p&gt;My first cut at trying to build &lt;code&gt;render_component&lt;/code&gt; was the hardest thing I had to solve.  The &lt;code&gt;Employees&lt;/code&gt; class, with its &lt;code&gt;get&lt;/code&gt; method for generating the list was already written, so what I really wanted to do was somehow instantiate the  class and call &lt;code&gt;get&lt;/code&gt;.  &lt;a href="http://blog.roberthahn.ca/articles/2007/07/22/camping-without-the-view"&gt;In my previous article&lt;/a&gt;, we learned that the return values of these methods are always a string; if I could somehow instantiate  that class, and call its &lt;code&gt;get&lt;/code&gt; method, then I&#8217;d have a string containing a chunk of HTML that I can store in an instance variable to be used in my current view.&lt;/p&gt;


&lt;p&gt;Calling &lt;code&gt;Employees.new.get&lt;/code&gt;, unfortunately, wasn&#8217;t that simple.  Hints to what the Employee&#8217;s constructor looked like was kind of buried in the Camping microframework itself, and after poring over the code and testing things out, I was able to come up with the following module:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;module Component_Support
  def render_component( classname, method, payload, args )
    classname.new( payload, @env, method.to_s ).send(method.to_sym, *args)
  end
end
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;To use this, you&#8217;d have a bit of code in your app that looks like this:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;module YourApp
  include Component_Support
end
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;and then you&#8217;re free to call &lt;code&gt;render_component&lt;/code&gt; wherever you like. Let&#8217;s break it down.  &lt;code&gt;render_component&lt;/code&gt;, here, takes 4 arguments:&lt;/p&gt;


&lt;dl&gt;
&lt;dt&gt;classname&lt;/dt&gt;
&lt;dd&gt;The name of the class you want to instantiate&lt;/dd&gt;
&lt;dt&gt;method&lt;/dt&gt;
&lt;dd&gt;The method you want to call in that class&lt;/dd&gt;
&lt;dt&gt;payload&lt;/dt&gt;
&lt;dd&gt;If you&#8217;re making a `GET` request, this should be empty. If you&#8217;re making a `POST` request, the payload should consist of `form-encoded` data (which typically looks like this: `name1=value&amp;name2=value&amp;name3=value&#8230;`)&lt;/dd&gt;
&lt;dt&gt;args&lt;/dt&gt;
&lt;dd&gt;These would be the regular arguments the method requires to execute, if there are any.&lt;/dd&gt;
&lt;/dl&gt;


&lt;p&gt;The body of the method instantiates the object correctly, calls the method that we&#8217;re interested in, and returns the result.  If I was displaying an employee profile page, and I wanted to show that list of employees down the side, all I need to do is add the following line to my profile controller:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;@employee_list = render_component(Employees, 'GET', '', '')
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;And I&#8217;ll have a nice fragment of HTML to include in my output.&lt;/p&gt;


&lt;h2&gt;Method 3: Define methods for sharing data&lt;/h2&gt;


&lt;p&gt;I don&#8217;t know about you, but up until recently, I built the classes in Camping using only &lt;code&gt;get&lt;/code&gt; and &lt;code&gt;post&lt;/code&gt; methods because I thought that was the way you were supposed to do it. It was &lt;em&gt;&#8220;the rules&#8221;&lt;/em&gt;.  It never occurred to me that I could easily add other methods if I needed to.&lt;/p&gt;


&lt;p&gt;The thing is, it&#8217;s just a class, like any other, so if you really needed to, you could move code that&#8217;s common to both &lt;code&gt;get&lt;/code&gt; and &lt;code&gt;post&lt;/code&gt; methods into their own method calls and call them.&lt;/p&gt;


&lt;p&gt;using the above code snippet, we could basically build something like this:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;class Profile &amp;lt; R '/profile'
  def get
    # code specific to get

    @employee_list = build_emp_list

    render :profile
  end

  def post
    # code specific to post

    @employee_list = build_emp_list

    render :profile
  end

  private

  def build_emp_list
    @employees = Employees.find :all, :order =&amp;gt; "name asc"
    render :employee_nav_list
  end
end
&lt;/code&gt;&lt;/pre&gt;


&lt;h2&gt;Method 3: Putting components in modules&lt;/h2&gt;


&lt;p&gt;As a variation on a theme, suppose you have a number of controller classes, and most of them needs to display the same type of data on a page.  You&#8217;d like to be as DRY as possible, and you don&#8217;t want to use the &lt;code&gt;render_component&lt;/code&gt; code up above &#8211; maybe because the component you want to render doesn&#8217;t properly belong to one of the controller classes. All you&#8217;d need to do is make two changes to code that looks like method 2:&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;move &lt;code&gt;#build_emp_list&lt;/code&gt; into a module, along with other methods you want to re-use.&lt;/li&gt;
&lt;li&gt;add &lt;code&gt;include Your_new_modulename&lt;/code&gt; to the class.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Here&#8217;s an example:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;module MyApp::Controllers
  module Employee_Components

    # some components defined here...

    def build_emp_list
      @employees = Employees.find :all, :order =&amp;gt; "name asc"
      render :employee_nav_list
    end
  end

  class Profile &amp;lt; R '/profile'
    include Employee_Components

    def get
      # code specific to get

      @employee_list = build_emp_list

      render :profile
    end

    def post
      # code specific to post

      @employee_list = build_emp_list

      render :profile
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;


&lt;h2&gt;Conclusion&lt;/h2&gt;


&lt;p&gt;So, with this article, you&#8217;re well equipped to add &lt;code&gt;render_component&lt;/code&gt; functionality to your Camping apps, which will help in making your code more DRY.  Which method you use should depend on how you can best make your code readable.  If the code you want to reuse resides in a related class, then I&#8217;ve provided you with a means to instantiate that class and call the method.  If the only time you require this method is in one class, define it as its own function.  Otherwise, some clever &lt;code&gt;module&lt;/code&gt; management might do the trick.&lt;/p&gt;

</content>
  </entry>
  <entry>
    <id>tag:blog.roberthahn.ca,2007-07-21:/articles/2007/07/22/camping-without-the-view/</id>
    <title type="html">Camping Without the View</title>
    <published>2007-07-22T02:27:16Z</published>
    <updated>2009-03-21T21:14:58Z</updated>
    <link href="http://blog.roberthahn.ca/articles/2007/07/22/camping-without-the-view/" rel="alternate"/>
    <content type="html">&lt;h1&gt;Camping Without the View&lt;/h1&gt;


&lt;p&gt;It&#8217;s possible to construct a web application using the Camping microframework without using the &lt;code&gt;*::Views&lt;/code&gt; module.  Most of the time, you would be crazy to do so, since the &lt;code&gt;*::Views&lt;/code&gt; module makes it very easy to print out HTML responses using _why&#8217;s Markaby markup engine.&lt;/p&gt;


&lt;p&gt;There is a strong use case for not using &lt;code&gt;*::Views&lt;/code&gt;&#8211; if you want to return data that is not supposed to be formatted as HTML (like XML or JSON), you&#8217;ll probably find that it&#8217;s more work than it really ought to be, to print out that response.&lt;/p&gt;


&lt;h2&gt;The Model-less, &lt;em&gt;View-less&lt;/em&gt; Camping App.&lt;/h2&gt;


&lt;p&gt;That&#8217;s right. I&#8217;m going to show you a camping app that does not use either the &lt;code&gt;*::Views&lt;/code&gt; or the &lt;code&gt;*::Models&lt;/code&gt; modules, that is perfectly legit:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;#!/usr/bin/env ruby

require 'rubygems'
require 'camping'

Camping.goes :HelloWorld

module HelloWorld::Controllers
  class Index &amp;lt; R '/'
    def get
      %Q(
      &amp;lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt;
      &amp;lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&amp;gt;
        &amp;lt;head&amp;gt;&amp;lt;title&amp;gt;My small page&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;
      &amp;lt;body&amp;gt;
        &amp;lt;h1&amp;gt;Hello, world!&amp;lt;/h1&amp;gt;
      &amp;lt;/body&amp;gt;
      &amp;lt;/html&amp;gt;
      )
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;If you save this program as &lt;code&gt;hello_world.rb&lt;/code&gt;, then run &lt;code&gt;camping hello_world.rb&lt;/code&gt;, it will run fine, and if you point your browser to &lt;code&gt;http://localhost:3301/&lt;/code&gt;, you will see the friendly greeting.&lt;/p&gt;


&lt;p&gt;What&#8217;s going on? Let&#8217;s rewrite the application to something more familiar:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;#!/usr/bin/env ruby

require 'rubygems'
require 'camping'

Camping.goes :HelloWorld

module HelloWorld::Controllers
  class Index &amp;lt; R '/'
    def get
      render :hello_world
    end
  end
end

module HelloWorld::Views
  def hello_world
    html do
      head do
        title "My small page"
      end
      body do
        h1 "Hello, world!"
      end
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;If you run this &lt;code&gt;hello_world.rb&lt;/code&gt; app, you&#8217;ll get almost the same output. There is almost no magic involved here; in the &lt;code&gt;get&lt;/code&gt; function, we have a &lt;code&gt;render&lt;/code&gt; statement that performs the necessary work to call the &lt;code&gt;hello_world&lt;/code&gt; function in the &lt;code&gt;HelloWorld::Views&lt;/code&gt; module.  What&#8217;s not obvious here (except to seasoned Rubyists, I suppose) is that there is an implicit return in the &lt;code&gt;hello_world&lt;/code&gt; function; it returns a string containing the HTML to be printed. There is &lt;em&gt;also&lt;/em&gt; an implicit return happening in the controller, where the results of the &lt;code&gt;render&lt;/code&gt; statement (the Hello, world page) is returned to &lt;em&gt;its&lt;/em&gt; caller.&lt;/p&gt;


&lt;p&gt;So the dirty secret is out: as long as the methods in your controller classes return a string, whatever is in that string is what&#8217;s going to be returned to the browser.&lt;/p&gt;


&lt;h2&gt;Why do we want to do this, again?&lt;/h2&gt;


&lt;p&gt;Most of the time, you &lt;em&gt;want&lt;/em&gt; to use the &lt;code&gt;*::Views&lt;/code&gt; module, because, most of the time, you want to return HTML to the browser.&lt;/p&gt;


&lt;p&gt;Knowing what we know now, we have the ability to do a couple of useful and interesting things:&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;build web apps that emit alternative formats &lt;/li&gt;
&lt;li&gt;build a &lt;code&gt;render_component&lt;/code&gt; feature into your Camping app&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;I&#8217;m going to get into more of this in future articles.&lt;/p&gt;

</content>
  </entry>
  <entry>
    <id>tag:blog.roberthahn.ca,2007-06-21:/articles/2007/06/22/setting-http-response-codes-with-exceptions/</id>
    <title type="html">Setting HTTP Response Codes with Exceptions</title>
    <published>2007-06-22T00:24:00Z</published>
    <updated>2009-03-21T21:14:58Z</updated>
    <link href="http://blog.roberthahn.ca/articles/2007/06/22/setting-http-response-codes-with-exceptions/" rel="alternate"/>
    <content type="html">&lt;h1&gt;Setting HTTP Response Codes with Exceptions&lt;/h1&gt;


&lt;p&gt;A client of mine needed an admin tool for their site: a basic file upload script.  Anyone who has ever written such a script knows that there are at least a good half dozen potential failure points when writing such a thing.  Experience suggests that you should check at a minimum the following types of things:&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;The file type of the file&lt;/li&gt;
&lt;li&gt;The size of the file&lt;/li&gt;
&lt;li&gt;The save path were you want to put the file (does it exist? is it a directory? Can I write to that directory?&lt;/li&gt;
&lt;li&gt;Does the file you want to upload already exist in that location? Can I overwrite it? &lt;/li&gt;
&lt;li&gt;etc.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;For some of these tests, it may actually be pretty important to fail loudly enough for the user to notice.  And of course, if we want to build RESTful web applications, it would be good to return the correct HTTP response code in a failure scenario.&lt;/p&gt;


&lt;p&gt;While writing the script, using Camping, it suddenly occurred to me that with a bit of tooling, I could easily use Ruby&#8217;s exception handling tools to make my life easier, and to make the code maintainable.  This technique can easily be applied to Rails applications too.&lt;/p&gt;


&lt;h2&gt;The setup&lt;/h2&gt;


&lt;p&gt;&lt;a href="http://www.pragmaticprogrammer.com/titles/ruby/"&gt;The Pickaxe&lt;/a&gt;, in its description of the &lt;code&gt;Exception&lt;/code&gt; class, describes a &lt;code&gt;#status&lt;/code&gt; method that is only available to the &lt;code&gt;SystemExit&lt;/code&gt; exception.  That looks like a useful idea, but I need the status baked into the base Exception class.  Why?  Well, if the script is going to die because a directory isn&#8217;t writable, or for some other reason I couldn&#8217;t anticipate, I need the script to return a 500 error code.  Time to monkey patch &lt;code&gt;Exception&lt;/code&gt;:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;class Exception
    def status
        500
    end
end
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Now let&#8217;s extend this for some other error scenarios, like a user attempting to upload a file larger than I&#8217;d like:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;class ForbiddenException &amp;lt; RuntimeError
    def status
        403
    end
end
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;You should compile a big list of these exceptions into its own file and &lt;code&gt;require&lt;/code&gt; it in your app.&lt;/p&gt;


&lt;h2&gt;In Your Application&lt;/h2&gt;


&lt;p&gt;Ok, so now you&#8217;re in Camping (or Rails), and you&#8217;ve got your file processing logic sitting in a class in the model, you have an &#8216;error&#8217; view (among others) and you&#8217;re calling this code from the controller.  In the model, you might have a &lt;code&gt;#save&lt;/code&gt; method, and one of the things you&#8217;re going to do before you save your uploaded file is check the file size:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;raise( ForbiddenException.new, "File Too Large!") if @file.size &amp;gt; 11000000
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Now for the juicy bit: the controller logic:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;class Index
    def Post
        file = FileUpload.new(...)

        page = :index

        begin
            @file = file.save
       rescue Exception =&amp;gt; @e # oh, noes! my bad!
            @status = @e.status 
            page = :error
        end

        render page
    end
end
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;In Camping, the &lt;code&gt;@status&lt;/code&gt; variable is reserved for setting the HTTP response code, and is smart enough to change it from 200 to 302 when you&#8217;re redirecting; that said, you can set it to other codes as we&#8217;re doing here.&lt;/p&gt;


&lt;p&gt;And the view (the &lt;code&gt;@e&lt;/code&gt; is being used here):&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;def error
    h1 "An Error has occurred"
    p @e
end
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;After getting this working, there&#8217;s two things I couldn&#8217;t believe: &lt;/p&gt;


&lt;ol&gt;
&lt;li&gt;How easy it was to get this going&lt;/li&gt;
&lt;li&gt;That no one else thought to mention this&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;One of the niftiest (yes, I like to use the word nifty) things about this method is that you could catch different kinds of exceptions and customize the output accordingly:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;def Post
    file = FileUpload.new(...)

    page = :index

    begin
        @file = file.save
    rescue ForbiddenException =&amp;gt; @e # silly user!
        @status = @e.status 
        page = :forbidden_page
    rescue Exception =&amp;gt; @e # oh, noes! my bad!
        @status = @e.status 
        page = :generic_error
    end

    render page
end
&lt;/code&gt;&lt;/pre&gt;


&lt;h2&gt;Conclusion&lt;/h2&gt;


&lt;p&gt;With only a handful of lines, I managed to create a system that easily handled error scenarios in a maintainable fashion, and passed them off to the correct view as required.  Better still, I managed to easily ensure that the client was receiving the correct HTTP response code.  Are there ways this could be improved still more?  Let me know.&lt;/p&gt;

</content>
  </entry>
  <entry>
    <id>tag:blog.roberthahn.ca,2007-05-02:/articles/2007/05/02/annotated-pdfs-preview-app-and-adobe-acrobat/</id>
    <title type="html">Annotated PDFs, Preview.app and Adobe Acrobat</title>
    <published>2007-05-02T04:13:34Z</published>
    <updated>2009-03-21T21:14:58Z</updated>
    <link href="http://blog.roberthahn.ca/articles/2007/05/02/annotated-pdfs-preview-app-and-adobe-acrobat/" rel="alternate"/>
    <content type="html">&lt;h1&gt;Annotated PDFs, Preview.app and Adobe Acrobat&lt;/h1&gt;


&lt;p&gt;Late tonight i annotated a PDF in Preview.app, and wanted to send it to a Windows-using colleague who uses Adobe Acrobat.  Then I realized that hey: maybe Preview is the only app that can read annotations made in Preview. Would Acrobat be able to read it?&lt;/p&gt;


&lt;p&gt;Google wouldn&#8217;t tell me.  So I went and downloaded the reader to see for myself.&lt;/p&gt;


&lt;p&gt;Rest assured: it does.  Annotations made in Preview can be read in Acrobat.&lt;/p&gt;

</content>
  </entry>
  <entry>
    <id>tag:blog.roberthahn.ca,2007-04-11:/articles/2007/04/12/a-question-about-using-403-forbidden/</id>
    <title type="html">A Question About Using 403 Forbidden</title>
    <published>2007-04-12T01:51:33Z</published>
    <updated>2009-03-21T21:14:58Z</updated>
    <link href="http://blog.roberthahn.ca/articles/2007/04/12/a-question-about-using-403-forbidden/" rel="alternate"/>
    <content type="html">&lt;h1&gt;A Question About Using 403 Forbidden&lt;/h1&gt;


&lt;p&gt;From &lt;a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html"&gt;here&lt;/a&gt;:&lt;/p&gt;


&lt;blockquote&gt;
    &lt;p&gt;&lt;strong&gt;403 Forbidden&lt;/strong&gt;&lt;/p&gt;
    
    &lt;p&gt;The server understood the request, but is refusing to fulfill it. Authorization will not help and the request SHOULD NOT be repeated. If the request method was not HEAD and the server wishes to make public why the request has not been fulfilled, it SHOULD describe the reason for the refusal in the entity. If the server does not wish to make this information available to the client, the status code 404 (Not Found) can be used instead.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;p&gt;Consider the following scenario:  You have a web-based application that uses HTTP Authentication, and there are URIs that authorized people may edit.  Here are the sample URIs:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;/shipment/9879
/shipment/form/9879
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;You may recognize the design of these URIs from my previous post on &lt;a href="http://blog.roberthahn.ca/articles/2007/04/06/url-design"&gt;URI Design&lt;/a&gt;.  The difference between the URIs is that the second one pulls up a form prepopulated with shipment 9879, whereas the first is merely a read-only representation.  Assume that only HTTP Authenticated users may edit the shipment.&lt;/p&gt;


&lt;p&gt;Consider the scenario where an authenticated user wishes to share the data on this page with a group of people, some of whom have authorization to edit the resource. An unauthorized person clicks on &lt;code&gt;/shipment/form/9879&lt;/code&gt; and should (correctly?) get a 403 code.&lt;/p&gt;


&lt;p&gt;Now, the developer would very much like to make life easy for people using the site.  There are a number of possible ways to resolve this:&lt;/p&gt;


&lt;ol&gt;
&lt;li&gt;Unauthorized users will get a 403 code, and whatever &lt;code&gt;/shipment/9879&lt;/code&gt; would have returned, plus an error message explaining the problem.&lt;/li&gt;
&lt;li&gt;Unauthorized users will get a 302, redirecting to &lt;code&gt;/shipment/9879&lt;/code&gt;; no mention of the error is logged.&lt;/li&gt;
&lt;li&gt;Unauthorized users will get a 200, but the returned representation is identical to &lt;code&gt;/shipment/9879&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Unauthorized users will get a 403 code, a helpful error message, and a link to &lt;code&gt;/shipment/9879&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;???&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;My gut feeling is to go with 1 or 2, saving the users a click.  What do you think?&lt;/p&gt;

</content>
  </entry>
  <entry>
    <id>tag:blog.roberthahn.ca,2007-04-06:/articles/2007/04/06/url-design/</id>
    <title type="html">The 3 Rules of URI Design</title>
    <published>2007-04-06T04:20:00Z</published>
    <updated>2009-03-21T21:14:58Z</updated>
    <link href="http://blog.roberthahn.ca/articles/2007/04/06/url-design/" rel="alternate"/>
    <content type="html">&lt;h1&gt;The 3 Rules of URI Design&lt;/h1&gt;


&lt;p&gt;There seems to be a problem that hasn&#8217;t been properly solved in aesthetic URI design.  The problem is: How do I construct a RESTful set of URIs such that one GET request results in a read-only view of the data, and another GET request results in an editable view of the data?  A key constraint on the solution: it must not rely on CSS or JavaScript, because we want the basic functionality to be available to all browsers.&lt;/p&gt;


&lt;p&gt;Dave Thomas&#8217; &lt;a href="http://pragdave.pragprog.com/pragdave/2007/03/the_radar_archi.html"&gt;recent article on the RADAR architecture &lt;/a&gt; (well worth the read) is the impetus behind this article, and has crystallized some thoughts that I have about the matter.  At the beginning of his article, he points to a problem:  the Rails developers (or at least one of them, anyway) figured URIs of the type &lt;code&gt;http://example.com/articles/1;edit&lt;/code&gt; was a perfectly reasonable way to request an editable representation of an article.  Like Dave, I think it looks tacked on.&lt;/p&gt;


&lt;p&gt;Joe Gregorio&#8217;s excellent series of articles discussing REST (&lt;a href="http://bitworking.org/news/141/REST-Tips-Prefer-following-links-over-URI-construction"&gt;here&#8217;s one&lt;/a&gt;) also has examples of URI fragments that I don&#8217;t much like, specifically &lt;code&gt;/employees/1&lt;/code&gt;.  Obviously, the pattern of plural noun followed by identifier is used by many others too.&lt;/p&gt;


&lt;p&gt;At the core, this is simply a naming problem.  &lt;a href="http://bitworking.org/news/132/REST-Tips-URI-space-is-infinite"&gt;RESTful URIs are nouns&lt;/a&gt;, because they name things.  In the Rails URI example, we&#8217;ve got a verb tacked on.  In Joe&#8217;s employee example, we have the notion of one thing being mashed with many things.&lt;/p&gt;


&lt;p&gt;With that in mind, I wanted to see if there&#8217;s some kind of conceptual framework that can be employed for URI design.  If I need to create a new, RESTful URI, what rules could I follow to ensure a consistent, predictable, simple design?  Note that for the moment, I&#8217;m focusing entirely on representing a set of data within one mime type, in particular, HTML.  Further, I&#8217;m focusing only on GETting a representation.  If there&#8217;s call for it, I could try to see how this works with the  other HTTP verbs.&lt;/p&gt;


&lt;p&gt;Let&#8217;s start with some generalizations.  For most things, (hence the generalization) we seem to look at pages that somehow fit the following criteria:&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;a list of items (products on an e-commerce site; articles in a blog)&lt;/li&gt;
&lt;li&gt;a single item (a product, or a single article)&lt;/li&gt;
&lt;li&gt;a form that creates or edits an item (add/edit article)&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;These statements can be organized into a quadrant graph:&lt;/p&gt;


&lt;p&gt;&lt;img src="/files/types_of_resources.png" alt="Quadrant graph showing types of representations of data; consider the following combinations: read-only list; read-only item; editable list; editable item" title="Quadrant graph showing types of representations of data"/&gt;&lt;/p&gt;


&lt;p&gt;The point of the graph is to illustrate what possibilities we could encounter when we&#8217;re surfing.  If you look at any given page, they tend to fall into one of the 4 boxes. You can have a read-only representation of a list, or of a single item.  You can also have an editable representation of a list or a single item.  The surprise (to me) is the notion of an editable list of items, but there&#8217;s no technical reason why it can&#8217;t be done; most of the time applications are designed more to edit a single item at a time.&lt;/p&gt;


&lt;p&gt;That said, I can&#8217;t think of a single resource that wouldn&#8217;t fit in any of the categories.  Sure, sometimes you might only want to edit a particular attribute of an item, but the type of representation you&#8217;re dealing with is still an editable, single item.&lt;/p&gt;


&lt;h2&gt;Designing URIs for List-like representations&lt;/h2&gt;


&lt;p&gt;Let&#8217;s tackle URIs that would fetch a list of items.  We would typically see URI fragments that look something like this:&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;&lt;code&gt;/employees/&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/articles/&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;That seems pretty straightforward; one would reasonably expect to see a list of employees or articles, and in RESTful apps, this is often the case. But what gets me are URI fragments that look like this:&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;&lt;code&gt;/employees/38&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/articles/5&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;The current convention is that those numbers somehow map to a particular article or a particular employee. While we&#8217;re used to the convention, it actually doesn&#8217;t make a whole lot of sense.  I think that if you&#8217;re going to refer to &lt;code&gt;/employees/&lt;/code&gt;, then anything past the trailing slash should also identify lists.  &lt;code&gt;/employees/marketing&lt;/code&gt; could refer to a list of employees in marketing.  For references to a single employee, I would rather see this:&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;&lt;code&gt;/employee/38&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/article/5&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;There&#8217;s no question that we&#8217;re referring to an employee with an id of 38, or a single article with an id of 5.&lt;/p&gt;


&lt;p&gt;&lt;strong&gt;Rule #1: Use plural nouns to represent lists of things; use singular nouns to represent  1 particular thing.&lt;/strong&gt;&lt;/p&gt;


&lt;h2&gt;Designing URIs for Different Views of the Same Data&lt;/h2&gt;


&lt;p&gt;Let&#8217;s consider the following set of URIs (I&#8217;m going to go ahead and apply rule #1 right now)&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;&lt;code&gt;/article/1;edit&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/article/1&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;These URI fragments are readily understandable enough, and there&#8217;s obviously a lot going for the notion of human factors in URIs (strange, Google has nothing on URI Human Factors).  But if you want RESTful URIs, then the verb &lt;code&gt;edit&lt;/code&gt; has got to go.&lt;/p&gt;


&lt;p&gt;It&#8217;s clear then that what I need to do is somehow qualify the noun in those URI fragments.  &lt;code&gt;/article/1&lt;/code&gt; can be used to retrieve a default view of an article &#8211; likely a read-only view.  If I want an editable view, I need to somehow modify the noun.  Here are some approaches:&lt;/p&gt;


&lt;ol&gt;
&lt;li&gt;&lt;code&gt;/article/editable/1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/editable/article/1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/article/form/1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/form/article/1&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;I&#8217;ve come up with two ways to qualify different views of an article: by adding an adjective (1 and 2), and by adding a noun (3 and 4).  Either seems to work fine, and some URI fragments (2, 3) read more naturally than others.&lt;/p&gt;


&lt;p&gt;&lt;strong&gt;Rule # 2: When creating alternate views of the same data, consider using compound nouns or an adjective-noun pair, depending on the problem space you&#8217;re working in.  Whichever style you choose, stick with it for the entire site.&lt;/strong&gt;&lt;/p&gt;


&lt;h2&gt;Scaling URIs for many views of the same data&lt;/h2&gt;


&lt;p&gt;Alright, if you&#8217;re willing to accept either style (because I can&#8217;t see that there&#8217;s anything to always prefer one over the other), then we should see how this applies to more complicated examples.&lt;/p&gt;


&lt;p&gt;Consider a web application that&#8217;s used to manage information about international shipments.  The amount of information that can be required to ship a package is absolutely astonishing, depending on what you want to ship where, so naturally, if someone wishes to change something in their shipment information, it would be good not to have them look at a huge page full of form fields.&lt;/p&gt;


&lt;p&gt;If you are dealing with such a form, here&#8217;s how you might want to construct multiple editable views on the data:&lt;/p&gt;


&lt;ul&gt;
&lt;li&gt;/shipment/1/editable/shipping-address&lt;/li&gt;
&lt;li&gt;/shipment/1/shipping-address/form&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;It should be pretty easy to guess at what&#8217;s being asked for here &#8211; we use the &lt;code&gt;/1&lt;/code&gt; to qualify which shipment we want to view, then further qualify what it is about that shipment we want to inspect.&lt;/p&gt;


&lt;p&gt;&lt;strong&gt;Rule #3: Given a complicated object with simple parts, design your URI so that you first qualify which complicated object you need a view on, then select which view you want on the simple part.&lt;/strong&gt;&lt;/p&gt;


&lt;h2&gt;Combining the Rules&lt;/h2&gt;


&lt;p&gt;If you consider what I&#8217;ve covered in light of the quadrant graph above, I&#8217;ve defined  some rules for addressing lists vs. single items, and read-only vs. editable views.  Let&#8217;s look at how they could be combined.  In getting this far, I&#8217;ve already combined the rules for some situations, so some of the example URI fragments already look familiar.&lt;/p&gt;


&lt;table&gt;
&lt;caption&gt;Illustration of URI fragments that leverage the 3 URI Design Rules&lt;/caption&gt;
&lt;tr&gt;
&lt;th&gt;Type of view requested&lt;/th&gt;
&lt;th&gt;Sample URI&lt;/th&gt;
&lt;th&gt;Suggested interpretation&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Read-Only; Single Item&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/article/1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;View article &#8216;1&#8217; &lt;i&gt;Rule 1&lt;/i&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Read-Only; List&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/articles/javascript&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;View all javascript articles &lt;i&gt;Rule 1&lt;/i&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Editable; Single Item&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/article/form/1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;View an editable version of article &#8216;1&#8217; &lt;i&gt;Rules 1, 2&lt;/i&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Editable; List&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/articles/javascript/title/form/&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;View an editable list of titles for all javascript articles  &lt;i&gt;Rules 1, 2, 3&lt;/i&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;


&lt;h2&gt;Conclusions&lt;/h2&gt;


&lt;p&gt;I wouldn&#8217;t be surprised if, upon looking at some of these examples, your first thought would be &lt;em&gt;&#8220;is that it?&#8221;&lt;/em&gt; because many of the URI fragments don&#8217;t look at all that weird or new.  And that&#8217;s as it should be.  A lot of us who care about designing URIs are probably doing many of these things right without breaking a sweat.&lt;/p&gt;


&lt;p&gt;And yet, we&#8217;re still left with conversations like you see in the comments of Dave Thomas&#8217; &lt;a href="http://pragdave.pragprog.com/pragdave/2007/03/the_radar_archi.html"&gt;RADAR article&lt;/a&gt;, and there are still a lot of people who are building websites with weird structuring conventions.&lt;/p&gt;


&lt;p&gt;For your convenience, here are the 3 Rules of URI design in one place:&lt;/p&gt;


&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Use plural nouns to represent lists of things; use singular nouns to represent 1 particular thing.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;When creating alternate views of the same data, consider using compound nouns or an adjective-noun pair, depending on the problem space you&#8217;re working in. Whichever style you choose, stick with it for the entire site.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Given a complicated object with simple parts, design your URI so that you first qualify which complicated object you need a view on, then select which view you want on the simple part.&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;I&#8217;m interested in your comments. I&#8217;m not married to any of this; if you can convince me that there&#8217;s a better way of structuring the problem, or a better way of solving some of these issues, I&#8217;ll be happy to modify the document accordingly.  I&#8217;d like this to become a resource that people who want to follow best practices.&lt;/p&gt;

</content>
  </entry>
  <entry>
    <id>tag:blog.roberthahn.ca,2007-04-05:/articles/2007/04/06/the-web-needs-killlfiles/</id>
    <title type="html">The Web Needs Killlfiles</title>
    <published>2007-04-06T00:21:06Z</published>
    <updated>2009-03-21T21:14:58Z</updated>
    <link href="http://blog.roberthahn.ca/articles/2007/04/06/the-web-needs-killlfiles/" rel="alternate"/>
    <content type="html">&lt;h1&gt;The Web Needs Killlfiles&lt;/h1&gt;


&lt;p&gt;&lt;a href="http://www.tbray.org/ongoing/When/200x/2007/03/27/On-Aggression#c1175552573.723464"&gt;Nico&lt;/a&gt;: &lt;em&gt;&#8220;In the days of Usenet you could use a killfile.&#8221;&lt;/em&gt;&lt;/p&gt;


&lt;p&gt;You can do that on the web too.  Imagine a killfile service, to be set up beside OpenID services.  This service will accept as input the OpenID of anyone needing to be put in the killfile, and will output, on demand, an JSON string of the contents.&lt;/p&gt;


&lt;p&gt;Sites wishing to take advantage of that service must do a couple of things. First, allow comment posting only by people with OpenID accounts.  Second, fetch the authenticated person&#8217;s killfile with JavaScript and use it to conceal from view any comment posted by persons in the killfile.  The only thing you&#8217;d need to agree upon is a comment microformat.&lt;/p&gt;


&lt;p&gt;If you want to set up a server-based filtering system, it&#8217;ll require more work, but it can be done.&lt;/p&gt;


&lt;p&gt;Stupid idea?&lt;/p&gt;

</content>
  </entry>
  <entry>
    <id>tag:blog.roberthahn.ca,2007-04-05:/articles/2007/04/05/css-naked-day/</id>
    <title type="html">CSS Naked Day</title>
    <published>2007-04-05T11:24:53Z</published>
    <updated>2009-03-21T21:14:58Z</updated>
    <link href="http://blog.roberthahn.ca/articles/2007/04/05/css-naked-day/" rel="alternate"/>
    <content type="html">&lt;h1&gt;CSS Naked Day&lt;/h1&gt;


&lt;p&gt;&lt;a href="http://naked.dustindiaz.com/"&gt;CSS Naked Day&lt;/a&gt; today. Don&#8217;t think about it. Just do it.&lt;/p&gt;

</content>
  </entry>
</feed>

