Cleanup of the remote context processing
This commit is contained in:
parent
140faf4741
commit
426bc8f56d
3 changed files with 19 additions and 17 deletions
|
@ -59,16 +59,13 @@ defmodule JSON.LD.Context do
|
|||
# 3.2.3)
|
||||
document_loader = options.document_loader || JSON.LD.DocumentLoader.Default
|
||||
|
||||
document = try do
|
||||
document =
|
||||
case apply(document_loader, :load, [local, options]) do
|
||||
{:ok, result} -> result.document
|
||||
{:error, reason} -> raise JSON.LD.LoadingRemoteContextFailedError,
|
||||
message: "Could not load remote context (#{local}): #{inspect reason}"
|
||||
end
|
||||
rescue
|
||||
e -> raise JSON.LD.LoadingRemoteContextFailedError,
|
||||
message: "Could not load remote context: #{inspect e}"
|
||||
end
|
||||
|
||||
document = cond do
|
||||
is_map(document) -> document
|
||||
is_binary(document) -> case Jason.decode(document) do
|
||||
|
@ -79,11 +76,10 @@ defmodule JSON.LD.Context do
|
|||
true -> raise JSON.LD.InvalidRemoteContextError,
|
||||
message: "Context is not a valid JSON object: #{inspect document}"
|
||||
end
|
||||
local = case document["@context"] do
|
||||
nil -> raise JSON.LD.InvalidRemoteContextError,
|
||||
message: "Invalid remote context: No @context key in #{inspect document}"
|
||||
value -> value
|
||||
end
|
||||
|
||||
local = document["@context"] ||
|
||||
raise JSON.LD.InvalidRemoteContextError,
|
||||
message: "Invalid remote context: No @context key in #{inspect document}"
|
||||
|
||||
# 3.2.4) - 3.2.5)
|
||||
do_update(active, local, remote, options)
|
||||
|
|
|
@ -4,9 +4,15 @@ defmodule JSON.LD.DocumentLoader.Default do
|
|||
alias JSON.LD.DocumentLoader.RemoteDocument
|
||||
|
||||
def load(url, _options) do
|
||||
with {:ok, res} <- HTTPoison.get(url, [accept: "application/ld+json"], follow_redirect: true),
|
||||
with {:ok, res} <- http_get(url),
|
||||
{:ok, data} <- Jason.decode(res.body) do
|
||||
{:ok, %RemoteDocument{document: data, document_url: res.request_url}}
|
||||
end
|
||||
end
|
||||
|
||||
defp http_get(url) do
|
||||
HTTPoison.get(url, [accept: "application/ld+json"], follow_redirect: true)
|
||||
rescue
|
||||
e -> {:error, "HTTPoison failed: #{inspect e}"}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
defmodule JSON.LD.DocumentLoader.RemoteDocument do
|
||||
@type t :: %JSON.LD.DocumentLoader.RemoteDocument{context_url: String.t,
|
||||
document_url: String.t,
|
||||
document: any}
|
||||
@type t :: %__MODULE__{
|
||||
context_url: String.t,
|
||||
document_url: String.t,
|
||||
document: any
|
||||
}
|
||||
|
||||
defstruct context_url: nil,
|
||||
document_url: nil,
|
||||
document: nil
|
||||
defstruct [:context_url, :document_url, :document]
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue