Skip to content

Commit 3e49303

Browse files
committed
Add expand, compact, flatten and format CLI commands. This duplicates the functionality of the jsonld command in the rdf command.
1 parent 8b53522 commit 3e49303

File tree

3 files changed

+169
-6
lines changed

3 files changed

+169
-6
lines changed

lib/json/ld/format.rb

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,132 @@ def self.detect(sample)
4141
# Exclude CSVW metadata
4242
!sample.include?("http://www.w3.org/ns/csvw")
4343
end
44-
44+
45+
##
46+
# Hash of CLI commands appropriate for this format
47+
# @return [Hash{Symbol => Lambda(Array, Hash)}]
48+
def self.cli_commands
49+
{
50+
expand: ->(files, options) do
51+
out = options[:output] || $stdout
52+
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
53+
options = options.merge(expandContext: options.delete(:context)) if options.has_key?(:context)
54+
if options[:format] == :jsonld
55+
if files.empty?
56+
# If files are empty, either use options[:execute]
57+
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
58+
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
59+
JSON::LD::API.expand(input, options) do |expanded|
60+
out.puts expanded.to_json(JSON::LD::JSON_STATE)
61+
end
62+
else
63+
files.each do |file|
64+
JSON::LD::API.expand(file, options) do |expanded|
65+
out.puts expanded.to_json(JSON::LD::JSON_STATE)
66+
end
67+
end
68+
end
69+
else
70+
# Turn RDF into JSON-LD first
71+
RDF::CLI.parse(files, options) do |reader|
72+
JSON::LD::API.fromRdf(reader) do |expanded|
73+
out.puts expanded.to_json(JSON::LD::JSON_STATE)
74+
end
75+
end
76+
end
77+
end,
78+
compact: ->(files, options) do
79+
raise ArgumentError, "Compacting requires a context" unless options[:context]
80+
out = options[:output] || $stdout
81+
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
82+
if options[:format] == :jsonld
83+
if files.empty?
84+
# If files are empty, either use options[:execute]
85+
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
86+
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
87+
JSON::LD::API.compact(input, options[:context], options) do |compacted|
88+
out.puts compacted.to_json(JSON::LD::JSON_STATE)
89+
end
90+
else
91+
files.each do |file|
92+
JSON::LD::API.compact(file, options[:context], options) do |compacted|
93+
out.puts compacted.to_json(JSON::LD::JSON_STATE)
94+
end
95+
end
96+
end
97+
else
98+
# Turn RDF into JSON-LD first
99+
RDF::CLI.parse(files, options) do |reader|
100+
JSON::LD::API.fromRdf(reader) do |expanded|
101+
JSON::LD::API.compact(expanded, options[:context], options) do |compacted|
102+
out.puts compacted.to_json(JSON::LD::JSON_STATE)
103+
end
104+
end
105+
end
106+
end
107+
end,
108+
flatten: ->(files, options) do
109+
out = options[:output] || $stdout
110+
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
111+
if options[:format] == :jsonld
112+
if files.empty?
113+
# If files are empty, either use options[:execute]
114+
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
115+
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
116+
JSON::LD::API.flatten(input, options[:context], options) do |flattened|
117+
out.puts flattened.to_json(JSON::LD::JSON_STATE)
118+
end
119+
else
120+
files.each do |file|
121+
JSON::LD::API.flatten(file, options[:context], options) do |flattened|
122+
out.puts flattened.to_json(JSON::LD::JSON_STATE)
123+
end
124+
end
125+
end
126+
else
127+
# Turn RDF into JSON-LD first
128+
RDF::CLI.parse(files, options) do |reader|
129+
JSON::LD::API.fromRdf(reader) do |expanded|
130+
JSON::LD::API.flatten(expanded, options[:context], options) do |flattened|
131+
out.puts flattened.to_json(JSON::LD::JSON_STATE)
132+
end
133+
end
134+
end
135+
end
136+
end,
137+
frame: ->(files, options) do
138+
raise ArgumentError, "Framing requires a frame" unless options[:frame]
139+
out = options[:output] || $stdout
140+
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
141+
if options[:format] == :jsonld
142+
if files.empty?
143+
# If files are empty, either use options[:execute]
144+
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
145+
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
146+
JSON::LD::API.frame(input, options[:frame], options) do |framed|
147+
out.puts framed.to_json(JSON::LD::JSON_STATE)
148+
end
149+
else
150+
files.each do |file|
151+
JSON::LD::API.frame(file, options[:frame], options) do |framed|
152+
out.puts framed.to_json(JSON::LD::JSON_STATE)
153+
end
154+
end
155+
end
156+
else
157+
# Turn RDF into JSON-LD first
158+
RDF::CLI.parse(files, options) do |reader|
159+
JSON::LD::API.fromRdf(reader) do |expanded|
160+
JSON::LD::API.frame(expanded, options[:frame], options) do |framed|
161+
out.puts framed.to_json(JSON::LD::JSON_STATE)
162+
end
163+
end
164+
end
165+
end
166+
end,
167+
}
168+
end
169+
45170
##
46171
# Override normal symbol generation
47172
def self.to_sym

script/parse

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ def run(input, options)
5151
secs = Time.new - start
5252
options[:output].puts output.to_json(JSON::LD::JSON_STATE)
5353
puts "Framed in #{secs} seconds."
54-
elsif options[:frame]
55-
output = JSON::LD::API.frame(input, options[:frame], options)
56-
secs = Time.new - start
57-
options[:output].puts output.to_json(JSON::LD::JSON_STATE)
58-
puts "Framed in #{secs} seconds."
5954
else
6055
r = reader_class.new(input, options[:parser_options])
6156
if options[:output_format] == :none

spec/format_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,47 @@
6666
end
6767
end
6868
end
69+
70+
describe ".cli_commands" do
71+
require 'rdf/cli'
72+
let(:ttl) {File.expand_path("../test-files/test-1-rdf.ttl", __FILE__)}
73+
let(:json) {File.expand_path("../test-files/test-1-input.json", __FILE__)}
74+
let(:context) {File.expand_path("../test-files/test-1-context.json", __FILE__)}
75+
76+
describe "#expand" do
77+
it "expands RDF" do
78+
expect {RDF::CLI.exec_command("expand", [ttl], format: :ttl)}.to write.to(:output)
79+
end
80+
it "expands JSON" do
81+
expect {RDF::CLI.exec_command("expand", [json], format: :jsonld)}.to write.to(:output)
82+
end
83+
end
84+
85+
describe "#compact" do
86+
it "compacts RDF" do
87+
expect {RDF::CLI.exec_command("compact", [ttl], context: context, format: :ttl)}.to write.to(:output)
88+
end
89+
it "compacts JSON" do
90+
expect {RDF::CLI.exec_command("compact", [json], context: context, format: :jsonld)}.to write.to(:output)
91+
end
92+
end
93+
94+
describe "#flatten" do
95+
it "flattens RDF" do
96+
expect {RDF::CLI.exec_command("flatten", [ttl], context: context, format: :ttl)}.to write.to(:output)
97+
end
98+
it "flattens JSON" do
99+
expect {RDF::CLI.exec_command("flatten", [json], context: context, format: :jsonld)}.to write.to(:output)
100+
end
101+
end
102+
103+
describe "#frame" do
104+
it "frames RDF" do
105+
expect {RDF::CLI.exec_command("frame", [ttl], frame: context, format: :ttl)}.to write.to(:output)
106+
end
107+
it "frames JSON" do
108+
expect {RDF::CLI.exec_command("frame", [json], frame: context, format: :jsonld)}.to write.to(:output)
109+
end
110+
end
111+
end
69112
end

0 commit comments

Comments
 (0)