Generate feed for posts.
This commit is contained in:
parent
f3bcea48e6
commit
f9906fa4f7
12
lib/jol_web/controllers/feed_controller.ex
Normal file
12
lib/jol_web/controllers/feed_controller.ex
Normal 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
20
lib/jol_web/feeds.ex
Normal 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
|
Loading…
Reference in a new issue