Publish Atom feeds for posts. #1

Merged
phoenix merged 3 commits from feeds into main 2024-08-20 21:21:16 +00:00
2 changed files with 32 additions and 0 deletions
Showing only changes of commit f9906fa4f7 - Show all commits

View file

@ -0,0 +1,12 @@
defmodule JOLWeb.FeedController do
use JOLWeb, :controller
def feed(conn, _params) do
feed = JOL.Blog.all_posts()
|> JOLWeb.Feeds.build_feed()
conn
|> put_resp_content_type("text/xml")
|> send_resp(200, feed)
end
end

20
lib/jol_web/feeds.ex Normal file
View file

@ -0,0 +1,20 @@
defmodule JOLWeb.Feeds do
use Phoenix.VerifiedRoutes, endpoint: JOLWeb.Endpoint, router: JOLWeb.Router
alias Atomex.{Feed, Entry}
def build_feed(posts) do
Feed.new(url(~p"/"), DateTime.utc_now, "Jessica Online")
|> Feed.author("Jessica Phoenix Canady", email: "jess@canady.tech")
|> Feed.link(url(~p"/feed.xml"), rel: "self")
|> Feed.entries(Enum.map(posts, &build_entry/1))
|> Feed.build()
|> Atomex.generate_document()
end
defp build_entry(post) do
Entry.new(url(~p"/blog/#{post.slug}"), post.date, post.title)
|> Entry.author(post.author, uri: url(~p"/"))
|> Entry.content(post.body, type: "html")
|> Entry.build()
end
end