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.5"

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


75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/youtube_embed.rb', line 75

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


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
68
69
70
71
72
73
# File 'lib/youtube_embed.rb', line 34

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)
        if video_details.entry !='failed'  #invalid video check
          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"] ? thumbnails[1]["url"] : ""}" />
              </div>
              <div class="youtube_embed_details">
                <div class="youtube_embed_title">
                 <strong>
                   #{video_details.title["__content__"] ? video_details.title["__content__"] : ""}
                 </strong>
                </div>
                <div class="youtube_embed_description">
                  #{video_details.description["__content__"] ? 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
      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
31
32
# File 'lib/youtube_embed.rb', line 22

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