Class: SPARQL::Server::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/sparql-server.rb

Defined Under Namespace

Classes: BadRequest, Item

Class Method Summary collapse

Class Method Details

.find(string, schema) ⇒ Object

-


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sparql-server.rb', line 29

def find(string, schema)
  items = {}
  reader = RDF::Reader.for(:sparql).new(string)

  unless reader.type == :describe
    raise BadRequest, 'Only `DESCRIBE` queries are allowed'
  end

  reader.triples.flatten.uniq.each do |item|
    items[item] = Item.new(item, schema)
  end

  reader.each_statement do |statement|
    triple = statement.to_triple.map { |item| items[item] }
    triple.each { |item| item << triple }
  end

  reader.targets.each do |target|
    items[target] ||= Item.new(target, schema)
  end

  reader.targets.map do |target|
    items[target].instances.to_a
  end.flatten
end

.to_xml(query, schema, options = {}) ⇒ Object

-


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sparql-server.rb', line 9

def to_xml(query, schema, options = {})
  options[:namespaces] ||= {}
  RDF::Writer.for(:xml).buffer do |writer|
    options[:namespaces].each do |ns, uri|
      writer.namespace!(uri, ns)
    end
    if options[:base]
      writer.base!(options[:base])
    end
    find(query, schema).map do |item|
      item.to_triples.each do |triple|
        writer << triple
      end
    end
  end
end