yak(credo-strict-tuned-config-both-apps-x02d): credo strict, tuned config (both apps)
This commit is contained in:
@@ -38,11 +38,9 @@ defmodule Blogex.LinkError do
|
||||
end
|
||||
|
||||
errors_list =
|
||||
errors
|
||||
|> Enum.map(fn {line, link, reason, _meta} ->
|
||||
Enum.map_join(errors, "\n", fn {line, link, reason, _meta} ->
|
||||
" line #{line}: #{link} — #{reason}"
|
||||
end)
|
||||
|> Enum.join("\n")
|
||||
|
||||
"""
|
||||
invalid internal blog links in #{blog}#{post_label}
|
||||
|
||||
@@ -129,24 +129,25 @@ defmodule Blogex.LinkValidator do
|
||||
@spec validate_link(String.t()) :: :ok | {:error, String.t()}
|
||||
def validate_link(link) when is_binary(link) do
|
||||
case parse_blog_link(link) do
|
||||
nil ->
|
||||
{:error, "not a blog link: #{link}"}
|
||||
|
||||
{blog_id_str, slug_part} ->
|
||||
case Map.fetch(@valid_blog_ids, blog_id_str) do
|
||||
{:ok, _blog_atom} ->
|
||||
if String.starts_with?(slug_part, "tag/") do
|
||||
validate_tag(String.replace_prefix(slug_part, "tag/", ""))
|
||||
else
|
||||
validate_slug(slug_part)
|
||||
end
|
||||
|
||||
:error ->
|
||||
{:error, "unknown blog ID: #{blog_id_str}"}
|
||||
end
|
||||
nil -> {:error, "not a blog link: #{link}"}
|
||||
{blog_id_str, path} -> validate_blog_path(blog_id_str, path)
|
||||
end
|
||||
end
|
||||
|
||||
# The blog ID must be one this app knows about; only then is the rest of the
|
||||
# path worth looking at. Extracted so `validate_link/1` stays one `case` deep:
|
||||
# the branching here is data (which part of the path is this?), not control.
|
||||
@spec validate_blog_path(String.t(), String.t()) :: :ok | {:error, String.t()}
|
||||
defp validate_blog_path(blog_id_str, path) do
|
||||
case Map.fetch(@valid_blog_ids, blog_id_str) do
|
||||
{:ok, _blog_atom} -> validate_path(path)
|
||||
:error -> {:error, "unknown blog ID: #{blog_id_str}"}
|
||||
end
|
||||
end
|
||||
|
||||
defp validate_path("tag/" <> tag), do: validate_tag(tag)
|
||||
defp validate_path(slug), do: validate_slug(slug)
|
||||
|
||||
@doc """
|
||||
Validates a list of links, returning errors with line numbers.
|
||||
|
||||
@@ -221,11 +222,13 @@ defmodule Blogex.LinkValidator do
|
||||
@doc false
|
||||
@spec parse_blog_link(String.t()) :: {String.t(), String.t()} | nil
|
||||
def parse_blog_link(path) do
|
||||
with ["", "blog", blog_id, rest] <- String.split(path, "/", parts: 4) do
|
||||
slug = String.slice(rest, 0, slug_end(rest))
|
||||
{blog_id, slug}
|
||||
else
|
||||
_ -> nil
|
||||
case String.split(path, "/", parts: 4) do
|
||||
["", "blog", blog_id, rest] ->
|
||||
slug = String.slice(rest, 0, slug_end(rest))
|
||||
{blog_id, slug}
|
||||
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -34,12 +34,15 @@ defmodule Blogex.Router do
|
||||
|
||||
use Plug.Router
|
||||
|
||||
alias Blogex.Router.Helpers
|
||||
alias Phoenix.HTML.Safe
|
||||
|
||||
plug :match
|
||||
plug :dispatch
|
||||
|
||||
get "/feed.xml" do
|
||||
blog = conn.private[:blogex_blog]
|
||||
base_url = Blogex.Router.Helpers.base_url(conn)
|
||||
base_url = Helpers.base_url(conn)
|
||||
|
||||
xml = Blogex.Feed.rss(blog, base_url)
|
||||
|
||||
@@ -50,7 +53,7 @@ defmodule Blogex.Router do
|
||||
|
||||
get "/atom.xml" do
|
||||
blog = conn.private[:blogex_blog]
|
||||
base_url = Blogex.Router.Helpers.base_url(conn)
|
||||
base_url = Helpers.base_url(conn)
|
||||
|
||||
xml = Blogex.Feed.atom(blog, base_url)
|
||||
|
||||
@@ -164,7 +167,7 @@ defmodule Blogex.Router do
|
||||
defp send_html(conn, rendered) do
|
||||
html =
|
||||
rendered
|
||||
|> Phoenix.HTML.Safe.to_iodata()
|
||||
|> Safe.to_iodata()
|
||||
|> IO.iodata_to_binary()
|
||||
|
||||
conn
|
||||
|
||||
Reference in New Issue
Block a user