Module: SPARQL

Defined in:
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/rdf-n3-8fd20193c115/lib/rdf/n3/extensions.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/grammar.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/results.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/version.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/query.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/update.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/version.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/aggregate.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/expression.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/evaluatable.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/if.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/in.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/or.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/tz.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/abs.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/add.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/alt.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/and.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/asc.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/ask.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/avg.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/bgp.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/day.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/iri.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/max.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/md5.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/min.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/not.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/now.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/seq.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/str.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/sum.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/base.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/ceil.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/copy.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/desc.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/drop.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/join.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/lang.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/load.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/move.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/path.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/plus.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/rand.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/sha1.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/uuid.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/with.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/year.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/bnode.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/bound.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/clear.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/count.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/equal.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/floor.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/graph.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/group.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/hours.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/lcase.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/minus.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/month.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/notin.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/order.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/regex.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/round.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/slice.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/strdt.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/table.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/ucase.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/union.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/using.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/concat.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/create.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/delete.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/divide.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/exists.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/extend.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/filter.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/insert.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/is_iri.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/modify.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/negate.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/object.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/prefix.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/sample.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/sha256.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/sha384.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/sha512.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/strlen.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/substr.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/triple.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/update.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/compare.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/dataset.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/minutes.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/project.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/reduced.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/replace.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/reverse.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/seconds.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/strends.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/strlang.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/struuid.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/subject.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/coalesce.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/contains.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/datatype.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/describe.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/distinct.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/exprlist.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/is_blank.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/multiply.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/notoneof.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/path_opt.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/sequence.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/strafter.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/subtract.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/timezone.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/construct.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/is_triple.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/left_join.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/less_than.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/not_equal.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/notexists.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/path_plus.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/path_star.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/predicate.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/same_term.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/strbefore.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/strstarts.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/is_literal.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/is_numeric.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/delete_data.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/insert_data.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/delete_where.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/greater_than.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/group_concat.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/lang_matches.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/encode_for_uri.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/less_than_or_equal.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/algebra/operator/greater_than_or_equal.rb,
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-client-39558059b9a6/lib/sparql/client.rb

Overview

A SPARQL for RDF.rb.

Defined Under Namespace

Modules: Algebra, Grammar, Results, VERSION Classes: Client, MalformedQuery, QueryRequestRefused

Constant Summary collapse

ERROR_MESSAGE =
%q(<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>SPARQL Processing Service: %s</title>
  </head>
  <body>
    <p>%s: %s</p>
  </body>
</html>
).freeze

Class Method Summary collapse

Class Method Details

.execute(query, queryable, **options) {|solution| ... } ⇒ RDF::Graph, ...

Parse and execute the given SPARQL query string against queriable.

Requires a queryable object (such as an RDF::Repository), into which the dataset will be loaded.

Optionally takes a list of URIs to load as default or named graphs into queryable.

Note, if default or named graphs are specified as options (protocol elements), or the query references specific default or named graphs the graphs are either presumed to be existant in queryable or are loaded into queryable depending on the presense and value of the :load_datasets option.

Attempting to load into an immutable queryable will result in a TypeError.

Examples:

repository = RDF::Repository.new
results = SPARQL.execute("SELECT * WHERE { ?s ?p ?o }", repository)

Parameters:

Options Hash (**options):

Yields:

  • (solution)

    each matching solution, statement or boolean

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Returns:

  • (RDF::Graph, Boolean, RDF::Query::Solutions::Enumerator)

    Note, results may be used with serialize_results to obtain appropriate output encoding.

Raises:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql.rb', line 73

def self.execute(query, queryable, **options, &block)
  query = self.parse(query, **options)
  query = query.optimize(**options) if options[:optimize]
  queryable = queryable || RDF::Repository.new

  if options[:logger]
    options[:logger].debug("SPARQL.execute") {SXP::Generator.string query.to_sxp_bin}
  end

  if options.has_key?(:load_datasets)
    queryable = queryable.class.new
    [options[:default_graph_uri]].flatten.each do |uri|
      queryable.load(uri)
    end
    [options[:named_graph_uri]].flatten.each do |uri|
      queryable.load(uri, graph_name: uri)
    end
  end
  query.execute(queryable, &block)
rescue SPARQL::Grammar::Parser::Error => e
  raise MalformedQuery, e.message
rescue TypeError => e
  raise QueryRequestRefused, e.message
end

.first_content_type(acceptable, available) ⇒ String

Find a content_type from a list using an ordered list of acceptable content types using wildcard matching

Parameters:

Returns:

See Also:



311
312
313
314
315
316
317
318
319
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/results.rb', line 311

def first_content_type(acceptable, available)
  return acceptable.first if available.empty?
  available.flatten!
  acceptable.each do |pattern|
    type = available.detect { |t| File.fnmatch(pattern, t) }
    return type if type
  end
  nil
end

.parse(query, **options) ⇒ SPARQL::Query

Parse the given SPARQL query string.

Examples:

query = SPARQL.parse("SELECT * WHERE { ?s ?p ?o }")

Parameters:

Options Hash (**options):

  • :update (Boolean) — default: false

    Parse starting with UpdateUnit production, QueryUnit otherwise.

Returns:

  • (SPARQL::Query)

    The resulting query may be executed against a queryable object such as an RDF::Graph or RDF::Repository.

Raises:

  • (Parser::Error)

    on invalid input



33
34
35
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql.rb', line 33

def self.parse(query, **options)
  query = Grammar::Parser.new(query, **options).parse(options[:update] ? :UpdateUnit : :QueryUnit)
end

.serialize_exception(exception, **options) ⇒ String

Serialize error results

Returns appropriate content based upon an execution exception

Parameters:

Options Hash (**options):

  • Format (:format)

    of results, one of :html, :json or :xml. May also be an RDF::Writer format to serialize DESCRIBE or CONSTRUCT results

  • Format (:content_type)

    of results, one of 'application/sparql-results+json' or 'application/sparql-results+xml' May also be an RDF::Writer content_type to serialize DESCRIBE or CONSTRUCT results

Returns:

  • (String)

    String with serialized results and #content_type



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/results.rb', line 336

def serialize_exception(exception, **options)
  format = options[:format]
  content_type = options[:content_type]
  content_type ||= SPARQL::Results::MIME_TYPES[format]
  serialization = case content_type
  when 'text/html'
    title = exception.respond_to?(:title) ? exception.title : exception.class.to_s
    ERROR_MESSAGE % [title, title, exception.message]
  else
    content_type = "text/plain"
    exception.message
  end
  
  serialization.instance_eval do
    define_singleton_method(:content_type) { content_type }
  end

  serialization
end

.serialize_results(solutions, **options) ⇒ String

Serialize solutions using the determined format

Parameters:

Options Hash (**options):

  • :format (#to_sym)

    Format of results, one of :html, :json or :xml. May also be an RDF::Writer format to serialize DESCRIBE or CONSTRUCT results

  • :content_type (String)

    Format of results, one of 'application/sparql-results+json' or 'application/sparql-results+xml' May also be an RDF::Writer content_type to serialize DESCRIBE or CONSTRUCT results

  • :content_types (Array<String>)

    Similar to :content_type, but takes an ordered array of appropriate content types, and serializes using the first appropriate type, including wild-cards.

Returns:

  • (String)

    String with serialized results and #content_type

Raises:



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ae27c235c73f/lib/sparql/results.rb', line 215

def serialize_results(solutions, **options)
  format = options[:format].to_sym if options[:format]
  content_type = options[:content_type].to_s.split(';').first
  content_types = Array(options[:content_types] || '*/*')

  if !format && !content_type
    case solutions
    when RDF::Queryable
      content_type = first_content_type(content_types, RDF::Format.content_types.keys) || 'text/plain'
      format = RDF::Writer.for(content_type: content_type).to_sym
    else
      content_type = first_content_type(content_types, SPARQL::Results::MIME_TYPES.values) || 'application/sparql-results+xml'
      format = SPARQL::Results::MIME_TYPES.invert[content_type]
    end
  end

  serialization = case solutions
  when TrueClass, FalseClass, RDF::Literal::TRUE, RDF::Literal::FALSE
    solutions = solutions.object if solutions.is_a?(RDF::Literal)
    case format
    when :json
      require 'json' unless defined?(::JSON)
      {boolean: solutions}.to_json
    when :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.boolean(solutions.to_s)
      end
    when :html
      require 'builder' unless defined?(::Builder)
      content_type = "text/html"
      xml = ::Builder::XmlMarkup.new(indent: 2)
      xml.div(solutions.to_s, class: "sparql")
    else
      raise RDF::WriterError, "Unknown format #{(format || content_type).inspect} for #{solutions.class}"
    end
  when RDF::Queryable
    begin
      require 'linkeddata'
    rescue LoadError
      require 'rdf/ntriples'
    end
    fmt = RDF::Format.for(format ? format.to_sym : {content_type: content_type})
    unless fmt
      fmt = RDF::Format.for(file_extension: format.to_sym) || RDF::NTriples::Format
      format = fmt.to_sym
    end
    format ||= fmt.to_sym
    content_type ||= fmt.content_type.first
    results = solutions.dump(format, **options)
    raise RDF::WriterError, "Unknown format #{fmt.inspect} for #{solutions.class}" unless results
    results
  when RDF::Query::Solutions
    case format
    when :json  then solutions.to_json
    when :xml   then solutions.to_xml
    when :html  then solutions.to_html
    when :csv   then solutions.to_csv
    when :tsv   then solutions.to_tsv
    else
      raise RDF::WriterError, "Unknown format #{(format || content_type).inspect} for #{solutions.class}"
    end
  end

  content_type ||= SPARQL::Results::MIME_TYPES[format] if format
  
  serialization.instance_eval do
    define_singleton_method(:content_type) { content_type }
  end
  
  serialization
end