InquiryLabs

Politics, Programming and Possibilities

Archive for August, 2007

Another Major Change to Merb Routes

I updated the routing system to Merb again yesterday. I’ve added plenty of specs and examples now to illustrate its flexibility and usefulness. What I used to call “late-bound routes” is now “deferred routes” which makes more sense.

Take a look at the new specs for a run-through.

TextMate Footnotes Has Moved

TextMate’s Ruby on Rails bundle has changed directories, and along with it, the Footnotes plugin. People are reporting that “script/plugin install” results in “Plugin not found: http://macromates.com/svn/Bundles/trunk/Bundles/Rails.tmbundle/Support/plugins/footnotes”. Indeed, it can now be found at:

http://macromates.com/svn/Bundles/trunk/Bundles
/Ruby on Rails.tmbundle/Support/plugins/footnotes

(no spaces or newlines)

To switch from the old address to the new, just use “svn switch [new-url]” at the command line.

Also, for those of you on edge rails, you can point your scripts / svn clients at:

http://macromates.com/svn/Bundles/trunk/Bundles
/Ruby on Rails.tmbundle/Support/plugins/footnotes-edge

Enhancing the ‘irb’ Experience

I use the ‘irb’ command at the terminal every day. In case you program in Ruby and haven’t used it yet, you’re missing out. But as it turns out, so was I. The irb program can be configured to do almost anything, and specifically, people have already done stuff like this:

  • Autocompletion… wow. Just press tab on any partially-complete method name and irb will complete the rest for you.
  • Save and restore the commands you type between sessions (use the “up” cursor key to cycle through past commands, like normal)
  • Use the “ri” command right inside of irb. You won’t need to have two terminals open to reference the core or standard library documentation any more.

I’ve uploaded my .irbrc file here for anyone to use and/or modify. Just create a “.irbrc” file in your home directory (on a unix-based machine) and paste it in there. Yum!

Getting to Know Merb

Merb is in its incubation phase—small, changing quickly, and growing fast. As I’ve mentioned in previous posts, it’s a light-weight framework similar to Rails. Here is a list of resources on the web I’ve found useful as I’ve been introduced to this mini-framework:

If you have a half hour, I highly recommend Ezra’s hoedown presentation listed first above. He talks about his design philosophy (small, fast, less magic) and the features now available as well as upcoming.

New Router in Merb

Note: The API has significantly changed since this posting. Best to check out this post for details.

With Ezra’s approval, I committed some significant changes to the Merb routing system last night. If you’ve ever been limited by Rails’ routing system, you might want to check out some of the features now in Merb. Here’s a run-down of some ways you can use the new system:

Construct Params from Matched Portions of the URL

Merb::Router.prepare do |r|
  # There is now a named_route method which is
  # basically a simple_route plus a URL generator
  r.named_route :test,
    "/:controller/:action/:id/:prefix",
    :action => ':prefix_:action'
end

# In a controller or view, we can use its generator:
url :test,
  :controller => "users",
  :action => "show",
  :id => 1,
  :prefix => "alternate"

# => "/users/show/1/alternate

# Example controller class that would respond properly:
class Users < Application
  def simple_show
    #
  end
  def alternate_show
    #
  end
end

Use Any Request / Environment Variable

Merb::Router.prepare do |r|
  # Send all SSL / HTTPS traffic to the Secure class, 'index' action
  r.route :protocol => /https/,
    :controller => "secure",
    :action => "index"

  # Use the 'admin' subdomain to route to the admin module
  r.route :domain => /^admin\\./,
    :path => %r[/~~/~~/~~],
    :controller => "admin/:path[1]",
    :action => ":path[2]",
    :id => ":path[3]"
end

Note in the above example that some of the keys in the hash are environment variables (:domain, :path) and some are outputs to the params hash (:controller, :action, :id). This may change in the future, but for now it just means you have to know that the environment variables are “reserved words”, used specifically for matching against the incoming URL request.

Also, you may wonder at the “~~” and “:path[1]” notation. The “~~” in the path is a special string sequence that gets replaced with a parenthetical regular expression that will match a normal URL segment (e.g. it will match characters such as a-z but not ‘/’ or ‘?’). Next, the “:path[1]” notation is used to tell the router how to reconstruct the params from the matched portions of the request / environment variables. So, in the above example, the :controller is going to be “admin/” plus whatever gets matched in the first “~~”. Here’s an example match. Let’s use the URL, “http://admin.mysite.com/books/show/1″:

# Routes to the controller Admin::Books, and the "show" action
module Admin
  class Books < Application
    def show
      params[:controller] # => "admin/books"
      params[:action] # => "show"
      params[:id] # => "1"
    end
  end
end

A full list of request / environment variables that you can use is available in the lib/merb/router.rb class, but here is a sampling:

  • protocol
  • domain
  • port
  • path
  • method (e.g. ‘put’, ‘post’, ‘get’)
  • query_string
  • accept
  • remote_ip
  • user_agent
  • referer

This flexibility could, for example, let you essentially create two different sets of views for Mozilla vs. Internet Explorer browsers (routes would distinguish between browsers using the “user_agent” variable). Or you could ban certain IP addresses. Or allow certain routes from third party sites using the “referer”. Lots of possibilities.

Late-bound Route Decisions

Merb::Router.prepare do |r|
  # Query the database for an object type, and then route
  # to the appropriate controller based on type
  r.route do |request|
    if request.path.match(%r{/([a-z0-9\-] )})
      guid = $1
      if obj = AppObject.find_by_guid(guid)
        {:controller => obj.type, :action => 'show', :id => guid}
      end
    end
  end
end

Some things may continue to change as I get feedback and also optimize the system, but the above examples should stand as a doorway for solutions to your own development needs. Let me know if you have any special requests and I’ll try to post more later.

Changes to Merb in the Last Week

I’ve been busy working on FamilyLearn projects and haven’t had time until now to pay attention to the many changes that Ezra and Jon have been making to Merb. Here’s a summary of some noteworthy changes over the past week:

  • There is no longer a “dist” folder. The Merb directory structure is now even closer to Rails’ directory structure.
  • Created a “mixins” directory and removed “merb_” prefixes from Merb’s inner workings.
  • An interesting ticket was submitted which documents how to progressively stream HTTP output in Merb/Mongrel. (Thanks to b.candler and ry)
  • You can now “throw” content and “catch” it again from any partial or view.

You Can Make Your Own Books at MemoryPress.com

The MemoryPress project I’ve been working on for about a year and a half now is finally ready for prime time! It’s purpose is to let you create a quality hard-bound book by collaborating with as many people as you would like. It’s ideal for a personal history, a wedding gift, or a 50th anniversary gift, for example. We’ve also rolled out a journaling app called Pyxlin with similar features.

One of the features that sets MemoryPress apart from other online book-making sites is that it actually uses a professional typesetting back-end (LaTeX) to create and typeset the book. The result is something that makes even amateur work look beautiful—text flows from page to page and around photos, with the correct proportions in the margins and distances between sections. On the back-end, we have a distributed Rails app on Amazon’s EC2 servers and some heavy javascript (yootools) on the front-end for the book editor.

Our goal at FamilyLearn is to create a searchable online database of your family’s memories in written, audio and video form. MemoryPress is the first product released to that end.

Random Idea: Trees as Networks

I often have random ideas and usually let them pass by, either because “someone else must have thought of it already,” or I’m too rushed, or perhaps I feel a little embarrassed by being different and having weird ideas. And then there’s that thought at the back of the mind that lurks in every capitalist’s mind: “but if I keep it a secret, I can profit from it and prevent all those other people from getting rich from my ingenuity.” But no more—it’s time to start documenting these things. The world always needs good ideas, and good ideas just start with plain old ideas.

Today, my coworker Paul and I were talking about the fragility of the internet. If you’ve ever been a network administrator, you can probably sympathize with me if I say that making things work and stay working can be troublesome. But the internet as a whole is pretty good these days, considering that it’s a man-made invention.

But just a few weeks ago, a power outage in San Francisco caused an internet black-out for several popular social networking sites. The rumor was that some drunk employee arrived at work at a hosting company and started unplugging servers at random. It seems the rumor was unsubstantiated, but still, the possibility remains that someone could do such a thing. The main thing keeping the internet together is the intense interest that we as a global community have in keeping it together—especially companies that have money and want to make more.

What would happen if that common interest waned, however? What will our society be like 100 years from now, or 500? Will we still have an internet?

Maybe. But if we could make global communication a more natural state, imagine how that would protect this critical infrastructure. What if, for example, we could use trees to communicate data, in similar fashion to the forests of Narnia? I suppose we would have to genetically alter a tree and start planting it around the world. Alas, the ability to create such a thing is beyond most of us—but wouldn’t it be a remarkable gift to future civilizations to have a communication network built in, as a natural state of the earth?

  • 3 Comments
  • Filed under: Uncategorized
  • Everyone should cheer for California Secretary of State Debra Bowen and the geek squad that worked with her to find out just what security vulnerabilities existed in the 5 brands of electronic voting machines used in California.

    Bowen announced that Diebold and Sequoia voting machines would not be used with the exception of people with disabilities, and that the other brands would be re-certified when certain security requirements were met.

    It’s been known for some time now that these electronic voting machines are exceptionally vulnerable to hackers. That the companies who make these machines would go on record to say that “Electronic voting systems have never been successfully tampered with in an actual election” is naive at best, and seems to imply that if they have never been tampered with then they could never possibly be tampered with in the future. I’m glad that the California Secretary of State is wiser than that.

    As a software engineer and computer scientist, I feel that I am in a special position to know just how easy it is to leave loopholes or backdoors in a product. There is very little stopping me, except for my own sense of honesty and perhaps a small possibility that I could potentially be caught for such mischief. But when it comes to something as sensitive as the basis of our freedom and liberty, I expect a much higher level of scrutiny on the companies and individuals who could undermine it.

    The saying, “He who votes decides nothing; he who counts the votes decides everything” is attributed to Joseph Stalin. I hope this new standard of scrutiny is extended beyond the state of California.

    Money, Banking and the Federal Reserve

    How did we get a Federal Reserve, and what does it do for us? Why does the Mises Institute say that it needs to go?

    I found this 1996 video production from the Mises Institute quite informative:

    Money, Banking and the Federal Reserve

    It’s 40 minutes long. Ron Paul is quoted a few times in there, back when he was “between elections” (he’s been in congress on and off for the past 25 years or so—1996 was an off year, I understand.)

    My belief that we are in the middle of an economic “bust” motivated me to check it out.

  • 2 Comments
  • Filed under: Economy