Module: YoutubeEmbed

Defined in:
lib/youtube_embed.rb,
lib/youtube_embed/engine.rb,
lib/youtube_embed/railtie.rb,
lib/youtube_embed/version.rb,
lib/youtube_embed/video_object.rb,
lib/youtube_embed/video_details.rb,
lib/youtube_embed/model_additions.rb

Defined Under Namespace

Modules: ModelAdditions Classes: Engine, Railtie, VideoDetails, VideoObject

Constant Summary collapse

VIDEO_FORMATS =
[
    %r(https?://www\.youtube\.com/embed/(.*?)(\?|$)),
    %r(https?://youtu\.be/(.+)),
    %r(https?://www\.youtube\.com/user/.*?#\w/\w/\w/\w/(.+)\b),
    %r(https?://www\.youtube\.com/v/(.*?)(#|\?|$)),
    %r(https?://www\.youtube\.com/watch\?v=(.*?)(&|#|$))
]
VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.get_video_id(url) ⇒ Object



17
18
19
20
# File 'lib/youtube_embed.rb', line 17

def self.get_video_id(url)
  url.strip!
  VIDEO_FORMATS.find { |video_format| url =~ video_format } and $1
end

.simple(video_url, width, height) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/youtube_embed.rb', line 69

def self.simple(video_url, width, height)
  begin
    if video_url.to_s != ' '
      video_id = get_video_id(video_url)
      if video_id.present?
        return %Q{<div class="youtube_embed_video"><iframe title="YouTube player" width="#{ width }" height="#{ height }" src="http://www.youtube.com/embed/#{ video_id }" frameborder="0" allowfullscreen></iframe></div>}
      else
        return video_url
      end
  end
  rescue Exception => e
    Rails.logger.debug e.message
  end
end

.thumbnail_and_description(video_url, width, height) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/youtube_embed.rb', line 32

def self.thumbnail_and_description(video_url, width, height)
  begin
    if video_url.to_s != ' '
      video_id = get_video_id(video_url)
      if video_id.present?
        video_details = YoutubeEmbed::VideoDetails.new(video_id)
        thumbnails = video_details.thumbnail
        return %Q{<div class="youtube_embed_video">
          <div class="youtube_embed_partial_video">
            <div class="youtube_embed_thumbnail">
              <img src="#{thumbnails[1]["url"]}" />
            </div>
            <div class="youtube_embed_details">
              <div class="youtube_embed_title">
               <strong>
                 #{video_details.title["__content__"]}
               </strong>
              </div>
              <div class="youtube_embed_description">
                #{video_details.description["__content__"].truncate(185)}
              </div>
            </div>
          </div>
          <div class="youtube_embed_main_video" style="display:none;">
            <iframe title="YouTube player" width="#{ width }" height="#{ height }" src="http://www.youtube.com/embed/#{ video_id }" frameborder="0" allowfullscreen></iframe>
          </div>
        </div>}
      else
        return video_url
      end

    end
  rescue Exception => e
    Rails.logger.debug e.message
  end
end

.youtube_embed(data, options = {:with_description => true, :height => 200, :width => 300}) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/youtube_embed.rb', line 22

def self.youtube_embed(data, options = {:with_description => true, :height => 200, :width => 300})
  data = data.gsub(/<a?[^<]+ href="[(https?:\/\/)?(www\.)?youtube.com[^<]+]+">([^<]+)<\/a>/i, '\1')
  if options[:with_description]
    data = data.gsub(/https?:\/\/?(?:www\.)?youtube\.com(?:\/v\/|\/watch\?v=)([A-Za-z0-9_-]{11})/, thumbnail_and_description("#{$1}", options[:width], options[:height]))
  else
    data = data.gsub(/https?:\/\/?(?:www\.)?youtube\.com(?:\/v\/|\/watch\?v=)([A-Za-z0-9_-]{11})/, simple("#{$1}", options[:width], options[:height]))
  end
  return data
end