Class: YouTubeRails
- Inherits:
-
Object
- Object
- YouTubeRails
- Defined in:
- lib/youtube_rails.rb
Constant Summary collapse
- URL_FORMATS =
{ regular: /^(https?:\/\/)?(www\.)?youtube.com\/watch\?(.*\&)?v=(?<id>[^&]+)/, shortened: /^(https?:\/\/)?(www\.)?youtu.be\/(?<id>[^&]+)/, embed: /^(https?:\/\/)?(www\.)?youtube.com\/embed\/(?<id>[^&]+)/, embed_as3: /^(https?:\/\/)?(www\.)?youtube.com\/v\/(?<id>[^?]+)/, chromeless_as3: /^(https?:\/\/)?(www\.)?youtube.com\/apiplayer\?video_id=(?<id>[^&]+)/ }
- INVALID_CHARS =
/[^a-zA-Z0-9\:\/\?\=\&\$\-\_\.\+\!\*\'\(\)\,]/
Class Method Summary collapse
- .extract_video_id(youtube_url) ⇒ Object
- .extract_video_image(youtube_url, version = 'default') ⇒ Object
- .has_invalid_chars?(youtube_url) ⇒ Boolean
- .youtube_embed_url(youtube_url, width = 420, height = 315, options = {}) ⇒ Object
- .youtube_embed_url_only(youtube_url, options = {}) ⇒ Object
- .youtube_regular_url(youtube_url, options = {}) ⇒ Object
- .youtube_shortened_url(youtube_url, options = {}) ⇒ Object
Class Method Details
.extract_video_id(youtube_url) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/youtube_rails.rb', line 16 def self.extract_video_id(youtube_url) return nil if has_invalid_chars?(youtube_url) URL_FORMATS.values.inject(nil) do |result, format_regex| match = format_regex.match(youtube_url) match ? match[:id] : result end end |
.extract_video_image(youtube_url, version = 'default') ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/youtube_rails.rb', line 44 def self.extract_video_image(youtube_url, version = 'default') vid_id = extract_video_id(youtube_url) case version when 'default' "https://i.ytimg.com/vi/#{ vid_id }/default.jpg" when 'medium' "https://i.ytimg.com/vi/#{ vid_id }/mqdefault.jpg" when 'high' "https://i.ytimg.com/vi/#{ vid_id }/hqdefault.jpg" when 'maximum' "https://i.ytimg.com/vi/#{ vid_id }/sddefault.jpg" end end |
.has_invalid_chars?(youtube_url) ⇒ Boolean
12 13 14 |
# File 'lib/youtube_rails.rb', line 12 def self.has_invalid_chars?(youtube_url) !INVALID_CHARS.match(youtube_url).nil? end |
.youtube_embed_url(youtube_url, width = 420, height = 315, options = {}) ⇒ Object
25 26 27 |
# File 'lib/youtube_rails.rb', line 25 def self.(youtube_url, width = 420, height = 315, = {}) %(<iframe width="#{width}" height="#{height}" src="#{ (youtube_url, ) }" frameborder="0" allowfullscreen></iframe>) end |
.youtube_embed_url_only(youtube_url, options = {}) ⇒ Object
39 40 41 42 |
# File 'lib/youtube_rails.rb', line 39 def self.(youtube_url, = {}) vid_id = extract_video_id(youtube_url) "http#{'s' if [:ssl]}://www.youtube.com/embed/#{ vid_id }#{'?rel=0' if [:suggestion]}" end |
.youtube_regular_url(youtube_url, options = {}) ⇒ Object
29 30 31 32 |
# File 'lib/youtube_rails.rb', line 29 def self.youtube_regular_url(youtube_url, = {}) vid_id = extract_video_id(youtube_url) "http#{'s' if [:ssl]}://www.youtube.com/watch?v=#{ vid_id }" end |
.youtube_shortened_url(youtube_url, options = {}) ⇒ Object
34 35 36 37 |
# File 'lib/youtube_rails.rb', line 34 def self.youtube_shortened_url(youtube_url, = {}) vid_id = extract_video_id(youtube_url) "http#{'s' if [:ssl]}://youtu.be/#{ vid_id }" end |