« May 2007 | Main | July 2007 »
June 29, 2007
Haiku0002
Recently, somehow, haikus and tech-geekery got linked in my brain. I think it happened when some piece of press pointed out the fact that the maximum allowed length of a Twitter update (140 characters) it just about exactly right for most haikus.
Since then, said brain seems intent on summarizing certain technical thoughts in haiku form. So, since I've got 'em, I decided to start posting 'em to my tumblog. And I was proud enough of one that I thought it was worth reproducing here. It's called Haiku0002:
All the names of things melt away like late spring snow. Tagging will save us.In re: Everything Is Miscellaneous by David Weinberger, Clay Shirky on "Making Digital Durable" at the Long Now
For more, you can follow along.
Tagged: haiku, weinberger, shirky, tagging, folksonomyPosted by Greg at 5:46 PM | Comments (0)
June 15, 2007
Presenting THUMBNAIL: a Ruby wrapper for the AWS Alexa Site Thumbnail service
I'm proud to announce the release of my very first gem: THUMBNAIL. It's a Ruby wrapper for the Amazon Web Services Alexa Site Thumbnail Service, which lets you automatically download thumbnails of any website or dynamically embed them in your own pages for an incredibly small fee ($0.20 per 1000 images). And it has a badass logo (see above)!
You can get it from RubyForge thusly:
$ sudo gem install thumbnail
You've got to do some bureaucratic overhead at Amazon before you can play (more about that on the THUMBNAIL homepage), but once you do it's just as easy as pie to download pix of sites from around the web like so:
require 'rubygems'
require 'thumbnail'
require 'open-uri'
t = Thumbnail::Client.new :access_key_id => YOUR_ACCESS_KEY_ID,
:secret_access_key => YOUR_SECRET_ACCESS_KEY
url = t.get("www.urbanhonking.com")[:thumbnail][:url]
File.open("urho.jpg", "w") { |f| f.write open(url).read }
where YOUR_ACCESS_KEY_ID and YOUR_SECRET_ACCESS_KEY are things you get from Amazon when signing up for the service. Running such code would make you the proud owner of a local copy of an image like this:
THUMBNAIL will also build you a url you can include in any webpage that will redirect to the site thumbnail you want like so:
require 'thumbnail'
t = Thumbnail::Client.new :access_key_id => YOUR_ACCESS_KEY_ID,
:secret_access_key => YOUR_SECRET_ACCESS_KEY,
:action => :redirect
url = t.get("www.twitter.com")
#=> http://ast.amazonaws.com/?Action=Redirect&AWSAccessKeyId=YOUR_ACCESS_KEY_ID
&Signature=sdhfiawrkjw3h9bncoa8ue&Timestamp=2007-06-14T09:09:18.000Z&Url=www.tw
itter.com&Size=Large
I'm working on a Rails plugin that will provide a view helper so you can easily do this from any template, but for now you'll have to be satisfied with the additional sample code available on the THUMBNAIL RDoc.
There are all kinds of other details available at the THUMBNAIL homepage. And the code is, of course, available to all for free under the MIT License. So, Go! Get your thumbs dirty! Enjoy!
Tagged: thumbnail, ruby, aws, amazon, web, service, alexa, api, rubyforge, gemPosted by Greg at 12:33 AM | Comments (2)
June 12, 2007
A Beginner's Guide to Practical Syntactic Magic: the tale of Hpricot's sudo-constructor
I spent much of today working with Hpricot. And so, as when spending significant solo time with any of why the lucky stiff's code, I found myself admiring all the neat little syntactic nicknacks strewn about to cozy up the place.
One of the best is the way you get started. Hpricot is a toolkit for parsing and manipulating XHTML. So, obviously enough, just about every time you invoke it, you're going to want to pass it an XHTML document so it can, you know, prep it for parsing and manipulation. And how do you do that? What's the syntax?
Hpricot(my_document)
That's it. There's no "Hpricot::Base.new(my_document).parse" nonsense, or any of the other more or less torturous common options. Not a single character of syntax is wasted.
But, if you're a mere Ruby mortal, like me, you're probably looking at that code and going: 'Huh?' Isn't Hpricot a constant? It's capitalized. But it's taking an argument like a method. How is that even valid Ruby? How can the parser tell if it's a constant or a method?
Well, it turns out that there's no rule against having capitalized method names; the parser can tell it's a method because it's got an argument. And that's all that's required for it to be sent off to method- instead of constant-dispatch (as Chris pointed out, this is one advantage of not having Ruby be "turtles all the way down"; Smalltalk couldn't do this).
Beyond providing fodder for a Language Nerd Attack, though, what's the upshot? How's this fact help the man on the street? Well: there's nothing actually sophisticated going on here. So: you can do it too.
Here's an admittedly contrived (and useless) example:
class Dogger
def initialize
puts "dog"
end
end
def Dogger()
Dogger.new
end
a simple class definition followed by a simple method invoking it.
Which leaves us with the ability to write two snippets of code that, while they may look nearly the same, do very different things:
>> Dogger
=> Dogger
>> Dogger()
dog
=> #<Dogger:0x15d2478>
and that is exactly from where _why's use of this little quirk derives its leverage. This trick makes you feel like you're invoking a constructor or calling some other kind of class method when you are, in fact, doing nothing of the sort. Just as our Dogger() method above needn't have done anything remotely related to the Dogger class, _why could have named his method Clown() or ChunkyBacon() while still calling Hpricot.parse(input, opts) inside it (which is exactly what Hpricot() does).
But his chosen usage is particularly inspired. In one fell swoop, he gives his whole complex feature-ful library a single welcoming point of entry. You need never concern yourself with the internal machinery; just heave a document over the transom and let the library figure out what to do with it. And this is the wider lesson of _why: real power comes from combining the playfulness (better: the insouciance) needed to probe, question, and even bend the limits of the language with the discipline and aesthetic sense required to use what you find not to obfuscate and confuse, but to write elegant and, above all, more humane code.
I mean, Hpricot would definitely not be a better library if that method was called ChunkyBacon(). Right?
Tagged: ruby, why, dispatch, hpricot, syntax, chunkybaconPosted by Greg at 1:29 AM | Comments (10)


