I recently released LiveForm which is a service which gives you form endpoints (I’d love to have you check it out :) ) I wanted to show my blog’s content on the home page, It is pretty straightforward with the rich ruby ecosystem.
- First you need a way to get the data from your blog. The LiveForm blog has an atom feed at http://blog.liveformhq.com/atom.xml . I initially used RestClient to get the data from the feed.
- Once we have the feed, we need to parse it to extract the right content. Some quick googling led me to the awesome feedjira gem, (I am not gonna comment about the awesome name:))
- feedjira actually has a simple method to parse the feed from a URL
Feedjira::Feed.fetch_and_parse(url)
- Once I got the entries, I just had to format them properly. However, there was an issue with summaries of blog posts having malformed html. This was due to naively slicing the blog post content at 200 characters by hexo (the blog engine I use), Nokogiri has a simple way of working around this. However, I went one step further and removed all html markup from the summary so that it doesn’t mess with the web application’s markup:
Nokogiri::HTML(entry.summary).css("body").text
- Finally, I didn’t want to fetch and parse my feed for every user that visited my website. So, I used fragment caching to render the feed once every day.
Here is all the relevant code:
The class that fetches and parses the feed
1 | class LiveformBlog |
The view that caches and renders the feed
1 | <%= cache Date.today.to_s do %> |
Screenshot of the current page
I am currently working on LiveForm which makes
setting up contact forms on your website a breeze.