Class: RubySesame::Repository
- Inherits:
-
Object
- Object
- RubySesame::Repository
- Defined in:
- lib/ruby-sesame.rb
Overview
class Server
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#readable ⇒ Object
readonly
Returns the value of attribute readable.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
-
#writable ⇒ Object
readonly
Returns the value of attribute writable.
Class Method Summary collapse
-
.get_parameterize(hash) ⇒ Object
Convert the given hash into a URL paramter string for a GET.
-
.post_parameterize(hash) ⇒ Object
Convert the given hash into an array of strings for a POST.
Instance Method Summary collapse
-
#add!(data, data_format = ) ⇒ Object
Adds new data to the repository.
-
#contexts ⇒ Object
Returns an Array of Strings, where each is the id of a context available on the server.
-
#delete_all_namespaces! ⇒ Object
Deletes all namespaces in the repository.
-
#delete_all_statements! ⇒ Object
Convenience method; deletes all data from the repository.
-
#delete_namespace!(prefix) ⇒ Object
Deletes the namespace with the given prefix.
-
#delete_statements!(options = {}, safety = true) ⇒ Object
Delete one or more statements from the repository.
-
#get_statements(options = {}) ⇒ Object
Returns a list of statements from the repository (i.e. performs the REST GET operation on statements in the repository.).
-
#initialize(server, attrs) ⇒ Repository
constructor
A new instance of Repository.
-
#namespace(prefix) ⇒ Object
Gets the namespace for the given prefix.
-
#namespace!(prefix, namespace) ⇒ Object
Sets the given prefix to the given namespace.
-
#namespaces ⇒ Object
Returns a Hash.
-
#query(query, options = {}) ⇒ Object
The valid result_types depend on what type of query you’re doing: “Relevant values are the MIME types of supported RDF formats for graph queries, the MIME types of supported variable binding formats for tuple queries, and the MIME types of supported boolean result formats for boolean queries.”.
-
#raw_contexts(result_format = "application/sparql-results+json") ⇒ Object
Returns the contexts available in the repository, unprocessed.
-
#raw_namespaces(result_format = "application/sparql-results+json") ⇒ Object
Return the namespaces available in the repository, raw and unprocessed.
-
#size ⇒ Object
Returns the number of statements in the repository.
Constructor Details
#initialize(server, attrs) ⇒ Repository
Returns a new instance of Repository.
103 104 105 106 107 108 109 110 |
# File 'lib/ruby-sesame.rb', line 103 def initialize(server, attrs) @server = server @uri = attrs["uri"]["value"] @id = attrs["id"]["value"] @title = attrs["title"]["value"] @writable = attrs["writable"]["value"] == "true" @readable = attrs["readable"]["value"] == "true" end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
101 102 103 |
# File 'lib/ruby-sesame.rb', line 101 def id @id end |
#readable ⇒ Object (readonly)
Returns the value of attribute readable.
101 102 103 |
# File 'lib/ruby-sesame.rb', line 101 def readable @readable end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
101 102 103 |
# File 'lib/ruby-sesame.rb', line 101 def server @server end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
101 102 103 |
# File 'lib/ruby-sesame.rb', line 101 def title @title end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
101 102 103 |
# File 'lib/ruby-sesame.rb', line 101 def uri @uri end |
#writable ⇒ Object (readonly)
Returns the value of attribute writable.
101 102 103 |
# File 'lib/ruby-sesame.rb', line 101 def writable @writable end |
Class Method Details
.get_parameterize(hash) ⇒ Object
Convert the given hash into a URL paramter string for a GET.
340 341 342 |
# File 'lib/ruby-sesame.rb', line 340 def self.get_parameterize(hash) post_parameterize(hash).join("&") end |
.post_parameterize(hash) ⇒ Object
Convert the given hash into an array of strings for a POST.
332 333 334 335 336 337 |
# File 'lib/ruby-sesame.rb', line 332 def self.post_parameterize(hash) easy = Curl::Easy.new hash.keys.map{|key| easy.escape(key.to_s) + "=" + easy.escape(hash[key]) } end |
Instance Method Details
#add!(data, data_format = ) ⇒ Object
Adds new data to the repository. The data can be an RDF document or a “special purpose transaction document”. I don’t know what the latter is.
313 314 315 316 317 318 319 |
# File 'lib/ruby-sesame.rb', line 313 def add!(data, data_format=DATA_TYPES[:Turtle]) easy = Curl::Easy.new easy.headers["Content-Type"] = data_format easy.url = self.uri + "/statements" easy.http_post(data) end |
#contexts ⇒ Object
Returns an Array of Strings, where each is the id of a context available on the server.
253 254 255 |
# File 'lib/ruby-sesame.rb', line 253 def contexts JSON.parse(raw_contexts())["results"]["bindings"].map{|x| x["contextID"]["value"] } end |
#delete_all_namespaces! ⇒ Object
Deletes all namespaces in the repository.
303 304 305 306 307 |
# File 'lib/ruby-sesame.rb', line 303 def delete_all_namespaces! uri = URI.parse(self.uri + "/namespaces") http = Net::HTTP.start(uri.host, uri.port) http.delete(uri.path) end |
#delete_all_statements! ⇒ Object
Convenience method; deletes all data from the repository.
237 238 239 |
# File 'lib/ruby-sesame.rb', line 237 def delete_all_statements! delete_statements!({}, false) end |
#delete_namespace!(prefix) ⇒ Object
Deletes the namespace with the given prefix.
296 297 298 299 300 |
# File 'lib/ruby-sesame.rb', line 296 def delete_namespace!(prefix) uri = URI.parse(self.uri + "/namespaces/" + URI.escape(prefix)) http = Net::HTTP.start(uri.host, uri.port) http.delete(uri.path) end |
#delete_statements!(options = {}, safety = true) ⇒ Object
Delete one or more statements from the repository. Takes the same arguments as get_statements.
If you do not set one of subj, pred, or obj in your options, it will delete ALL statements from the repository. This is ordinarily not allowed. Set safety=false to delete all statements.
221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/ruby-sesame.rb', line 221 def delete_statements!(={}, safety=true) unless !safety || .keys.select {|x| [:subj, :pred, :obj].include?(x) }.size > 0 raise Exception.new("You asked to delete all statements in the repository. Either give a subj/pred/obj qualifier, or set safety=false") end # We have to use net/http, because curb has not yet implemented DELETE as of this writing. uri = URI.parse(self.uri + "/statements?" + self.class.get_parameterize(.reject{|k,v| ![:subj, :pred, :obj, :context, :infer].include?(k) })) http = Net::HTTP.start(uri.host, uri.port) http.delete(uri.path) end |
#get_statements(options = {}) ⇒ Object
Returns a list of statements from the repository (i.e. performs the REST GET operation on statements in the repository.)
N.B. if unqualified with 1 or more options, this will return all statements in the repository.
Options:
* result_type is the desired MIME type for results (see the DATA_TYPES constant.) Defaults to :Turtle.
* 'subj' (optional): Restricts the GET to statements with the specified N-Triples encoded resource as subject.
* 'pred' (optional): Restricts the GET to statements with the specified N-Triples encoded URI as predicate.
* 'obj' (optional): Restricts the GET to statements with the specified N-Triples encoded value as object.
* 'context' (optional): If specified, restricts the
operation to one or more specific contexts in the
repository. The value of this parameter is either an
N-Triples encoded URI or bnode ID, or the special value
'null' which represents all context-less statements. If
multiple 'context' parameters are specified as an Array, the request
will operate on the union of all specified contexts. The
operation is executed on all statements that are in the
repository if no context is specified.
* 'infer' (optional): Boolean; specifies whether inferred statements
should be included in the result of GET requests. Inferred
statements are included by default.
202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/ruby-sesame.rb', line 202 def get_statements(={}) = {:result_type => DATA_TYPES[:Turtle]}.merge() easy = Curl::Easy.new easy.headers["Accept"] = [:result_type] url = self.uri + "/statements?" + self.class.get_parameterize(.reject{|k,v| ![:subj, :pred, :obj, :context, :infer].include?(k) }) easy.url = url easy.http_get easy.body_str end |
#namespace(prefix) ⇒ Object
Gets the namespace for the given prefix. Returns nil if not found.
280 281 282 283 284 285 286 |
# File 'lib/ruby-sesame.rb', line 280 def namespace(prefix) easy = Curl::Easy.new easy.url = self.uri + "/namespaces/" + easy.escape(prefix) easy.http_get ns = easy.body_str ns =~ /^Undefined prefix:/ ? nil : ns end |
#namespace!(prefix, namespace) ⇒ Object
Sets the given prefix to the given namespace.
289 290 291 292 293 |
# File 'lib/ruby-sesame.rb', line 289 def namespace!(prefix, namespace) uri = URI.parse(self.uri + "/namespaces/" + URI.escape(prefix)) http = Net::HTTP.start(uri.host, uri.port) http.send_request('PUT', uri.path, namespace).body end |
#namespaces ⇒ Object
Returns a Hash. Keys are the prefixes, and the values are the corresponding namespaces.
269 270 271 272 273 274 275 276 |
# File 'lib/ruby-sesame.rb', line 269 def namespaces ns = {} JSON.parse(raw_namespaces)["results"]["bindings"].each {|x| ns[x["prefix"]["value"]] = x["namespace"]["value"] } ns end |
#query(query, options = {}) ⇒ Object
The valid result_types depend on what type of query you’re doing: “Relevant values are the MIME types of supported RDF formats for graph queries, the MIME types of supported variable binding formats for tuple queries, and the MIME types of supported boolean result formats for boolean queries.”
Options:
-
:result_type - from DATA_TYPES
-
:method - :get or :post
-
:query_language - “sparql”, “serql”, or any other query language your Sesame server supports.
-
:infer => true or false. Defaults to true (serverside) if not specified.
-
:variable_bindings - if given, should be a Hash. If present, it will
be used to bind variables outside the actual query. Keys are variable names and values are N-Triples encoded RDF values.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/ruby-sesame.rb', line 129 def query(query, ={}) = { :result_type => DATA_TYPES[:JSON], :method => :get, :query_language => "sparql", }.merge() easy = Curl::Easy.new easy.headers["Accept"] = [:result_type] if [:method] == :get easy.url = (self.uri + "?" + "query=#{ easy.escape(query) }&"+ "queryLn=#{ easy.escape([:query_language]) }&" + (![:infer] ? "infer=false&" : "" ) + if [:variable_bindings] [:variable_bindings].keys.map {|name| "$<#{ easy.escape(name) }>=#{ easy.escape([:variable_bindings][name]) }" }.join("&") else "" end ).chop easy.http_get else # POST. easy.url = self.uri fields = ["query=#{ easy.escape(query) }", "queryLn=#{ easy.escape([:query_language]) }" ] fields.push("infer=false") unless [:infer] [:variable_bindings].keys.map {|name| field.push("$<#{ easy.escape(name) }>=#{ easy.escape([:variable_bindings][name]) }") } if [:variable_bindings] easy.http_post(fields) end easy.body_str end |
#raw_contexts(result_format = "application/sparql-results+json") ⇒ Object
Returns the contexts available in the repository, unprocessed. Results are in JSON by default, though XML and binary are also available.
243 244 245 246 247 248 249 250 |
# File 'lib/ruby-sesame.rb', line 243 def raw_contexts(result_format="application/sparql-results+json") easy = Curl::Easy.new easy.headers["Accept"] = result_format easy.url = self.uri + "/contexts" easy.http_get easy.body_str end |
#raw_namespaces(result_format = "application/sparql-results+json") ⇒ Object
Return the namespaces available in the repository, raw and unprocessed. Results are in JSON by default, though XML and binary are also available.
259 260 261 262 263 264 265 266 |
# File 'lib/ruby-sesame.rb', line 259 def raw_namespaces(result_format="application/sparql-results+json") easy = Curl::Easy.new easy.headers["Accept"] = result_format easy.url = self.uri + "/namespaces" easy.http_get easy.body_str end |
#size ⇒ Object
Returns the number of statements in the repository.
323 324 325 326 327 328 |
# File 'lib/ruby-sesame.rb', line 323 def size easy = Curl::Easy.new easy.url = self.uri + "/size" easy.http_get easy.body_str.to_i end |