Created
January 29, 2014 17:02
-
-
Save mkwiatkowski/8692263 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
# application_helper.rb | |
def bookmark_thumbnail(metadata) | |
image_tag(metadata[:thumbnail_url] || "thumb.png", width: "260") | |
end | |
# migration | |
add_column :bookmarks, :metadata, :text | |
# bookmark model | |
after_save :get_embedly_data | |
serialize :metadata, Hash | |
private | |
def get_embedly_data | |
embedly_api = set_key | |
obj = embedly_api.oembed(:url => self.url) | |
h = { url: obj[0].provider_url, | |
description: obj[0].description, | |
title: obj[0].title, | |
thumbnail_width: obj[0].thumbnail_width, | |
thumbnail_url: obj[0].thumbnail_url } | |
if h[:thumbnail_url] | |
begin | |
open(h[:thumbnail_url]) | |
rescue OpenURI::HTTPError | |
h.delete(:thumbnail_url) | |
end | |
end | |
self.metadata = h | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment