From 38bd4b859f99065ee183e6d35e960889c01bef32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2EFernando=20S=C3=A1nchez?= Date: Thu, 15 Mar 2012 16:09:37 +0100 Subject: [PATCH] Converted the dbpedia example to a generic lib --- README.md | 2 +- index.js | 2 ++ lib/hook.io-sparql.coffee | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 index.js create mode 100644 lib/hook.io-sparql.coffee diff --git a/README.md b/README.md index ecf03b8..1be2d19 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Description --------------------- This module is part of the Web4.0 project. -So far, it performs basic queries to dbpedia, but the same code could be use to query any other sparql endpoint. +You can use it to query any sparql endpoint. So far, dbpedia and sindice.com have been tested. Dependencies -------------------- diff --git a/index.js b/index.js new file mode 100644 index 0000000..27eef1f --- /dev/null +++ b/index.js @@ -0,0 +1,2 @@ +require('coffee-script') +require('./lib/hook.io-sparql') diff --git a/lib/hook.io-sparql.coffee b/lib/hook.io-sparql.coffee new file mode 100644 index 0000000..dfabd9f --- /dev/null +++ b/lib/hook.io-sparql.coffee @@ -0,0 +1,39 @@ +# Imports +sparql = require 'sparql' +Hook = require('hook.io').Hook + +examplequery = " + SELECT DISTINCT ?label ?abstract ?population + WHERE { ?place rdf:type dbpedia-owl:PopulatedPlace. + ?place foaf:name ?label. + ?place dbpprop:population ?population. + OPTIONAL{ + ?place dbpedia-owl:abstract ?abstract. + FILTER langMatches( lang(?abstract), 'en') + } + } LIMIT 10" + + +class SPARQLHook extends Hook + constructor: (options) -> + self = this + Hook.call(self, options) + @client = new sparql.Client options.url + + self.on 'hook::ready', -> + self.on '*::query', (data,fn) -> + @queryDBPedia data , (res) -> + console.log res + fn res + console.log "Created" + + + query: (query, cb) -> + console.log ">>Going to query" + @client.rows query, (err, res) -> + cb res + + addPreffix: (key,url) -> + @client.prefix_map[key] = url + +exports.SPARQLHook = SPARQLHook