Skip to content

Commit a083623

Browse files
committed
Add :uri option to Format,content_type, and uris and to_uri class methods. This allows retrieving the defined format URI for specified formats. (See https://www.w3.org/ns/formats/).
1 parent 76f3e1a commit a083623

4 files changed

Lines changed: 79 additions & 6 deletions

File tree

lib/rdf/format.rb

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ module RDF
2323
#
2424
# @example Defining a new RDF serialization format class
2525
# class RDF::NTriples::Format < RDF::Format
26-
# content_type 'application/n-triples', extension: :nt
26+
# content_type 'application/n-triples',
27+
# extension: :nt,
28+
# uri: RDF::URI("http://www.w3.org/ns/formats/N-Triples")
2729
# content_encoding 'utf-8'
2830
#
2931
# reader RDF::NTriples::Reader
@@ -222,6 +224,19 @@ def self.file_extensions
222224
@@file_extensions
223225
end
224226

227+
##
228+
# Returns the unique URI for the format.
229+
#
230+
# @example retrieving a list of supported file format URIs
231+
#
232+
# RDF::Format.uris.keys
233+
#
234+
# @see https://www.w3.org/ns/formats/
235+
# @return [Hash{Symbol => URI}]
236+
def self.uris
237+
@@uris
238+
end
239+
225240
##
226241
# Returns the set of format symbols for available RDF::Reader subclasses.
227242
#
@@ -465,6 +480,7 @@ class << self
465480
# @option options [Array<String>] :aliases (nil)
466481
# @option options [Symbol] :extension (nil)
467482
# @option options [Array<Symbol>] :extensions (nil)
483+
# @option options [URI] :uri (nil)
468484
# @return [void]
469485
#
470486
# @overload content_type
@@ -504,6 +520,10 @@ def self.content_type(type = nil, options = {})
504520
@@content_types[aa] << self unless @@content_types[aa].include?(self)
505521
end
506522
end
523+
# URI identifying this format
524+
if uri = options[:uri]
525+
@@uris[RDF::URI(uri)] = self
526+
end
507527
end
508528
end
509529

@@ -517,7 +537,7 @@ def self.accept_type
517537
end
518538

519539
##
520-
# Retrieves or defines file extensions for this RDF serialization format.
540+
# Retrieves file extensions for this RDF serialization format.
521541
#
522542
# The return is an array where the first element is the cannonical
523543
# file extension for the format and following elements are alias file extensions.
@@ -527,6 +547,17 @@ def self.file_extension
527547
@@file_extensions.map {|ext, formats| ext if formats.include?(self)}.compact
528548
end
529549

550+
##
551+
# Retrieves any format URI defined for this format..
552+
#
553+
# @return [URI]
554+
def self.uri
555+
@@uris.invert[self]
556+
end
557+
class << self
558+
alias_method :to_uri, :uri
559+
end
560+
530561
protected
531562

532563
##
@@ -568,6 +599,7 @@ def self.content_encoding(encoding = nil)
568599
@@readers = {} # @private
569600
@@writers = {} # @private
570601
@@subclasses = [] # @private
602+
@@uris = {} # @private
571603

572604
##
573605
# @private

lib/rdf/nquads.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ module NQuads
2020
# @see http://www.w3.org/TR/n-quads/
2121
# @since 0.4.0
2222
class Format < RDF::Format
23-
content_type 'application/n-quads', extension: :nq, alias: 'text/x-nquads;q=0.2'
23+
content_type 'application/n-quads',
24+
extension: :nq,
25+
alias: 'text/x-nquads;q=0.2',
26+
uri: RDF::URI("http://www.w3.org/ns/formats/N-Quads")
2427
content_encoding 'utf-8'
2528

2629
reader { RDF::NQuads::Reader }

lib/rdf/ntriples/format.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ module RDF::NTriples
1616
# @see http://www.w3.org/TR/rdf-testcases/#ntriples
1717
# @see http://www.w3.org/TR/n-triples/
1818
class Format < RDF::Format
19-
content_type 'application/n-triples', extension: :nt, alias: 'text/plain;q=0.2'
19+
content_type 'application/n-triples',
20+
extension: :nt,
21+
alias: 'text/plain;q=0.2',
22+
uri: RDF::URI("http://www.w3.org/ns/formats/N-Triples")
2023
content_encoding 'utf-8'
2124

2225
reader { RDF::NTriples::Reader }

spec/format_spec.rb

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
class RDF::Format::FooFormat < RDF::Format
77
content_type 'application/test;q=1',
8-
extension: :test
8+
extension: :test,
9+
uri: "http://example.com/ns/formats/Foo"
910
reader { RDF::NTriples::Reader }
1011
def self.detect(sample)
1112
sample == "foo"
@@ -16,7 +17,8 @@ def self.to_sym; :foo_bar; end
1617
class RDF::Format::BarFormat < RDF::Format
1718
content_type 'application/test',
1819
extension: :test,
19-
alias: 'application/x-test;q=0.1'
20+
alias: 'application/x-test;q=0.1',
21+
uri: "http://example.com/ns/formats/Bar"
2022
reader { RDF::NTriples::Reader }
2123
def self.detect(sample)
2224
sample == "bar"
@@ -129,6 +131,16 @@ def self.to_sym; :foo_bar; end
129131
end
130132
end
131133

134+
describe ".uris" do
135+
it "returns accept-types of available readers with quality" do
136+
expect(RDF::Format.accept_types).to include(*%w(
137+
application/n-triples text/plain;q=0.2
138+
application/n-quads text/x-nquads;q=0.2
139+
application/test application/x-test;q=0.1
140+
))
141+
end
142+
end
143+
132144
describe ".writer_symbols" do
133145
it "returns symbols of available writers" do
134146
%i(ntriples nquads).each do |sym|
@@ -152,6 +164,15 @@ def self.to_sym; :foo_bar; end
152164
end
153165
end
154166

167+
describe ".uris" do
168+
it "matches format classes to URIs" do
169+
RDF::Format.uris.each do |u, c|
170+
expect(u).to be_a(RDF::URI)
171+
expect(c).to respond_to(:content_type)
172+
end
173+
end
174+
end
175+
155176
RDF::Format.each do |format|
156177
context format.name do
157178
subject {format}
@@ -166,6 +187,20 @@ def self.to_sym; :foo_bar; end
166187
end
167188
end
168189

190+
describe RDF::NTriples::Format do
191+
it "has a format URI" do
192+
expect(described_class.uri).to eql RDF::URI('http://www.w3.org/ns/formats/N-Triples')
193+
expect(described_class.to_uri).to eql RDF::URI('http://www.w3.org/ns/formats/N-Triples')
194+
end
195+
end
196+
197+
describe RDF::NQuads::Format do
198+
it "has a format URI" do
199+
expect(described_class.uri).to eql RDF::URI('http://www.w3.org/ns/formats/N-Quads')
200+
expect(described_class.to_uri).to eql RDF::URI('http://www.w3.org/ns/formats/N-Quads')
201+
end
202+
end
203+
169204
context "Examples" do
170205
require 'rdf/ntriples'
171206
subject {RDF::Format}

0 commit comments

Comments
 (0)