Created
February 14, 2013 14:47
-
-
Save tsunagun/4953262 to your computer and use it in GitHub Desktop.
RDF SesameにRDFファイルをPostする
This file contains hidden or 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
# coding: UTF-8 | |
# | |
# openRDF SesameにRDFファイルをHTTP Postするスクリプト | |
# ruby post2sesame.rb sample_file.rdf http://purl.org/net/mdlab/context_example | |
require 'net/http' | |
require 'cgi' | |
require 'uri' | |
# SesameのリポジトリID | |
repository_id = "example" | |
# Sesameに登録したいRDFファイル | |
datafile = ARGV[0] | |
# RDFファイル読み込む | |
data = open(datafile).read | |
# Contextの指定 | |
context = ARGV[1] || nil | |
# ヘッダ指定.ファイル形式にあわせて適宜変更 | |
# 本当は自動判別にした方が便利 | |
# 参照:http://www.openrdf.org/doc/sesame2/system/ch08.html#d0e764 | |
header = { | |
'Content-Type' => 'application/rdf+xml;charset=UTF-8' | |
} | |
if context.nil? | |
uri = URI.parse("http://localhost:8080/openrdf-sesame/repositories/#{repository_id}/statements") | |
client = Net::HTTP.new(uri.host, uri.port) | |
res = client.post(uri.path, data, header) | |
else | |
uri = URI.parse("http://localhost:8080/openrdf-sesame/repositories/#{repository_id}/rdf-graphs/service") | |
client = Net::HTTP.new(uri.host, uri.port) | |
res = client.post(uri.path+"?graph=#{CGI.escape(context)}", data, header) | |
end | |
p res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment