Created
November 28, 2012 23:34
-
-
Save xijo/4165555 to your computer and use it in GitHub Desktop.
Alternative oembed solutions
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
class OembedTranslator | |
include ActionView::Helpers::TagHelper | |
# usual stuff over here.. | |
def embed | |
@content.gsub(/(?:<a.+?<\/a>)|((?:www|https?).*?)(?=\s|\Z)/) do |match| | |
match.present? or next # regexp not perfect yet, there are nil matches for some reason | |
oembed_code_for(match) | |
end | |
end | |
private | |
def oembed_code_for(link) | |
response = OEmbed::Providers.get(link, :width => 450, :maxWidth => 450) | |
response.html | |
rescue OEmbed::NotFound | |
link.start_with?("<a") ? link : content_tag(:a, link, href: link) | |
rescue OEmbed::Error | |
link | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice one