-
-
Save jeena/9cf17b00f68dca4bcc21 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 set_from_html(html) | |
parser = Hpricot(html) | |
title = parser.at("title") | |
self.title ||= ActionView::Base.full_sanitizer.sanitize(title.inner_html).strip unless title.blank? | |
meta = parser.at("meta[@name='description']") || parser.at("meta[@property='og:description']") || parser.at("meta[@itemprop=description]") || parser.at("meta[@name='Description']") | |
self.excerpt ||= meta[:content] unless meta.nil? | |
author = parser.at("a[@rel=author]") | |
unless author.blank? | |
self.author_name ||= ActionView::Base.full_sanitizer.sanitize(author.inner_html).strip | |
self.author_url ||= source_base_url(author[:href]) | |
end | |
author_icon = author.at("img") | |
self.author_avatar_url ||= source_base_url(author_icon[:src]) unless author_icon.nil? | |
icon = parser.at("link[@rel=apple-touch-icon-precomposed]") || parser.at("link[@rel=apple-touch-icon]") | |
self.author_avatar_url ||= source_base_url(icon[:href]) unless icon.nil? | |
self.kind = "reply" unless parser.at("[@rel=in-reply-to]").nil? | |
end | |
def source_base_url(url) | |
URI.join(self.source_uri.gsub(URI.parse(self.source_uri).request_uri, ""), url).to_s | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment