diff --git a/lib/youtube_titles.rb b/lib/youtube_titles.rb
index 760cb703b..8530e8ad3 100644
--- a/lib/youtube_titles.rb
+++ b/lib/youtube_titles.rb
@@ -20,5 +20,5 @@ module YoutubeTitles
end
end
- YOUTUBE_ID_REGEX = /(?:youtu\.be\/|(?:[a-z]{2,3}\.)?youtube\.com\/watch(?:\?|#!|.+&|.+&)v=)([\w-]{11})(?:\S*(#[^ ]+))?/im unless defined? YOUTUBE_ID_REGEX
+ YOUTUBE_ID_REGEX = /(?:https?:\/\/)(?:youtu\.be\/|(?:[a-z]{2,3}\.)?youtube\.com\/watch(?:\?|#!|.+&|.+&)v=)([\w-]{11})(?:\S*(#[^ ]+)|\S+)?/im unless defined? YOUTUBE_ID_REGEX
end
diff --git a/spec/lib/youtube_titles_spec.rb b/spec/lib/youtube_titles_spec.rb
index d9c82a84f..82e6ec107 100644
--- a/spec/lib/youtube_titles_spec.rb
+++ b/spec/lib/youtube_titles_spec.rb
@@ -1,29 +1,35 @@
require 'spec_helper'
require 'youtube_titles'
+
describe YoutubeTitles do
+ include YoutubeTitles
+
before do
@video_id = "ABYnqp-bxvg"
@url="http://www.youtube.com/watch?v=#{@video_id}&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
@api_path = "/feeds/api/videos/#{@video_id}?v=2"
end
- include YoutubeTitles
+
describe '#youtube_title_for' do
before do
@expected_title = "UP & down & UP & down &"
@mock_http = mock("http")
Net::HTTP.stub!(:new).with('gdata.youtube.com', 80).and_return(@mock_http)
end
+
it 'gets a youtube title corresponding to an id' do
@mock_http.should_receive(:get).with(@api_path, nil).and_return(
[nil, "Foobar
#{@expected_title} hallo welt dsd"])
youtube_title_for(@video_id).should == @expected_title
end
+
it 'returns a fallback for videos with no title' do
@mock_http.should_receive(:get).with(@api_path, nil).and_return(
[nil, "Foobar #{@expected_title} hallo welt dsd"])
youtube_title_for(@video_id).should == I18n.t('application.helper.video_title.unknown')
end
end
+
describe 'serialization and marshalling' do
before do
@expected_title = '""Procrastination"" Tales Of Mere Existence'
@@ -33,10 +39,34 @@ describe YoutubeTitles do
[nil, "Foobar #{@expected_title} hallo welt dsd"])
@post = Factory.create(:status_message, :text => @url)
end
+
it 'can be re-marshalled' do
lambda {
StatusMessage.find(@post.id).youtube_titles
}.should_not raise_error
end
end
+
+ describe "YOUTUBE_ID_REGEX" do
+ specify "normal url" do
+ url = "http://www.youtube.com/watch?v=dQw4w9WgXcQ"
+ matched_data = url.match(YoutubeTitles::YOUTUBE_ID_REGEX)
+ matched_data[0].should == url
+ matched_data[1].should == "dQw4w9WgXcQ"
+ end
+
+ specify "https url" do
+ url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
+ matched_data = url.match(YoutubeTitles::YOUTUBE_ID_REGEX)
+ matched_data[0].should == url
+ matched_data[1].should == "dQw4w9WgXcQ"
+ end
+
+ specify "url with extra query params" do
+ url = "http://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=related"
+ matched_data = url.match(YoutubeTitles::YOUTUBE_ID_REGEX)
+ matched_data[0].should == url
+ matched_data[1].should == "dQw4w9WgXcQ"
+ end
+ end
end