Clever Ruby Idiom
Posted 2006 Sep 23
def sometimes
yield if rand > 0.5
end
Now I can do this:
sometimes do
some_action
end
I love that!
def sometimes
yield if rand > 0.5
end
Now I can do this:
sometimes do
some_action
end
I love that!
<!-contact form->
Looks like Jeremy Zawodny is back at the Yahoo! Developer Network. I posted my request regarding adding support for Yahoo! Shortcuts. Hopefully we’ll see some action!
Another day, another weather/Ruby/IRC plugin :)
This time it’s an rbot IRC plugin for weather forecasts. It also uses the Yahoo Maps API, so any free-form location works, e.g.
rbot forecast Tulsa, OK
or
rbot forecast 74104
Download it here: [forecast.rb]
I have to say: I’m really impressed with Yahoo! lately. I have used a personalized My Yahoo! for the longest time, but have never really thought of Yahoo as “cool”. First, my encounter with the Yahoo Maps API, then their new ajaxalicious UI library and now a tweaked-out My Yahoo!
The new My Yahoo! has site-specific mini-icons in each of the content bars—which I great because I collate feeds from Reuters, AP, del.ico.us, and others. Besides being drag-and-drop capable, the context boxes also show a mini preview in a tooltip.
I also want to thank Yahoo for adding “Submits over SSL” to the mail login… That saves me like 10,000 clicks over the course of a year ;)
My Yahoo! is the “dashboard” that Dashboard can only hope to be: it’s open, powerful, portable and - now - sexy!
I was writing another rbot plugin - this time to get forecast data from the NOAA like my Quicksilver plugin [1] - and I needed a good way to get latitude and longitude. A quick search revealed that the Yahoo Maps API provides just such a thing!
<pre><code>
def get_lat_long(loc)
loc = ERB::Util.u(loc)
url="http://api.local.yahoo.com/MapsService/V1/geocode?appid=YahooDemo&location=#{loc}"
</code>
That’s it! Now I get back an XML document that I can easily parse with REXML. To do the same thing with SOAP requires a little bit more effort and, to me, doesn’t feel as natural.
<pre><code>
class Forecast
WSDL_URI="http://www.nws.noaa.gov/forecasts/xml/SOAP_server/ndfdXMLserver.php?wsdl"
def initialize(lat,long)
@lat,@long=lat,long
@forecaster=SOAP::WSDLDriverFactory.new(WSDL_URI).create_rpc_driver
end
def forecast
forecast = @forecaster.NDFDgenByDay(
@lat,@long,Time.now.strftime("%Y-%m-%d"),2,"24 hourly")
xml = (REXML::Document.new(forecast)).root
File.open("forecast.xml",'w') do |file|
file < < xml
end
end
end
</code>
But, most importantly, you have to use a SOAP client to interact with this service… In the case of Yahoo’s API, I can just type a URL into my browser and get results. This is minor, but can be important when first getting started or when debugging.
1 I’ll post later about the differences in writing this software in Ruby versus Cocoa/Objective-C…
To pass the time last Friday afternoon, I wrote a plugin for the Ruby IRC bot, rbot, to query the status of World of Warcraft realms.
With a single piece of code, I satisfied both my Ruby and WOW needs…
Thanks to Blizzard for providing Realm Status in an easy-to-use XML format. Thanks to rbot for bringing Ruby to IRC.
Download: realm.rb
Wishing everyone who visits mrchucho.net a Happy New Year!
I updated my This Window Firefox extension to be compatible with older (1.07) versions of Firefox. I also added it my update.rdf.