Class: Sasquatch::Store

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/sasquatch/store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storename, options = {}) ⇒ Store

Returns a new instance of Store.



7
8
9
10
11
12
13
# File 'lib/sasquatch/store.rb', line 7

def initialize(storename, options={})
  @store_name = storename
  if options[:username]
    set_credentials(options[:username], options[:password])
  end

end

Instance Attribute Details

#last_responseObject (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_clientsObject (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_nameObject (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_statusObject



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 = "/#{@store_name}/config/access-status"
  @last_response = get(path, {:digest_auth=>@auth})
  graph = parse_json(@last_response.body)      
  self.class.headers 'Accept' => accept      
  graph
end

#augment(uri, pattern = :cbd) ⇒ Object



37
38
39
40
41
# File 'lib/sasquatch/store.rb', line 37

def augment(uri, pattern=:cbd)
  graph = augment_multi([uri], pattern)
  graph.set_requested_resource(uri)
  graph
end

#augment_multi(uris, pattern = :cbd) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sasquatch/store.rb', line 43

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 ")}  }"
  options = {:body=>{:query=>sparql, :output=>"ntriples"}, :digest_auth=>@auth}
  @last_response = post("/#{@store_name}/services/sparql", options)
  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



20
21
22
23
24
25
26
# File 'lib/sasquatch/store.rb', line 20

def describe(uri)
  options = {:query=>{:about=>uri, :output=>"ntriples"}, :digest_auth=>@auth}
  @last_response = get("/#{@store_name}/meta", options)
  graph = parse_ntriples(@last_response.body)
  graph.set_requested_resource(uri)
  graph
end

#describe_multi(uris) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/sasquatch/store.rb', line 28

def describe_multi(uris)
  sparql = "DESCRIBE "
  uris.each {|uri| sparql << "<#{uri}> "}
  options = {:query=>{:query=>sparql, :output=>"ntriples"}, :digest_auth=>@auth}
  @last_response = get("/#{@store_name}/services/sparql", options)
  graph = parse_ntriples(@last_response.body)
  graph      
end

#get(path, options) ⇒ Object



59
60
61
# File 'lib/sasquatch/store.rb', line 59

def get(path, options)
  self.class.get(path, options)      
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



63
64
65
# File 'lib/sasquatch/store.rb', line 63

def post(path, options)
  self.class.post(path, options)
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

Returns:

  • (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

Raises:

  • (ArgumentError)


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



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/sasquatch/store.rb', line 67

def save(graph_statement_or_resource, graph_name=nil)
  path = "/#{@store_name}/meta"
  path << "/graphs/#{graph_name}" if graph_name
  options = {:headers=>{"Content-Type"=> "text/turtle"}, :body=>graph_statement_or_resource.to_ntriples, :digest_auth=>@auth}
  @last_response = post(path, options )      
  if @last_response.response.code == "204"
    true
  else
    false
  end
end

#search(query, options = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sasquatch/store.rb', line 79

def search(query, options={})
  accept = self.class.headers['Accept']
  self.class.headers 'Accept' => 'application/json'
  path = "/#{@store_name}/items"
  opts = {:query=>options}
  opts[:query][:query] = query
  opts[:digest_auth] = @auth
  @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 = "/#{@store_name}/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
    
  options = {:headers=>{"Content-Type"=> "application/vnd.talis.changeset+turtle"}, :body=>graph.to_ntriples, :digest_auth=>@auth}
  @last_response = post(path, options )      
  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



15
16
17
18
# File 'lib/sasquatch/store.rb', line 15

def set_credentials(username, password)
  @auth = {:username => username, :password => password}
  @auth[:headers] = self.get("/#{@store_name}/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 = "/#{@store_name}/services/sparql"
  unless graph == :default
    path << "/graphs/#{graph}"
  end
  options = {:query=>{:query=>query, :output=>'ntriples'}, :digest_auth=>@auth}
  @last_response = get(path, options)
  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 = "/#{@store_name}/services/sparql"
  unless graph == :default
    path << "/graphs/#{graph}"
  end
  options = {:query=>{:query=>query, :output=>'json'}, :digest_auth=>@auth}
  @last_response = get(path, options)
  SPARQL::Client.parse_json_bindings(@last_response.body) || false
end

#status_messageObject



264
265
266
267
268
269
# File 'lib/sasquatch/store.rb', line 264

def status_message
  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