Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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

·
Free Resource

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 " SocialCamp Vancouver (Incl. Facebook Developer Garage) - Open Web Edition" on the Yahoo! Upcoming service using mofo – a microformat parser in Ruby .

require 'mofo'
require 'pp'

event = hCalendar.find 'http://upcoming.yahoo.com/event/444250/'
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:
=> "SocialCamp Vancouver (Incl. Facebook Developer Garage) - Open Web Edition"

puts event.dtstart
Prints the event start date:
=> "Tue Apr 15 25 00:00:00 -0800 2008"

puts event.description
Prints the event description:
=> "We're back. Join us for Vancouver's 3rd Facebook Developer Garage and 1st" 
"(Open)Social(Web)Camp hosted at the Open Web Vancouver 2008 Conference on Day 2. "
"Please note, to attend you will need an Open Web Vancouver 2008 ticket."

"We have a SocialCampVancouver event wiki. Tell us about your Facebook or social network"
"web app and sign-up for a live demo and Q&A or sign-up for a lightning talk."
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.
Topics:

Published at DZone with permission of Gerald Bauer. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}