Created
August 15, 2017 18:57
-
-
Save thebrianemory/70a574e52670285d67c59fdd4d9ddba9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_blog_posts | |
# this would be so much easier if the Medium API allowed GET requests for posts | |
require 'rss' | |
@posts = [] | |
run_sanitizer | |
rss = RSS::Parser.parse(open('https://medium.brianemory.com/feed').read, false).items[0..2] | |
rss.each do |result| | |
result = { title: get_title(result.title), | |
date: get_date(result.pubDate), | |
link: get_link(result.link), | |
heading: get_heading(result.content_encoded), | |
content: get_content(result.content_encoded) } | |
@posts.push(result) | |
end | |
@posts | |
end | |
def run_sanitizer | |
@sanitizer = Rails::Html::FullSanitizer.new | |
end | |
def get_title(title) | |
title | |
end | |
def get_date(date) | |
date > 1.day.ago ? "#{time_ago_in_words(date)} ago" : date.strftime('%b %d, %Y') | |
end | |
def get_link(link) | |
link[/[^?]+/] | |
end | |
def get_heading(content) | |
@sanitizer.sanitize(content.sub(/(<h4>|<h3>)/, '<Z>') | |
.sub(/(<\/h4>|<\/h3>)/, '<Z>')[/(<Z>).*(<Z>)/]) | |
end | |
def get_content(content) | |
@sanitizer.sanitize(content.sub(/(<figure>).*(<\/figure>)/, '') | |
.truncate(300, separator: ' ') | |
.sub(/(<h4>|<h3>).*(<\/h4>|<\/h3>)/, '')) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment