Last active
December 15, 2015 17:49
-
-
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?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
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
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:
I'll leave the rest to @gkellogg as he's the Ruby expert.