From c819ff2f203c8390f2a1f46c3fb23375281d6e77 Mon Sep 17 00:00:00 2001 From: Daniel Kempkens Date: Thu, 25 May 2023 22:07:28 +0200 Subject: [PATCH] Support empty titles --- lib/bdfr_browser/http/plug.ex | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/bdfr_browser/http/plug.ex b/lib/bdfr_browser/http/plug.ex index 6fcb559..ab79b7f 100644 --- a/lib/bdfr_browser/http/plug.ex +++ b/lib/bdfr_browser/http/plug.ex @@ -104,7 +104,7 @@ defmodule BdfrBrowser.HTTP.Plug do {:ok, content} = [post_dir, post] |> Path.join() |> File.read!() |> Jason.decode() %{ - title: content["title"], + title: extract_title(content["title"]), author: content["author"], num_comments: content["num_comments"], created_utc: content["created_utc"], @@ -115,12 +115,21 @@ defmodule BdfrBrowser.HTTP.Plug do Enum.sort_by(compact_posts, fn p -> p.created_utc end, sort) end + defp extract_title(title) do + if String.length(title) == 0 do + "Empty Title" + else + title + end + end + defp read_post(post, args) do base_directory = Application.fetch_env!(:bdfr_browser, :base_directory) post_dir = Path.join([base_directory | Keyword.fetch!(args, :paths)]) post_file = "#{post}.json" {:ok, content} = [post_dir, post_file] |> Path.join() |> File.read!() |> Jason.decode(keys: :atoms) + content end