rubybook = Blog.new :title => “Gerald Bauer’s Ruby Notebook”, :about => “Internet Professional in Vancouver, B.C.”

January 18, 2008

Mofo - Getting Started w/ Microformats using Ruby - Web 3.0 In Action

Filed under: Microformats, Ruby, giantglobalgraph, hcalendar, hcard, mofo, semanticweb, web30 — Gerald @ 3:08 am

What are Microformats? Microformats let you turn your web page into a web service using nothing more than simple coding conventions and patterns that work in today’s browsers. If you markup events, people, reviews, locations and more in your web page using Microformats you contribute to the next version of the web – known as the Semantic Web, Web 3.0 or the Giant Global Graph (GGG).

Let’s skip the theory and dive into code and tap into the Giant Global Graph.

Let’s load up the event listing web page for the Ruby on Rails Workshop on the Yahoo! Upcoming service using mofo – a microformat parser in Ruby .

require 'mofo'
require 'pp'

event = hCalendar.find 'http://upcoming.yahoo.com/event/390146/'

Using hCalendar.find we tell the mofo microformat parser to look for all events marked up in the web page using the hCalendar microformat standard.

That’s all. Now we’re ready to play with the event data:

puts event.summary

Prints the events one-line summary:

=> Intermed. Ruby on Rails Workshop (Incl. Facebook and Mobile Web)

puts event.dtstart

Prints the event start date:

=> Fri Jan 25 00:00:00 -0800 2008

puts event.description

Prints the event description:

=> On Friday Jan 25, 2008, Scott Patten and Gerald Bauer (Rails Advance) are giving a full-day workshop on Intermediate Ruby on Rails at BCIT’s downtown campus. Sign-up and learn how to build a complete RESTful web application and web service using Rails 2.0 and then use ActiveResource to build a facebook and mobile application on top of the web service.

Using the pretty printer (pp) module we can print the hCard microformat inside the hCalendar microformat used to markup the event’s venue name and address:

pp event.location

Pretty prints the hCard microformat data:

#<HCard:0x5225a88
 @adr=
  #<Adr:0x5249d70
   @locality="Vancouver",
   @postal_code="",
   @properties=["locality", "street_address", "region", "postal_code"],
   @region=”British Columbia”,
   @street_address=”555 Seymour Street”>,
 @fn=”BCIT Downtown Campus”,
 @org=”BCIT Downtown Campus”,
 @properties=["fn", "adr", "org"]>

We can, of course, print the items one at a time:

puts event.location.fn

Prints the event’s venue name and fn stands for full name:

=> BCIT Downtown Campus

puts event.location.adr.locality

Prints the event’s venue locality, that is, the city, village, or hamlet:

=> Vancouver

puts event.location.adr.street_address

Prints the event’s venue street address:

=> 555 Seymour Street

And so on and so forth. To see the fully monty pretty print the hCalendar microformat and stand back 1000 meters:

pp event

That’s all to get started using Microformats in Ruby thanks to the mofo parser library.

Interested in learning more? Sign-up today for the world’s 1st Ruby on Rails workshop covering the Semantic Web, Web 3.0, Microformats and the Giant Global Graph.

Any comments, corrections or additions about “Mofo - Getting Started w/ Microformats using Ruby - Web 3.0 In Action”? Send them along to the Vancouver.rb Mailing List/Forum. Thanks!

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Blog at WordPress.com.