Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>SOLUTION:</strong></p> <p>Since this is a Rails app I decided to go for the <a href="https://github.com/judofyr/ruby-oembed" rel="nofollow">oembed gem</a> and parse the video links when the text is displayed rather than when it is entered. This is the best solution for me since storing the bare video URLs is more future proof than storing the full embed iframe code returned by oembed and hoping it never has to change.</p> <p>This code was hacked together pretty quickly and needs to be refactored soon but it's working fine for now and failure isn't in any way critical to the app.</p> <p>When displaying the lesson I run <code>parse_media_embeds</code> on the <code>lesson.notes</code> which finds all the links and for any matching the media sites it fetches the oEmbed code (with a <code>rescue</code> for errors) and replaces the link with the iframe. Totally janky but it allowed me to move on with the app.</p> <p>I'll post new code when I refactor it.</p> <p><strong>VIEW: lesson.html.rb</strong></p> <pre><code>... &lt;section class='lesson-notes'&gt; &lt;p&gt;&lt;%= lesson.parse_media_embeds.html_safe %&gt;&lt;/p&gt; &lt;/section&gt; ... </code></pre> <p><strong>MODEL: lesson.rb</strong></p> <pre><code>def parse_media_embeds # get an array of links in the notes links = URI.extract self.notes, /http(s)?/ # due to wysihtml5 parsing each link is stored twice, once as url and once as text # like &lt;a href='http://google.com'&gt;http://google.com&lt;/a&gt; # so remove the double references for pos in (0..links.count) link.delete_at(pos) end # now replace each link that matches the Regexp pattern with oembed version links.each do |link| # only replace links for YouTube, Vimeo and Soundcloud if link.match(/youtu\.be|youtube|vimeo|soundcloud/) # get the full link tag, which includes both instances of the link text string = /(&lt;a\w*[^&gt;]*&gt;#{Regexp.escape(link)}[^&lt;]*&lt;\/a&gt;)/ embed = get_oembed(link) self.notes = self.notes.gsub(string, embed) if embed end end self.notes end </code></pre> <p><strong>MODEL: lesson.rb</strong></p> <pre><code>def get_oembed(link) # each provider has a different set of params if link.match(/youtu\.be|youtube/) begin resource = OEmbed::Providers::Youtube.get(link, {maxwidth: 320}) rescue return end resource.html elsif link.match(/vimeo/) begin resource = OEmbed::Providers::Vimeo.get(link, {maxwidth: 320}) rescue return end resource.html elsif link.match(/soundcloud/) begin resource = OEmbed::Providers::SoundCloud.get(link, {color: '39d', show_comments: false}) rescue return end resource.html else return end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload