Skip to content

Commit a73b483

Browse files
abrissegkellogg
authored andcommitted
Add URI constants
1 parent 7fb89e6 commit a73b483

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

lib/json/ld.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ module LD
110110

111111
MAX_CONTEXTS_LOADED = 50
112112

113+
# URI Constants
114+
RDF_JSON = RDF::URI("#{RDF.to_uri}JSON")
115+
RDF_DIRECTION = RDF::URI("#{RDF.to_uri}direction")
116+
RDF_LANGUAGE = RDF::URI("#{RDF.to_uri}language")
117+
113118
class JsonLdError < StandardError
114119
def to_s
115120
"#{self.class.instance_variable_get :@code}: #{super}"

lib/json/ld/from_rdf.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def from_statements(dataset, useRdfType: false, useNativeTypes: false, extendedR
5757
extendedRepresentation)
5858

5959
# If predicate is rdf:datatype, note subject in compound literal subjects map
60-
if @options[:rdfDirection] == 'compound-literal' && statement.predicate == RDF.to_uri + 'direction'
60+
if @options[:rdfDirection] == 'compound-literal' && statement.predicate == RDF_DIRECTION
6161
compound_literal_subjects[name][subject] ||= true
6262
end
6363

@@ -120,14 +120,14 @@ def from_statements(dataset, useRdfType: false, useNativeTypes: false, extendedR
120120

121121
v.delete('@id')
122122
v['@value'] = cl_node[RDF.value.to_s].first['@value']
123-
if cl_node[RDF.to_uri.to_s + 'language']
124-
lang = cl_node[RDF.to_uri.to_s + 'language'].first['@value']
123+
if (langs = cl_node[RDF_LANGUAGE.to_s])
124+
lang = langs.first['@value']
125125
unless /^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$/.match?(lang)
126126
warn "i18n datatype language must be valid BCP47: #{lang.inspect}"
127127
end
128128
v['@language'] = lang
129129
end
130-
v['@direction'] = cl_node[RDF.to_uri.to_s + 'direction'].first['@value']
130+
v['@direction'] = cl_node[RDF_DIRECTION.to_s].first['@value']
131131
end
132132
end
133133

@@ -209,7 +209,7 @@ def resource_representation(resource, useNativeTypes, extendedRepresentation)
209209
rdfDirection = @options[:rdfDirection]
210210
res = {}
211211

212-
if resource.datatype == RDF::URI(RDF.to_uri + "JSON") && @context.processingMode('json-ld-1.1')
212+
if resource.datatype == RDF_JSON && @context.processingMode('json-ld-1.1')
213213
res['@type'] = '@json'
214214
res['@value'] = begin
215215
::JSON.parse(resource.object)

lib/json/ld/streaming_writer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def stream_statement(statement)
6060

6161
pd << if statement.object.resource?
6262
{ '@id' => statement.object.to_s }
63-
elsif statement.object.datatype == RDF::URI(RDF.to_uri + "JSON")
63+
elsif statement.object.datatype == RDF_JSON
6464
{ "@value" => MultiJson.load(statement.object.to_s), "@type" => "@json" }
6565
else
6666
lit = { "@value" => statement.object.to_s }

lib/json/ld/to_rdf.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def item_to_rdf(item, graph_name: nil, quoted: false, &block)
2424
value = item.fetch('@value')
2525
datatype = item.fetch('@type', nil)
2626

27-
datatype = RDF::URI(RDF.to_uri + "JSON") if datatype == '@json'
27+
datatype = RDF_JSON if datatype == '@json'
2828

2929
case value
3030
when RDF::Value
@@ -35,7 +35,7 @@ def item_to_rdf(item, graph_name: nil, quoted: false, &block)
3535
datatype ||= RDF::XSD.boolean.to_s
3636
when Numeric
3737
# Otherwise, if value is a number, then set value to its canonical lexical form as defined in the section Data Round Tripping. If datatype is null, set it to either xsd:integer or xsd:double, depending on if the value contains a fractional and/or an exponential component.
38-
value = if datatype == RDF::URI(RDF.to_uri + "JSON")
38+
value = if datatype == RDF_JSON
3939
value.to_json_c14n
4040
else
4141
# Don't serialize as double if there are no fractional bits
@@ -62,15 +62,15 @@ def item_to_rdf(item, graph_name: nil, quoted: false, &block)
6262
when 'compound-literal'
6363
cl = RDF::Node.new
6464
yield RDF::Statement(cl, RDF.value, item['@value'].to_s)
65-
yield RDF::Statement(cl, RDF.to_uri + 'language', item['@language'].downcase) if item['@language']
66-
yield RDF::Statement(cl, RDF.to_uri + 'direction', item['@direction'])
65+
yield RDF::Statement(cl, RDF_LANGUAGE, item['@language'].downcase) if item['@language']
66+
yield RDF::Statement(cl, RDF_DIRECTION, item['@direction'])
6767
return cl
6868
end
6969
end
7070

7171
# Otherwise, if datatype is null, set it to xsd:string or xsd:langString, depending on if item has a @language key.
7272
datatype ||= item.key?('@language') ? RDF.langString : RDF::XSD.string
73-
value = value.to_json_c14n if datatype == RDF::URI(RDF.to_uri + "JSON")
73+
value = value.to_json_c14n if datatype == RDF_JSON
7474
end
7575
datatype = RDF::URI(datatype) if datatype && !datatype.is_a?(RDF::URI)
7676

0 commit comments

Comments
 (0)