Screencast: Use Rails and RDF.rb to parse Best Buy product reviews

Posted by Eric Kidd Sun, 05 Jun 2011 19:07:00 GMT

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)

Tags , ,  | 1 comment


  1. Gregg Kellogg said 2 days later:

    Great demo Eric! For another take on parsing Best Buy data, use (source available on GitHub as rdf-portal), also using the RDF.rb gems, to generate HTML+RDFa (or any other serialization) marked up with the same information as the search requests.

    Also, we do have SPARQL support, but not in the released versions yet. Check out, which uses the 0.4.x tag of RDF.rb to provide full SPARQL 1.0 support natively in Ruby.

    Thanks for the great work and helping to show what a great environment for working with RDF Ruby really is.


(leave url/email »)

   Preview comment