Module: SPARQL::Results

Included in:
RDF::Query::Solutions
Defined in:
vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/results.rb

Overview

Generate SPARQL Results as Boolean, XML or JSON

This module is a mixin for RDF::Query::Solutions

Constant Summary

MIME_TYPES =
{
  json: 'application/sparql-results+json',
  html: 'text/html',
  :xml  => 'application/sparql-results+xml',
  csv: 'text/csv',
  tsv: 'text/tab-separated-values'
}

Instance Method Summary collapse

Instance Method Details

#to_csvString

Generate Solutions as CSV



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/results.rb', line 122

def to_csv
  require 'csv' unless defined?(::CSV)
  bnode_map = {}
  bnode_gen = "_:a"
  CSV.generate(row_sep: "\r\n") do |csv|
    csv << variable_names.to_a
    self.each do |solution|
      csv << variable_names.map do |n|
        case term = solution[n]
        when RDF::Node then bnode_map[term] ||=
          begin
            this = bnode_gen
            bnode_gen = bnode_gen.succ
            this
          end
        else
          solution[n].to_s
        end
      end
    end
  end
end

#to_htmlString

Generate Solutions as HTML



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/results.rb', line 96

def to_html
  require 'builder' unless defined?(::Builder)
  
  xml = ::Builder::XmlMarkup.new(indent: 2)
  xml.table(class: "sparql") do
    xml.tbody do
      xml.tr do
        variable_names.each do |n|
          xml.th(n.to_s)
        end
      end
      self.each do |solution|
        xml.tr do
          variable_names.each do |n|
            xml.td(RDF::NTriples.serialize(solution[n]))
          end
        end
      end
    end
  end
end

#to_jsonString

Generate Solutions as JSON



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
44
45
46
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/results.rb', line 18

def to_json
  require 'json' unless defined?(::JSON)
  
  bindings = self.map do |solution|
    variable_names.inject({}) do |memo, n|
      memo.merge case s = solution[n]
      when RDF::URI
        {n => {type: "uri", value: s.to_s }}
      when RDF::Node
        {n => {type: "bnode", value: s.id }}
      when RDF::Literal
        if s.datatype?
          {n => {type: "typed-literal", datatype: s.datatype.to_s, value: s.to_s }}
        elsif s.language
          {n => {type: "literal", "xml:lang" => s.language.to_s, value: s.to_s }}
        else
          {n => {type: "literal", value: s.to_s }}
        end
      else
        {}
      end
    end
  end

  {
    :head     => { vars: variable_names },
    :results  => { bindings: bindings}
  }.to_json
end

#to_tsvString

Generate Solutions as TSV



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/results.rb', line 148

def to_tsv
  require 'csv' unless defined?(::CSV)
  bnode_map = {}
  bnode_gen = "_:a"
  results = [
    variable_names.map {|v| "?#{v}"}.join("\t")
  ] + self.map do |solution|
    variable_names.map do |n|
      case term = solution[n]
      when RDF::Literal::Integer, RDF::Literal::Decimal, RDF::Literal::Double
        term.canonicalize.to_s
      when nil
        ""
      else
        RDF::NTriples.serialize(term)
      end
    end.join("\t")
  end
  results.join("\n") + "\n"
end

#to_xmlString

Generate Solutions as XML



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/results.rb', line 52

def to_xml
  require 'builder' unless defined?(::Builder)
  
  xml = ::Builder::XmlMarkup.new(indent: 2)
  xml.instruct!
  xml.sparql(xmlns: "http://www.w3.org/2005/sparql-results#") do
    xml.head do
      variable_names.each do |n|
        xml.variable(name: n)
      end
    end
    xml.results do
      self.each do |solution|
        xml.result do
          variable_names.each do |n|
            s = solution[n]
            next unless s
            xml.binding(name: n) do
              case s
              when RDF::URI
                xml.uri(s.to_s)
              when RDF::Node
                xml.bnode(s.id)
              when RDF::Literal
                if s.datatype?
                  xml.literal(s.to_s, "datatype" => s.datatype.to_s)
                elsif s.language
                  xml.literal(s.to_s, "xml:lang" => s.language.to_s)
                else
                  xml.literal(s.to_s)
                end
              end
            end
          end
        end
      end
    end
  end
end