Skip to content

Instantly share code, notes, and snippets.

@bettysteger
Last active December 15, 2015 17:49
Show Gist options
  • Save bettysteger/5299316 to your computer and use it in GitHub Desktop.
Save bettysteger/5299316 to your computer and use it in GitHub Desktop.
Did anyone tried a JSON-LD Processor with a real data source, eg. DBpedia? this is what i got when using the ruby gem: https://github.com/gkellogg/json-ld could not achieve that with javascript (https://github.com/digitalbazaar/jsonld.js) yet.. is there any best practice to deal with a real data source in combination with JSON-LD?
require 'rubygems'
require 'json/ld'
require 'net/http'
context = {
"foaf" => "http://xmlns.com/foaf/0.1/",
"dbo" => "http://dbpedia.org/ontology/",
"xsd" => "http://www.w3.org/2001/XMLSchema#",
"name" => {
"@id" => "foaf:name",
"@type" => ""
},
"born"=> {
"@type" => "xsd:date",
"@id"=> "dbo:birthDate"
},
"thumb" => "dbo:thumbnail",
"homepage" => "foaf:homepage"
}
id = "http://dbpedia.org/resource/Jim_Carrey"
response = Net::HTTP.get_response("dbpedia.org","/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=DESCRIBE+%3Chttp://dbpedia.org/resource/Jim_Carrey%3E&output=application%2Fld%2Bjson")
input = JSON.parse response.body
input = input["@id"]
compact = JSON::LD::API.compact(input, context)
jim_carrey = compact["@graph"].detect do |resource|
resource["@id"] == id
end
puts jim_carrey["name"] # {"@language"=>"en", "@value"=>"Jim Carrey"}
puts jim_carrey["born"].first # 1962-01-17
puts jim_carrey["thumb"]
puts jim_carrey["homepage"]
# getting his movies
movies = compact["@graph"].map do |resource|
resource["@id"] if resource["dbo:starring"] == id
end
puts "---------------------------"
puts "Jim Carrey Movies:"
puts movies.compact!
@bettysteger
Copy link
Author

so i've updated the gist again with my own context!

and i now detect the resource of jim carrey and then print it out.. is this the right way to achieve this?

@lanthaler
Copy link

Good news.. DBpedia has been fixed. So as long as you set the right Accept headers and follow the redirect you get the data directly. You can try it with curl:

curl -LH "Accept: application/ld+json" http://dbpedia.org/resource/Jim_Carrey

I'll leave the rest to @gkellogg as he's the Ruby expert.

@bettysteger
Copy link
Author

oh ok cool! thank you, maybe i'm gonna now try something in javascript ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment