How To > Ruby Code Examples > How Do I Use Ruby to Query A SPARQL Endpoint?

Search the FAQ for entries containing:

Ruby SPARQL Scripts


Example 1

I wrote this to use as a command line tool, and as an example of how you can use Ruby for querying SPARQL endpoints. You will need Ruby, RubyGems, as well as, the gems RDF and sparql-client installed. This should allow you to run a SPARQL Query from the command line that returns the URI's for a given species name if that name occurs with in the TaxonConcept, DBpedia or EUNIS data sets.

 

Console view how to run script example 1
#!/usr/bin/env ruby
require 'rubygems'
require 'sparql/client'
# run this from the command line
# ruby get_txn.rb
# Example:
# ruby get_txn.rb "Puma concolor"
# Author: Peter J. DeVries
# Date: 2011-11-24

class App
  VERSION = '0.0.1'
  TXN_SPARQL_ENDPOINT = "http://lsd.taxonconcept.org/sparql"
  DBPEDIA_SPARQL_ENDPOINT = "http://dbpedia.org/sparql"
    
  attr_reader :options

  def initialize(argument, stdin)
    @argument = argument
    @stdin = stdin
  end

  def process_standard_input
      input = @stdin.read      
  end

# Retrieve the matching URI's from TaxonConcept.org
 def run_txn
  sparql = SPARQL::Client.new(TXN_SPARQL_ENDPOINT)
  tab = "\t"
  predicate = ''
  subject_type = ''
  object = '"' + @argument[0].to_s + '"'
  result = sparql.query("SELECT DISTINCT ?s WHERE { ?s #{predicate} #{object}.  ?s a #{subject_type}.  }")
  result.each do |line|
    subject  = line[:s].to_s
    puts "TXN" + tab + object + tab + subject
  end #do each line
 end #def run

# Retrieve the matching URI's from DBpedia.org
 def run_dbpedia
  sparql = SPARQL::Client.new(DBPEDIA_SPARQL_ENDPOINT)
  tab = "\t"
  predicate = ''
  object = '"' + @argument[0].to_s + '"'
  result = sparql.query("SELECT DISTINCT ?s WHERE { ?s #{predicate} #{object}@en }")
  result.each do |line|
    subject  = line[:s].to_s
    puts "DBPEDIA" + tab + object + tab + subject
  end #do each line
 end #def run

# Retrieve the matching URI's from eunis.eea.europa.eu
 def run_eunis
  sparql = SPARQL::Client.new(TXN_SPARQL_ENDPOINT)
  tab = "\t"
  predicate = ''
  object = '"' + @argument[0].to_s + '"'
  result = sparql.query("SELECT DISTINCT ?s WHERE { ?s #{predicate} #{object}  }")
  result.each do |line|
    subject  = line[:s].to_s
    puts "EUNIS" + tab + object + tab + subject
  end #do each line
 end #def run


end #Class App

# Create and run the application
app = App.new(ARGV, STDIN)
app.run_txn
app.run_dbpedia
app.run_eunis

 

Example 2

This example allows you to query DBpedia.org using a binomial name. It returns the species URI and the DBpedia classification for that species including the Kingdom, Phylum, Class, Order and Family.

 

Console view how to run script example 2
#!/usr/bin/env ruby
require 'rubygems'
require 'sparql/client'
# run this from the command line
# ruby get_dbcl.rb "Aedes vexans"
# Example:
# ruby get_dbcl.rb "Puma concolor"
# Author: Peter J. DeVries
# Date: 2011-11-26

class App
  VERSION = '0.0.1'
  TXN_SPARQL_ENDPOINT = "http://lsd.taxonconcept.org/sparql"
  DBPEDIA_SPARQL_ENDPOINT = "http://dbpedia.org/sparql"
    
  attr_reader :options

  def initialize(argument, stdin)
    @argument = argument
    @stdin = stdin
  end

  def process_standard_input
      input = @stdin.read      
  end

# For a given binomial name, retrieve the kingdom, phylum, class, order, family from DBpedia.org
 def run_dbpedia
  sparql = SPARQL::Client.new(DBPEDIA_SPARQL_ENDPOINT)
  tab = "\t"
  binomial = ''
  regnum   = ''
  phyl     = ''
  classis  = ''
  ordo     = ''
  familia  = ''
  species = '"' + @argument[0].to_s + '"'
  result = sparql.query("SELECT DISTINCT ?s ?kingdom ?phylum ?class ?order ?family WHERE
                         {
                          ?s #{binomial} #{species}@en.
                          ?s #{regnum} ?kingdom.
                          ?s #{phyl} ?phylum.
                          ?s #{classis} ?class.
                          ?s #{ordo} ?order.
                          ?s #{familia} ?family.}")
  result.each do |line|
     dbpedia_uri = line[:s].to_s
     kingdom_n   = line[:kingdom].to_s
     phylum_n    = line[:phylum].to_s
     class_n     = line[:class].to_s
     order_n     = line[:order].to_s
     family_n    = line[:family].to_s
     species_n   = species.gsub("\"", "")
     puts dbpedia_uri + "," + kingdom_n + "," + phylum_n + "," + class_n + "," + order_n + "," + family_n + "," + species_n
  end #do each line
 end #def run_dbpedia


end #Class App

# Create and run the application
app = App.new(ARGV, STDIN)
app.run_dbpedia

Last updated on January 26, 2011 by Pete DeVries