Commit a95e0de0e4c413f0712a8cd5500f5970ff84b7fb
1 parent
a5c0c0f3
Exists in
master
and in
29 other branches
video: fix youtube id format
Showing
2 changed files
with
12 additions
and
3 deletions
Show diff stats
plugins/video/lib/video_block.rb
| @@ -4,8 +4,10 @@ class VideoBlock < Block | @@ -4,8 +4,10 @@ class VideoBlock < Block | ||
| 4 | settings_items :width, :type => :integer, :default => 400 | 4 | settings_items :width, :type => :integer, :default => 400 |
| 5 | settings_items :height, :type => :integer, :default => 315 | 5 | settings_items :height, :type => :integer, :default => 315 |
| 6 | 6 | ||
| 7 | + YOUTUBE_ID_FORMAT = 'a-zA-Z0-9_-' | ||
| 8 | + | ||
| 7 | def is_youtube? | 9 | def is_youtube? |
| 8 | - url.match(/.*(youtube.com.*v=[[:alnum:]]+|youtu.be\/[[:alnum:]]+).*/) ? true : false | 10 | + url.match(/.*(youtube.com.*v=[#{YOUTUBE_ID_FORMAT}]+|youtu.be\/[#{YOUTUBE_ID_FORMAT}]+).*/) ? true : false |
| 9 | end | 11 | end |
| 10 | 12 | ||
| 11 | def is_vimeo? | 13 | def is_vimeo? |
| @@ -44,8 +46,8 @@ class VideoBlock < Block | @@ -44,8 +46,8 @@ class VideoBlock < Block | ||
| 44 | 46 | ||
| 45 | def extract_youtube_id | 47 | def extract_youtube_id |
| 46 | return nil unless is_youtube? | 48 | return nil unless is_youtube? |
| 47 | - youtube_match = url.match('v=([[:alnum:]]*)') | ||
| 48 | - youtube_match ||= url.match('youtu.be\/([[:alnum:]]*)') | 49 | + youtube_match = url.match("v=([#{YOUTUBE_ID_FORMAT}]*)") |
| 50 | + youtube_match ||= url.match("youtu.be\/([#{YOUTUBE_ID_FORMAT}]*)") | ||
| 49 | youtube_match[1] unless youtube_match.nil? | 51 | youtube_match[1] unless youtube_match.nil? |
| 50 | end | 52 | end |
| 51 | 53 |
plugins/video/test/unit/video_block_test.rb
| @@ -70,6 +70,13 @@ class VideoBlockTest < ActiveSupport::TestCase | @@ -70,6 +70,13 @@ class VideoBlockTest < ActiveSupport::TestCase | ||
| 70 | assert_equal id, block.send('extract_youtube_id') | 70 | assert_equal id, block.send('extract_youtube_id') |
| 71 | end | 71 | end |
| 72 | 72 | ||
| 73 | + should "extract youtube id from youtube video url's if it has underline and hyphen" do | ||
| 74 | + block = VideoBlock.new | ||
| 75 | + id = 'oi43_re-d2' | ||
| 76 | + block.url = "youtube.com/?v=#{id}" | ||
| 77 | + assert_equal id, block.send('extract_youtube_id') | ||
| 78 | + end | ||
| 79 | + | ||
| 73 | should "extract youtube id from youtube video url's if it's a valid youtube short url" do | 80 | should "extract youtube id from youtube video url's if it's a valid youtube short url" do |
| 74 | block = VideoBlock.new | 81 | block = VideoBlock.new |
| 75 | id = 'oi43jre2d2' | 82 | id = 'oi43jre2d2' |