Skip to content

Commit efaa712

Browse files
committed
Add README entry on creating a custom documentLoader. This fixes #17.
1 parent cbfd5a8 commit efaa712

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,24 @@ This gem implements an optimized streaming writer used for generating JSON-LD fr
222222
}
223223
]
224224

225+
## Use a custom Document Loader
226+
In some cases, the built-in document loader {JSON::LD::API.documentLoader} is inadequate; for example, when using `http://schema.org` as a remote context, it will be re-loaded every time.
227+
228+
All entries into the {JSON::LD::API} accept a `:documentLoader` option, which can be used to provide an alternative method to use when loading remote documents. For example:
229+
230+
def load_document_local(url, options={}, &block)
231+
if RDF::URI(url, canonicalize: true) == RDF::URI('http://schema.org/')
232+
remote_document = JSON::LD::API::RemoteDocument.new(url, File.read("etc/schema.org.jsonld"))
233+
return block_given? ? yield(remote_document) : remote_document
234+
else
235+
JSON::LD::API.documentLoader(url, options, &block)
236+
end
237+
end
238+
239+
Then, when performing something like expansion:
240+
241+
JSON::LD::API.expand(input, documentLoader: load_document_local)
242+
225243
## RDF Reader and Writer
226244
{JSON::LD} also acts as a normal RDF reader and writer, using the standard RDF.rb reader/writer interfaces:
227245

0 commit comments

Comments
 (0)