Add RDF.Serialization with functions to access available formats

This commit is contained in:
Marcel Otto 2018-03-08 01:57:38 +01:00
parent 7314f2d643
commit 4f5b06c830
3 changed files with 119 additions and 2 deletions

View file

@ -9,12 +9,19 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
### Changed
- rename `RDF.Serialization` behaviour to `RDF.Serialization.Format`
- rename `RDF.Serialization` behaviour to `RDF.Serialization.Format`; the new
`RDF.Serialization` module contains just simple RDF serialization related functions
### Added
- `RDF.Serialization.Format`s define an atom name
- `RDF.Serialization.Format`s define a `name` atom
- The following functions to access available `RDF.Serialization.Format`s:
- `RDF.Serialization.formats/0`
- `RDF.Serialization.available_formats/0`
- `RDF.Serialization.format/1`
- `RDF.Serialization.format_by_content_type/1`
- `RDF.Serialization.format_by_extension/1`
[Compare v0.3.1...HEAD](https://github.com/marcelotto/rdf-ex/compare/v0.3.1...HEAD)

View file

@ -0,0 +1,105 @@
defmodule RDF.Serialization do
@moduledoc """
General functions for working with RDF serializations.
"""
@formats [
RDF.Turtle,
JSON.LD,
RDF.NTriples,
RDF.NQuads,
]
@doc """
The list of all known `RDF.Serialization.Format`s in the RDF.ex eco-system.
Note: Not all known formats might be available to an application, see `available_formats/0`.
## Examples
iex> RDF.Serialization.formats
[RDF.Turtle, JSON.LD, RDF.NTriples, RDF.NQuads]
"""
def formats, do: @formats
@doc """
The list of all available `RDF.Serialization.Format`s in an application.
A known format might not be available in an application, when the format is
implemented in an external library and this not specified as a Mix dependency
of this application.
## Examples
iex> RDF.Serialization.available_formats
[RDF.Turtle, RDF.NTriples, RDF.NQuads]
"""
def available_formats do
Enum.filter @formats, &Code.ensure_loaded?/1
end
@doc """
Returns the `RDF.Serialization.Format` with the given name, if available.
## Examples
iex> RDF.Serialization.format(:turtle)
RDF.Turtle
iex> RDF.Serialization.format("turtle")
RDF.Turtle
iex> RDF.Serialization.format(:jsonld)
nil # unless json_ld is defined as a dependency of the application
"""
def format(name)
def format(name) when is_binary(name) do
try do
name
|> String.to_existing_atom
|> format()
rescue
ArgumentError -> nil
end
end
def format(name) do
format_where(fn format -> format.name == name end)
end
@doc """
Returns the `RDF.Serialization.Format` with the given media type, if available.
## Examples
iex> RDF.Serialization.format_by_content_type("text/turtle")
RDF.Turtle
iex> RDF.Serialization.format_by_content_type("application/ld+json")
nil # unless json_ld is defined as a dependency of the application
"""
def format_by_content_type(content_type) do
format_where(fn format -> format.content_type == content_type end)
end
@doc """
Returns the proper `RDF.Serialization.Format` for the given file extension, if available.
## Examples
iex> RDF.Serialization.format_by_extension("ttl")
RDF.Turtle
iex> RDF.Serialization.format_by_extension("jsonld")
nil # unless json_ld is defined as a dependency of the application
"""
def format_by_extension(extension) do
format_where(fn format -> format.extension == extension end)
end
defp format_where(fun) do
@formats
|> Stream.filter(&Code.ensure_loaded?/1)
|> Enum.find(fun)
end
end

View file

@ -0,0 +1,5 @@
defmodule RDF.SerializationTest do
use ExUnit.Case
doctest RDF.Serialization
end