Module: Embed::EmbedHelper

Defined in:
lib/embed_helper.rb

Instance Method Summary collapse

Instance Method Details

#_oembed(oembed_url, url, params) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/embed_helper.rb', line 31

def _oembed(oembed_url, url, params)
  uri = URI(oembed_url)
  uri.query = params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&')
  response = Net::HTTP.get(uri)
  if response
    begin
      html = JSON.parse(response)["html"]
      html.respond_to?(:html_safe) ? html.html_safe : html
    rescue JSON::ParserError
      defined?(link_to) ? link_to(url) : url
    end
  else
    defined?(link_to) ? link_to(url) : url
  end
end

#_soundcloud_embed(url, protocol) ⇒ Object



21
22
23
24
# File 'lib/embed_helper.rb', line 21

def _soundcloud_embed(url, protocol)
  params = {:format => 'json', :url => url}
  _oembed("#{protocol}://soundcloud.com/oembed", url, params)
end

#_vimeo_embed(url, width, height, protocol) ⇒ Object



15
16
17
18
19
# File 'lib/embed_helper.rb', line 15

def _vimeo_embed(url, width, height, protocol)
  video_id = Embed.vimeo_video_id(url)
  html = %Q{<iframe src="#{protocol}://player.vimeo.com/video/#{video_id}" width="#{width}" height="#{height}" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>}
  html.respond_to?(:html_safe) ? html.html_safe : html
end

#_wistia_embed(url, width, height, protocol) ⇒ Object



26
27
28
29
# File 'lib/embed_helper.rb', line 26

def _wistia_embed(url, width, height, protocol)
  params = { :format => 'json', :url => url, :width => width, :height => height }
  _oembed("#{protocol}://fast.wistia.com/oembed", url, params)
end

#_youtube_embed(url, width, height, protocol) ⇒ Object



9
10
11
12
13
# File 'lib/embed_helper.rb', line 9

def _youtube_embed(url, width, height, protocol)
  video_id = Embed.youtube_video_id(url)
  html = %Q{<iframe id="#{video_id}" type="text/html" width="#{width}" height="#{height}" src="#{protocol}://www.youtube.com/embed/#{video_id}?autoplay=0" frameborder="0"></iframe>}
  html.respond_to?(:html_safe) ? html.html_safe : html
end

#embed(url, params = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/embed_helper.rb', line 47

def embed(url, params = {})
  return if url.nil? || url.empty?

  params[:width] ||= 640
  params[:height] ||= 390
  params[:protocol] ||= 'http'
  if url[/(youtube.com|youtu.be)/]
    return _youtube_embed(url, params[:width], params[:height], params[:protocol])
  elsif url[/vimeo.com/]
    return _vimeo_embed(url, params[:width], params[:height], params[:protocol])
  elsif url[/soundcloud.com/]
    return _soundcloud_embed(url, params[:protocol])
  elsif url[/wistia.com/]
    return _wistia_embed(url, params[:width], params[:height], params[:protocol])
  end
end