Class: RDF::Repository
- Inherits:
-
Object
- Object
- RDF::Repository
- Includes:
- MonitorMixin, Parser
- Defined in:
- lib/lightrdf/repository.rb
Instance Method Summary collapse
-
#contexts ⇒ Object
Gets the list of the sesame contexts.
-
#data(*contexts) ⇒ Object
Extracts the data in sesame from the indicated repositories.
-
#data=(*args) ⇒ Object
Adds data to sesame without removing the previous data.
-
#initialize(options = {}) ⇒ Repository
constructor
A new instance of Repository.
-
#query(string) ⇒ Object
Performs a query based on turtle syntax and assuming ?q as variable.
-
#sparql(query) ⇒ Object
Performs a SPARQL query.
Methods included from Parser
parse, #serialize, #to_ntriples
Constructor Details
#initialize(options = {}) ⇒ Repository
Returns a new instance of Repository.
6 7 8 9 10 11 |
# File 'lib/lightrdf/repository.rb', line 6 def initialize ={} super() # Assigns the default value to the options. @options = {:host=>"http://localhost", :port=>8080, :repository=>"memory", :format=>:ntriples}.merge() end |
Instance Method Details
#contexts ⇒ Object
Gets the list of the sesame contexts
30 31 32 33 34 35 36 37 38 |
# File 'lib/lightrdf/repository.rb', line 30 def contexts synchronize do # The URL to get the context list from sesame url = "#{repository_url(@options[:repository])}/contexts" # Asks for the context list and parses it Nokogiri::XML( RestClient.get(url, :content_type=>'application/sparql-results+xml') ).search(:uri).map(&:text) end end |
#data(*contexts) ⇒ Object
Extracts the data in sesame from the indicated repositories
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/lightrdf/repository.rb', line 14 def data *contexts synchronize do params = ("?" + contexts.flatten.map{|context| "context=%3C#{CGI::escape(context)}%3E"}*"&&" if !contexts.empty?) # Prepares the URL to request the data url = "#{repository_statements_url(@options[:repository])}#{params}" # Asks for the data ntriples = RestClient.get url, :content_type=>content_type # Builds the graph Parser.parse :rdf, ntriples end end |
#data=(*args) ⇒ Object
Adds data to sesame without removing the previous data
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/lightrdf/repository.rb', line 41 def data= *args synchronize do # Retrieve arguments graph, context = if args.first.is_a?(Array) [args.first.first, args.first.last] else [args.first, nil] end # Prepares the dir to connect with the repository url = "#{repository_statements_url(@options[:repository])}" url += "?context=%3C#{CGI::escape(context)}%3E" if context data = graph.serialize :ntriples # Adds the data to Sesame RestClient.post url, data, :content_type=>content_type end end |
#query(string) ⇒ Object
Performs a query based on turtle syntax and assuming ?q as variable
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/lightrdf/repository.rb', line 61 def query string query = "" RDF::ID.ns.each do |prefix, uri| query += "PREFIX #{prefix}: <#{uri}>\n" end query += "SELECT ?q\n" query += "WHERE {#{string}}" results = sparql(query) results.children.first.children[3].children.select { |result| !result.text? }.map do |result| node = result.children[1].children[1] case node.name.to_sym when :uri then Node(node.content) when :bnode then Node("_:#{node.content}") when :literal then node.content end end end |
#sparql(query) ⇒ Object
Performs a SPARQL query
84 85 86 87 88 89 |
# File 'lib/lightrdf/repository.rb', line 84 def sparql query synchronize do url = "#{repository_url(@options[:repository])}?query=#{CGI::escape(query)}" Nokogiri::XML( RestClient.get(url, :content_type=>'application/sparql-results+xml') ) end end |