Class: Sasquatch::Store
- Inherits:
-
Object
- Object
- Sasquatch::Store
- Includes:
- HTTParty
- Defined in:
- lib/sasquatch/store.rb
Instance Attribute Summary collapse
-
#last_response ⇒ Object
readonly
Returns the value of attribute last_response.
-
#sparql_clients ⇒ Object
readonly
Returns the value of attribute sparql_clients.
-
#store_name ⇒ Object
readonly
Returns the value of attribute store_name.
Instance Method Summary collapse
- #access_status ⇒ Object
- #augment(uri, pattern = :cbd) ⇒ Object
- #augment_multi(uris, pattern = :cbd) ⇒ Object
- #delete_uri(uri, versioned = false, creator = "sasquatch.rb") ⇒ Object
- #delete_uris(uris, versioned = false, creator = "sasquatch.rb") ⇒ Object
- #describe(uri) ⇒ Object
- #describe_multi(uris) ⇒ Object
- #get(path, options) ⇒ Object
-
#initialize(storename, options = {}) ⇒ Store
constructor
A new instance of Store.
- #parse_json(body) ⇒ Object
- #parse_ntriples(body) ⇒ Object
- #parse_rss10(body) ⇒ Object
- #post(path, options) ⇒ Object
- #read_graph(data, format) ⇒ Object
- #read_only? ⇒ Boolean
- #remove_triple(stmt, versioned = false) ⇒ Object
- #remove_triples(stmts, versioned = false) ⇒ Object
- #replace(graph_statements_or_resource, versioned = false, creator = "sasquatch.rb") ⇒ Object
- #replace_triple(old_stmt, new_stmt, versioned = false) ⇒ Object
-
#replace_triples(changes, versioned = false, creator = "sasquatch.rb") ⇒ Object
takes a Hash in form of old_statement=>replacement_statement.
- #save(graph_statement_or_resource, graph_name = nil) ⇒ Object
- #search(query, options = {}) ⇒ Object
- #send_changeset(graph, versioned = false, creator = "sasquatch.rb") ⇒ Object
- #set_credentials(username, password) ⇒ Object
- #sparql(*variables) ⇒ Object
- #sparql_describe(query, graph = :default) ⇒ Object (also: #sparql_construct)
- #sparql_select(query, graph = :default) ⇒ Object (also: #sparql_ask)
- #status_message ⇒ Object
Constructor Details
#initialize(storename, options = {}) ⇒ Store
Returns a new instance of Store.
7 8 9 10 11 12 13 14 |
# File 'lib/sasquatch/store.rb', line 7 def initialize(storename, ={}) self.class.base_uri "http://api.talis.com/stores/#{storename}" @store_name = storename if [:username] set_credentials([:username], [:password]) end end |
Instance Attribute Details
#last_response ⇒ Object (readonly)
Returns the value of attribute last_response.
6 7 8 |
# File 'lib/sasquatch/store.rb', line 6 def last_response @last_response end |
#sparql_clients ⇒ Object (readonly)
Returns the value of attribute sparql_clients.
6 7 8 |
# File 'lib/sasquatch/store.rb', line 6 def sparql_clients @sparql_clients end |
#store_name ⇒ Object (readonly)
Returns the value of attribute store_name.
6 7 8 |
# File 'lib/sasquatch/store.rb', line 6 def store_name @store_name end |
Instance Method Details
#access_status ⇒ Object
247 248 249 250 251 252 253 254 255 |
# File 'lib/sasquatch/store.rb', line 247 def access_status accept = self.class.headers['Accept'] self.class.headers 'Accept' => 'application/json' path = "/config/access-status" @last_response = get(path, {}) graph = parse_json(@last_response.body) self.class.headers 'Accept' => accept graph end |
#augment(uri, pattern = :cbd) ⇒ Object
38 39 40 41 42 |
# File 'lib/sasquatch/store.rb', line 38 def augment(uri, pattern=:cbd) graph = augment_multi([uri], pattern) graph.set_requested_resource(uri) graph end |
#augment_multi(uris, pattern = :cbd) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/sasquatch/store.rb', line 44 def augment_multi(uris, pattern=:cbd) sparql = "DESCRIBE ?o " i = 1 where = [] uris.each do |uri| sparql << "<#{uri}> " where << "{<#{uri}> ?p ?o . }" i += 1 end sparql << "\nWHERE\n{ #{where.join(" UNION ")} }" = {:body=>{:query=>sparql, :output=>"ntriples"}} @last_response = post("/services/sparql", ) graph = parse_ntriples(@last_response.body) graph end |
#delete_uri(uri, versioned = false, creator = "sasquatch.rb") ⇒ Object
196 197 198 |
# File 'lib/sasquatch/store.rb', line 196 def delete_uri(uri, versioned=false, creator="sasquatch.rb") delete_uris([uri], versioned, creator) end |
#delete_uris(uris, versioned = false, creator = "sasquatch.rb") ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/sasquatch/store.rb', line 200 def delete_uris(uris, versioned=false, creator="sasquatch.rb") current_graph = describe_multi(uris) changesets = [] uris.each do |uri| u = RDF::URI.intern(uri) cs = Changeset.new(u) cs.remove_statements(current_graph.query(:subject=>u)) changesets << cs end graph = RDF::Graph.new changesets.each do |changeset| changeset.statements.each do |stmt| graph << stmt end end send_changeset(graph, versioned, creator) end |
#describe(uri) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/sasquatch/store.rb', line 21 def describe(uri) = {:query=>{:about=>uri, :output=>"ntriples"}} @last_response = get("/meta", ) graph = parse_ntriples(@last_response.body) graph.set_requested_resource(uri) graph end |
#describe_multi(uris) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/sasquatch/store.rb', line 29 def describe_multi(uris) sparql = "DESCRIBE " uris.each {|uri| sparql << "<#{uri}> "} = {:query=>{:query=>sparql, :output=>"ntriples"}} @last_response = get("/services/sparql", ) graph = parse_ntriples(@last_response.body) graph end |
#get(path, options) ⇒ Object
60 61 62 |
# File 'lib/sasquatch/store.rb', line 60 def get(path, ) self.class.get(path, ) end |
#parse_json(body) ⇒ Object
275 276 277 |
# File 'lib/sasquatch/store.rb', line 275 def parse_json(body) read_graph(body, :json) end |
#parse_ntriples(body) ⇒ Object
271 272 273 |
# File 'lib/sasquatch/store.rb', line 271 def parse_ntriples(body) read_graph(body, :ntriples) end |
#parse_rss10(body) ⇒ Object
279 280 281 |
# File 'lib/sasquatch/store.rb', line 279 def parse_rss10(body) read_graph(body, :rss10) end |
#post(path, options) ⇒ Object
64 65 66 |
# File 'lib/sasquatch/store.rb', line 64 def post(path, ) self.class.post(path, ) end |
#read_graph(data, format) ⇒ Object
283 284 285 286 287 288 289 290 291 |
# File 'lib/sasquatch/store.rb', line 283 def read_graph(data, format) graph = RDF::Graph.new RDF::Reader.for(format).new(data) do |reader| reader.each_statement do |statement| graph << statement end end graph end |
#read_only? ⇒ Boolean
257 258 259 260 261 262 |
# File 'lib/sasquatch/store.rb', line 257 def read_only? access_status.query(:predicate=>RDF::URI.intern('http://schemas.talis.com/2006/bigfoot/configuration#accessMode')).each do |stmt| return true if stmt.object == RDF::URI.intern("http://schemas.talis.com/2006/bigfoot/statuses#read-only") end return false end |
#remove_triple(stmt, versioned = false) ⇒ Object
93 94 95 |
# File 'lib/sasquatch/store.rb', line 93 def remove_triple(stmt, versioned=false) remove_triples([stmt], versioned, creator) end |
#remove_triples(stmts, versioned = false) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/sasquatch/store.rb', line 97 def remove_triples(stmts, versioned=false) changesets = {} stmts.each do |stmt| changesets[stmt.subject] ||= Changeset.new(stmt.subject) changesets[stmt.subject].remove_statements(stmt) end graph = RDF::Graph.new changesets.each_pair do |uri, changeset| changeset.statements.each do |stmt| graph << stmt end end send_changeset(graph, versioned, creator) end |
#replace(graph_statements_or_resource, versioned = false, creator = "sasquatch.rb") ⇒ Object
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 174 175 |
# File 'lib/sasquatch/store.rb', line 136 def replace(graph_statements_or_resource, versioned=false, creator="sasquatch.rb") uris = case graph_statements_or_resource.class.name when "RDF::Graph" subjects = [] graph_statements_or_resource.each_subject {|s| subjects << s} subjects when "Array" subjects = [] graph_statements_or_resource.each_subject {|s| subjects << s} subjects.uniq else # This should only work for rdf-rdfobjects Resources if graph_statements_or_resource.respond_to?(:predicates) [graph_statements_or_resource.to_s] end end raise ArgumentError unless uris current_graph = describe_multi(uris) changesets = {} current_graph.each_statement.each do |stmt| changesets[stmt.subject] ||= Changeset.new(stmt.subject) changesets[stmt.subject].remove_statements(stmt) end replacements = case graph_statements_or_resource.class.name when "Array" then graph_statements_or_resource else graph_statements_or_resource.statements end replacements.each do |stmt| changesets[stmt.subject] ||= Changeset.new(stmt.subject) changesets[stmt.subject].add_statements(stmt) end graph = RDF::Graph.new changesets.each do |uri, changeset| changeset.statements.each do |stmt| graph << stmt end end send_changeset(graph, versioned, creator) end |
#replace_triple(old_stmt, new_stmt, versioned = false) ⇒ Object
112 113 114 |
# File 'lib/sasquatch/store.rb', line 112 def replace_triple(old_stmt, new_stmt, versioned=false) replace_triples({old_triple=>new_triple}, versioned, creator) end |
#replace_triples(changes, versioned = false, creator = "sasquatch.rb") ⇒ Object
takes a Hash in form of old_statement=>replacement_statement
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/sasquatch/store.rb', line 119 def replace_triples(changes, versioned=false, creator="sasquatch.rb") changesets = {} changes.each_pair do |old_stmt, new_stmt| changesets[old_stmt.subject] ||= Changeset.new(old_stmt.subject) changesets[old_stmt.subject].remove_statements(*old_stmt) changesets[new_stmt.subject] ||= Changeset.new(new_stmt.subject) changesets[new_stmt.subject].remove_statements(*new_stmt) end graph = RDF::Graph.new changesets.each_pair do |uri, changeset| changeset.statements.each do |stmt| graph << stmt end end send_changeset(graph, versioned, creator) end |
#save(graph_statement_or_resource, graph_name = nil) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/sasquatch/store.rb', line 68 def save(graph_statement_or_resource, graph_name=nil) path = "/meta" path << "/graphs/#{graph_name}" if graph_name = {:headers=>{"Content-Type"=> "text/turtle"}, :body=>graph_statement_or_resource.to_ntriples, :digest_auth=>@auth} @last_response = post(path, ) if @last_response.response.code == "204" true else false end end |
#search(query, options = {}) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/sasquatch/store.rb', line 80 def search(query, ={}) accept = self.class.headers['Accept'] self.class.headers 'Accept' => 'application/json' path = "/items" opts = {:query=>} opts[:query][:query] = query @last_response = get(path, opts) #graph = parse_rss10(@last_response.body) graph = parse_json(@last_response.body) self.class.headers 'Accept' => accept || "text/plain" SearchResult.new_from_query(graph, opts, self) end |
#send_changeset(graph, versioned = false, creator = "sasquatch.rb") ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/sasquatch/store.rb', line 177 def send_changeset(graph, versioned=false, creator="sasquatch.rb") path = "/meta" path << "/changesets" if versioned graph.query(:predicate=>RDF.type, :object=>RDF::Talis::Changeset.ChangeSet).each_subject do |cs| graph << [cs, RDF::Talis::Changeset.creatorName, creator] end = {:headers=>{"Content-Type"=> "application/vnd.talis.changeset+turtle"}, :body=>graph.to_ntriples, :digest_auth=>@auth} @last_response = post(path, ) if !versioned && @last_response.response.code =~ /^20[0-9]$/ true elsif versioned && @last_response.response.code =~ /20[012]/ true else false end end |
#set_credentials(username, password) ⇒ Object
16 17 18 19 |
# File 'lib/sasquatch/store.rb', line 16 def set_credentials(username, password) @auth = {:username => username, :password => password} @auth[:headers] = self.get("/snapshots", {}).headers['www-authenticate'] end |
#sparql(*variables) ⇒ Object
218 219 220 |
# File 'lib/sasquatch/store.rb', line 218 def sparql(*variables) SparqlBuilder.init(self,variables) end |
#sparql_describe(query, graph = :default) ⇒ Object Also known as: sparql_construct
222 223 224 225 226 227 228 229 230 231 |
# File 'lib/sasquatch/store.rb', line 222 def sparql_describe(query, graph=:default) path = "/services/sparql" unless graph == :default path << "/graphs/#{graph}" end = {:query=>{:query=>query, :output=>'ntriples'}} @last_response = get(path, ) graph = parse_ntriples(@last_response.body) graph end |
#sparql_select(query, graph = :default) ⇒ Object Also known as: sparql_ask
235 236 237 238 239 240 241 242 243 |
# File 'lib/sasquatch/store.rb', line 235 def sparql_select(query, graph=:default) path = "/services/sparql" unless graph == :default path << "/graphs/#{graph}" end = {:query=>{:query=>query, :output=>'json'}} @last_response = get(path, ) SPARQL::Client.parse_json_bindings(@last_response.body) || false end |
#status_message ⇒ Object
264 265 266 267 268 269 |
# File 'lib/sasquatch/store.rb', line 264 def access_status.query(:predicate=>RDF::URI.intern('http://schemas.talis.com/2006/bigfoot/configuration#statusMessage')).each do |stmt| return stmt.object.value end nil end |