Skip to content

Commit 539bf62

Browse files
committed
refactor: reuse parse_date function
1 parent 2880fd0 commit 539bf62

File tree

2 files changed

+45
-43
lines changed

2 files changed

+45
-43
lines changed

server/lib/publisher/wordpress/episode.ex

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,38 @@ defmodule Publisher.WordPress.Episode do
3838
|> String.trim_leading(".")
3939
end
4040

41+
@doc """
42+
Parses the given date string and returns a DateTime struct or nil.
43+
44+
## Examples
45+
46+
iex> parse_date("2023-10-05T14:48:00Z")
47+
~U[2023-10-05 14:48:00Z]
48+
49+
iex> parse_date("not a date")
50+
nil
51+
52+
"""
53+
def parse_date(date) do
54+
formats = [
55+
"{RFC822}",
56+
"{RFC822z}",
57+
"{RFC1123}",
58+
"{RFC1123z}",
59+
"{RFC3339}",
60+
"{RFC3339z}",
61+
"{ISO:Extended}",
62+
"{ISO:Extended:Z}"
63+
]
64+
65+
Enum.find_value(formats, fn format ->
66+
case Timex.parse(date, format) do
67+
{:ok, date} -> date
68+
{:error, _} -> false
69+
end
70+
end)
71+
end
72+
4173
# Finds episode by guid. Creates episode with that episode if none exists.
4274
# Returns episode id.
4375
defp find_or_create_episode(req, guid) do
@@ -106,26 +138,6 @@ defmodule Publisher.WordPress.Episode do
106138
|> DateTime.to_iso8601()
107139
end
108140

109-
defp parse_date(date) do
110-
formats = [
111-
"{RFC822}",
112-
"{RFC822z}",
113-
"{RFC1123}",
114-
"{RFC1123z}",
115-
"{RFC3339}",
116-
"{RFC3339z}",
117-
"{ISO:Extended}",
118-
"{ISO:Extended:Z}"
119-
]
120-
121-
Enum.find_value(formats, fn format ->
122-
case Timex.parse(date, format) do
123-
{:ok, date} -> date
124-
{:error, _} -> false
125-
end
126-
end)
127-
end
128-
129141
defp upload_content(req, post_id, %{"content" => content, "pub_date" => pub_date} = _params)
130142
when not is_nil(content) and not is_nil(pub_date) do
131143
Logger.info("Episode post #{post_id} content is #{String.length(content)}")
@@ -283,6 +295,7 @@ defmodule Publisher.WordPress.Episode do
283295
case create_contributor(req, name) do
284296
{:ok, id} ->
285297
[id | acc]
298+
286299
{:error, reason} ->
287300
Logger.info("Couldn't create a contributor: #{inspect(reason)}")
288301
acc
@@ -368,16 +381,19 @@ defmodule Publisher.WordPress.Episode do
368381

369382
defp save_episode_image_url(req, episode_id, source_url) do
370383
Logger.info("Episode use #{source_url} as cover: #{episode_id}")
384+
371385
body = %{
372386
episode_poster: source_url
373387
}
388+
374389
Req.post(req, url: "podlove/v2/episodes/#{episode_id}", json: body)
375390
:ok
376391
end
377392

378393
defp upload_cover(req, episode_id, post_id, %{"cover" => cover} = params)
379-
when not is_nil(cover) do
394+
when not is_nil(cover) do
380395
image_name = "cover-" <> params["slug"]
396+
381397
with {:ok, source_url} <- Media.upload_media_from_url(req, post_id, cover, image_name),
382398
:ok <- save_episode_image_url(req, episode_id, source_url) do
383399
:ok
Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
defmodule PublisherWeb.Controllers.Validator.SaveEpisode do
22
use PublisherWeb.Controllers.Validator.Validator
33

4+
import Publisher.WordPress.Episode, only: [parse_date: 1]
5+
46
embedded_schema do
57
field(:guid, :string)
68
field(:title, :string)
@@ -22,33 +24,17 @@ defmodule PublisherWeb.Controllers.Validator.SaveEpisode do
2224
|> validate_date(:pub_date)
2325
end
2426

25-
defp validate_date(changeset, field) do
26-
validate_change(changeset, field, fn _, value ->
27-
if publication_date_valid?(value) do
27+
defp validate_date(changeset, :pub_date) do
28+
validate_change(changeset, :pub_date, fn _, pub_date ->
29+
if publication_date_valid?(pub_date) do
2830
[]
2931
else
30-
[{field, " is not a conformed date"}]
32+
[pub_date: "is not a valid date"]
3133
end
3234
end)
3335
end
3436

35-
defp publication_date_valid?(field) do
36-
formats = [
37-
"{RFC822}",
38-
"{RFC822z}",
39-
"{RFC1123}",
40-
"{RFC1123z}",
41-
"{RFC3339}",
42-
"{RFC3339z}",
43-
"{ISO:Extended}",
44-
"{ISO:Extended:Z}"
45-
]
46-
47-
Enum.find_value(formats, fn format ->
48-
case Timex.parse(field, format) do
49-
{:ok, field} -> field
50-
{:error, _} -> false
51-
end
52-
end)
37+
defp publication_date_valid?(value) do
38+
parse_date(value) !== nil
5339
end
5440
end

0 commit comments

Comments
 (0)