In the past few years, many companies have been embedding machine-readable metadata in their web pages. Among these is Best Buy, which provides extensive RDFa data describing their products, prices and user reviews.

The following 20-minute screencast shows how to use Ruby 1.9.2, Rails 3.1rc1, RDF.rb and my rdf-agraph gem to compare user ratings of the iPad and various Android Honeycomb tablets.


Formats: Ogg Theora, MP4

If you're at SemTech 2011 this coming week, and you'd like to learn more about Ruby and RDF, you're welcome attend my talk on Wednesday afternoon. I'll discuss some great tools from the RDF.rb maintainers, and suggest ideas on where to go from here.


Here's what the final output looks like:

Code snippets from the video

Add the following lines to your Gemfile and rerun bundle install:

gem 'rdf', '~> 0.3.3'
gem 'rdf-rdfa', '~> 0.3.3'
gem 'rdf-agraph', '~> 0.3.1'

Create config/initializers/rdf.rb to set up our RDF repository and vocabulary:

require 'rdf/rdfa'


DV ="")

Run the following code under rails console to download the RDFa data and store it in AllegroGraph:

graph = RDF::Graph.load("*")

Create app/controllers/reviews_controller.rb to perform the query and bind @reviews for use in our view:

class ReviewsController < ApplicationController
  def index
    @reviews = REPO.build_query do |q|
      q << [:review, RDF.type, DV['Review-aggregate']]
      q << [:review, DV.itemreviewed, :name]
      q << [:review, DV.rating, :rating]
      q << [:rating, DV.rating, :stars] do |review|

Create app/views/reviews/index.html.erb to display the product ratings:

  <% @reviews.each do |review| %>
      <td><%= stars(review.stars) %></td>
      <td><%= sanitize( %></td>
  <% end %>

And add a helper function in app/helpers/application_helper.rb to display the stars:

module ApplicationHelper
  def stars(literal)
              :width => 100, :height => 20)