-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathedtf.ex
89 lines (69 loc) · 1.78 KB
/
edtf.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
defmodule EDTF do
@moduledoc """
EDTF Parsing GenServer
"""
use GenServer
alias Meadow.Config
alias Meadow.Utils.Lambda
import Meadow.Utils.Atoms
require Logger
@timeout 1000
@doc """
Parse an EDTF date string
Example:
iex> parse("1999-06-10")
{:ok, %{level: 0, type: "Date", values: [1999, 5, 10]}}
iex> parse("bad date!")
{:error, "Invalid EDTF input: bad date!"}
"""
def parse(value) do
case GenServer.call(__MODULE__, {:parse, value}) do
{:ok, result} -> {:ok, atomize(result)}
other -> other
end
end
@doc """
Validate an EDTF date string
Example:
iex> validate("1999-06-10")
{:ok, "1999-06-10"}
iex> validate("bad date!")
{:error, "Invalid EDTF input: bad date!"}
"""
def validate(value),
do: GenServer.call(__MODULE__, {:validate, value})
@doc """
Humanize an EDTF date string
Example:
iex> humanize("1999-06-10")
"June 10, 1999"
iex> humanize("bad date!")
{:error, "Invalid EDTF input: bad date!"}
"""
def humanize(value) do
case value |> parse() |> EDTF.Humanize.humanize() do
:original -> value
other -> other
end
end
def child_spec(opts) do
%{
id: __MODULE__,
start: {__MODULE__, :start_link, [opts]}
}
end
def start_link(args \\ []) do
GenServer.start_link(__MODULE__, args, name: __MODULE__)
end
def init(_args) do
Logger.info("Starting EDTF Parser")
case script_config() |> Lambda.init() do
{_, port} -> {:ok, port}
other -> other
end
end
def handle_call({command, data}, _from, port) do
{:reply, script_config() |> Lambda.invoke(%{function: command, value: data}, @timeout), port}
end
defp script_config, do: {:local, {Config.priv_path("nodejs/edtf/index.js"), "handler"}}
end