Class: ShEx::Format

Inherits:
RDF::Format
  • Object
show all
Defined in:
lib/shex/format.rb

Overview

ShEx format specification. Note that this format does not define any readers or writers.

Examples:

Obtaining an ShEx format class

RDF::Format.for(:shex)           #=> ShEx::Format
RDF::Format.for("etc/foaf.shex")
RDF::Format.for(file_name:      "etc/foaf.shex")
RDF::Format.for(file_extension: "shex")
RDF::Format.for(content_type:   "application/shex")

Class Method Summary collapse

Class Method Details

.cli_commandsHash{Symbol => Lambda(Array, Hash)}

Hash of CLI commands appropriate for this format

Returns:

  • (Hash{Symbol => Lambda(Array, Hash)})


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
47
48
49
50
51
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/shex/format.rb', line 20

def self.cli_commands
  {
    shex: {
      description: "Validate repository given shape",
      help: "shex [--shape Resource] [--focus Resource] [--schema-input STRING] [--schema STRING] file",
      parse: true,
      lambda: -> (argv, **options) do
        options[:schema_input] ||= case options[:schema]
        when IO, StringIO then options[:schema]
        else RDF::Util::File.open_file(options[:schema]) {|f| f.read}
        end
        raise ArgumentError, "Shape matching requires a schema or reference to schema resource" unless options[:schema_input]
        raise ArgumentError, "Shape matching requires a focus node" unless options[:focus]
        format = options[:schema].to_s.end_with?('json') ? 'shexj' : 'shexc'
        shex = ShEx.parse(options[:schema_input], format: format, **options)

        if options[:to_sxp] || options[:to_json]
          options[:messages][:shex] = {}
          options[:messages][:shex].merge!({"S-Expression": [SXP::Generator.string(shex.to_sxp_bin)]}) if options[:to_sxp]
          options[:messages][:shex].merge!({ShExJ: [shex.to_json(JSON::LD::JSON_STATE)]}) if options[:to_json]
        else
          focus = options.delete(:focus)
          shape = options.delete(:shape)
          map = shape ? {focus => shape} : {}
          begin
            res = shex.execute(RDF::CLI.repository, map, focus: focus, **options)
            options[:messages][:shex] = {
              result: ["Satisfied shape."],
              detail: [SXP::Generator.string(res.to_sxp_bin)]
            }
          rescue ShEx::NotSatisfied => e
            options[:logger].error e.to_s
            options[:messages][:shex] = {
              result: ["Did not satisfied shape."],
              detail: [SXP::Generator.stringe.expression]
            }
            raise
          end
        end
      end,
      options: [
        RDF::CLI::Option.new(
          symbol: :focus,
          datatype: String,
          control: :text,
          use: :required,
          on: ["--focus Resource"],
          description: "Focus node within repository"
        ) {|v| RDF::URI(v)},
        RDF::CLI::Option.new(
          symbol: :shape,
          datatype: String,
          control: :text,
          use: :optional,
          on: ["--shape URI"],
          description: "Shape identifier within ShEx schema"
        ) {|v| RDF::URI(v)},
        RDF::CLI::Option.new(
          symbol: :schema_input,
          datatype: String,
          control: :none,
          on: ["--schema-input STRING"],
          description: "ShEx schema in URI encoded format"
        ) {|v| URI.decode(v)},
        RDF::CLI::Option.new(
          symbol: :schema,
          datatype: String,
          control: :url2,
          on: ["--schema URI"],
          description: "ShEx schema location"
        ) {|v| RDF::URI(v)},
        RDF::CLI::Option.new(
          symbol: :to_json,
          datatype: String,
          control: :checkbox,
          on: ["--to-json"],
          description: "Display parsed schema as ShExJ"
        ),
        RDF::CLI::Option.new(
          symbol: :to_sxp,
          datatype: String,
          control: :checkbox,
          on: ["--to-sxp"],
          description: "Display parsed schema as an S-Expression"
        ),
      ]
    }
  }
end