Class: Solis::Query::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/solis/query/run.rb

Class Method Summary collapse

Class Method Details

.run(entity, query) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/solis/query/run.rb', line 5

def self.run(entity, query)
  result = {}
  context = JSON.parse %(
{
"@context": {
    "@vocab": "#{Solis::Options.instance.get[:graph_name]}",
    "id": "@id"
},
"@type": "#{entity}",
"@embed": "@always"
}
   )

  c = Solis::Store::Sparql::Client.new(Solis::Options.instance.get[:sparql_endpoint], Solis::Options.instance.get[:graph_name])
  r = c.query(query)
  if r.is_a?(SPARQL::Client)
    g = RDF::Graph.new
    t = r.query('select * where{?s ?p ?o}')
    t.each do |s|
      g << [s.s, s.p, s.o]
    end

    framed = nil
    JSON::LD::API.fromRDF(g) do |e|
      framed = JSON::LD::API.frame(e, context)
    end
    result = sanitize_result(framed)
  else
    t = []
    r.each do |s|
      t << s.to_h
    end
    result = sanitize_result({'@graph' => t})
  end
  result
rescue StandardError => e
  puts e.message
  raise e
end