o_embed_helper.rb 975 Bytes
Newer Older
1 2
# frozen_string_literal: true

3 4 5
module OEmbedHelper
  def o_embed_html(cache)
    data = cache.data
6 7 8 9
    data = {} if data.blank?
    title = data.fetch('title', cache.url)
    html = link_to(title, cache.url, :target => '_blank') 
    return html unless data.has_key?('type')
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
    case data['type']
    when 'video', 'rich'
      if cache.is_trusted_and_has_html?
        html = data['html']
      elsif data.has_key?('thumbnail_url')
        html = link_to_oembed_image(cache)
      end
    when 'photo'
      if data.has_key?('url')
        img_options = cache.options_hash('')
        html = link_to_oembed_image(cache, '')
      end
    else
    end

25
    return html.gsub('http://', 'https://').html_safe
26 27 28 29 30 31 32
  end

  def link_to_oembed_image(cache, prefix = 'thumbnail_')
    link_to(oembed_image_tag(cache, prefix), cache.url, :target => '_blank')
  end
  
  def oembed_image_tag(cache, prefix)
33
    image_tag(cache.data[prefix + 'url'], cache.options_hash(prefix))
34 35
  end
end