#1019 comment/post handles multiple youtube links properly
This commit is contained in:
parent
f6e4be35a1
commit
cdf94a0275
3 changed files with 21 additions and 17 deletions
|
|
@ -9,14 +9,16 @@ module YoutubeTitles
|
||||||
end
|
end
|
||||||
title || I18n.t('application.helper.video_title.unknown')
|
title || I18n.t('application.helper.video_title.unknown')
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_youtube_title text
|
def get_youtube_title text
|
||||||
|
youtube_match = text.enum_for(:scan, YOUTUBE_ID_REGEX).map { Regexp.last_match }
|
||||||
|
return if youtube_match.empty?
|
||||||
|
|
||||||
self.youtube_titles ||= {}
|
self.youtube_titles ||= {}
|
||||||
youtube_match = text.match(YOUTUBE_ID_REGEX)
|
youtube_match.each do |match_data|
|
||||||
return unless youtube_match
|
self.youtube_titles[match_data[1]] = CGI::escape(youtube_title_for(match_data[1]))
|
||||||
video_id = youtube_match[1]
|
|
||||||
unless self.youtube_titles[video_id]
|
|
||||||
self.youtube_titles[video_id] = CGI::escape(youtube_title_for(video_id))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
YOUTUBE_ID_REGEX = /youtube\.com.*?v=([A-Za-z0-9_\\\-]+)/ unless defined? YOUTUBE_ID_REGEX
|
|
||||||
|
YOUTUBE_ID_REGEX = /(?:youtu\.be\/|(?:[a-z]{2,3}\.)?youtube\.com\/watch(?:\?|#!|.+&|.+&)v=)([\w-]{11})/im unless defined? YOUTUBE_ID_REGEX
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -139,13 +139,13 @@ describe ApplicationHelper do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "recognizes multiple links of different types" do
|
it "recognizes multiple links of different types" do
|
||||||
message = "http:// Hello World, this is for www.joindiaspora.com and not for http://www.google.com though their Youtube service is neat, take http://www.youtube.com/watch?v=foobar or www.youtube.com/watch?foo=bar&v=BARFOO&whatever=related It is a good idea we finally have youtube, so enjoy this video http://www.youtube.com/watch?v=rickrolld"
|
message = "http:// Hello World, this is for www.joindiaspora.com and not for http://www.google.com though their Youtube service is neat, take http://www.youtube.com/watch?v=foobar----- or www.youtube.com/watch?foo=bar&v=BARFOO-----&whatever=related It is a good idea we finally have youtube, so enjoy this video http://www.youtube.com/watch?v=rickrolld--"
|
||||||
res = markdownify(message)
|
res = markdownify(message)
|
||||||
res.should =~ /a target=\"_blank\" href=\"http:\/\/www.joindiaspora.com\"/
|
res.should =~ /a target=\"_blank\" href=\"http:\/\/www.joindiaspora.com\"/
|
||||||
res.should =~ /a target=\"_blank\" href=\"http:\/\/www.google.com\"/
|
res.should =~ /a target=\"_blank\" href=\"http:\/\/www.google.com\"/
|
||||||
res.should =~ /data-video-id="foobar"/
|
res.should =~ /data-video-id="foobar-----"/
|
||||||
res.should =~ /data-video-id="BARFOO"/
|
res.should =~ /data-video-id="BARFOO-----"/
|
||||||
res.should =~ /data-video-id="rickrolld"/
|
res.should =~ /data-video-id="rickrolld--"/
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should recognize basic ftp links" do
|
it "should recognize basic ftp links" do
|
||||||
|
|
|
||||||
|
|
@ -92,20 +92,22 @@ describe Comment do
|
||||||
before do
|
before do
|
||||||
@message = alice.post :status_message, :text => "hi", :to => @alices_aspect.id
|
@message = alice.post :status_message, :text => "hi", :to => @alices_aspect.id
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should process youtube titles on the way in' do
|
it 'should process youtube titles on the way in' do
|
||||||
video_id = "ABYnqp-bxvg"
|
first_video_id = "ABYnqp-1111"
|
||||||
url="http://www.youtube.com/watch?v=#{video_id}&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
|
second_video_id = "ABYnqp-2222"
|
||||||
expected_title = "UP & down & UP & down &"
|
url = "http://www.youtube.com/watch?v=#{first_video_id} http://www.youtube.com/watch?v=#{second_video_id}"
|
||||||
|
expected_title = "UP & down & UP & down &"
|
||||||
|
|
||||||
mock_http = mock("http")
|
mock_http = mock("http")
|
||||||
Net::HTTP.stub!(:new).with('gdata.youtube.com', 80).and_return(mock_http)
|
Net::HTTP.stub!(:new).with('gdata.youtube.com', 80).twice.and_return(mock_http)
|
||||||
mock_http.should_receive(:get).with('/feeds/api/videos/'+video_id+'?v=2', nil).and_return(
|
mock_http.should_receive(:get).with(/\/feeds\/api\/videos/, nil).twice.and_return(
|
||||||
[nil, 'Foobar <title>'+expected_title+'</title> hallo welt <asd><dasdd><a>dsd</a>'])
|
[nil, 'Foobar <title>'+expected_title+'</title> hallo welt <asd><dasdd><a>dsd</a>'])
|
||||||
|
|
||||||
comment = alice.build_comment url, :on => @message
|
comment = alice.build_comment url, :on => @message
|
||||||
|
|
||||||
comment.save!
|
comment.save!
|
||||||
Comment.find(comment.id).youtube_titles.should == {video_id => CGI::escape(expected_title)}
|
|
||||||
|
Comment.find(comment.id).youtube_titles.should == { first_video_id => CGI::escape(expected_title), second_video_id => CGI::escape(expected_title) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue