Skip to content
Extraits de code Groupes Projets
Valider 2282fd35 rédigé par Maxwell Salzberg's avatar Maxwell Salzberg
Parcourir les fichiers

kill no longer used youtube titles code with fire

parent a5d1657f
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -4,8 +4,6 @@ ...@@ -4,8 +4,6 @@
class Comment < ActiveRecord::Base class Comment < ActiveRecord::Base
require File.join(Rails.root, 'lib/diaspora/web_socket') require File.join(Rails.root, 'lib/diaspora/web_socket')
require File.join(Rails.root, 'lib/youtube_titles')
include YoutubeTitles
include ROXML include ROXML
include Diaspora::Webhooks include Diaspora::Webhooks
...@@ -30,7 +28,6 @@ class Comment < ActiveRecord::Base ...@@ -30,7 +28,6 @@ class Comment < ActiveRecord::Base
validates :text, :presence => true, :length => { :maximum => 2500 } validates :text, :presence => true, :length => { :maximum => 2500 }
validates :parent, :presence => true #should be in relayable (pending on fixing Message) validates :parent, :presence => true #should be in relayable (pending on fixing Message)
serialize :youtube_titles, Hash
scope :including_author, includes(:author => :profile) scope :including_author, includes(:author => :profile)
......
...@@ -6,8 +6,6 @@ class StatusMessage < Post ...@@ -6,8 +6,6 @@ class StatusMessage < Post
include Diaspora::Socketable include Diaspora::Socketable
include Diaspora::Taggable include Diaspora::Taggable
include YoutubeTitles
require File.join(Rails.root, 'lib/youtube_titles')
include ActionView::Helpers::TextHelper include ActionView::Helpers::TextHelper
include PeopleHelper include PeopleHelper
...@@ -26,7 +24,6 @@ class StatusMessage < Post ...@@ -26,7 +24,6 @@ class StatusMessage < Post
attr_accessible :text, :provider_display_name attr_accessible :text, :provider_display_name
attr_accessor :oembed_url attr_accessor :oembed_url
serialize :youtube_titles, Hash
after_create :create_mentions after_create :create_mentions
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
= person_link(comment.author, :class => "hovercardable") = person_link(comment.author, :class => "hovercardable")
%span{:class => [direction_for(comment.text), 'collapsible']} %span{:class => [direction_for(comment.text), 'collapsible']}
= markdownify(comment, :oembed => true, :youtube_maps => comment.youtube_titles) = markdownify(comment, :oembed => true)
.comment_info .comment_info
%time.timeago{:datetime => comment.created_at} %time.timeago{:datetime => comment.created_at}
......
...@@ -12,5 +12,5 @@ ...@@ -12,5 +12,5 @@
= comment.created_at ? time_ago_in_words(comment.created_at) : time_ago_in_words(Time.now) = comment.created_at ? time_ago_in_words(comment.created_at) : time_ago_in_words(Time.now)
%div{:class => direction_for(comment.text)} %div{:class => direction_for(comment.text)}
= markdownify(comment, :youtube_maps => comment[:youtube_titles]) = markdownify(comment)
...@@ -16,4 +16,4 @@ ...@@ -16,4 +16,4 @@
= link_to (image_tag photo.url(:thumb_small), :class => 'stream-photo thumb_small', 'data-small-photo' => photo.url(:thumb_medium), 'data-full-photo' => photo.url), photo_path(photo), :class => 'stream-photo-link' = link_to (image_tag photo.url(:thumb_small), :class => 'stream-photo thumb_small', 'data-small-photo' => photo.url(:thumb_medium), 'data-full-photo' => photo.url), photo_path(photo), :class => 'stream-photo-link'
%div{:class => direction_for(post.text)} %div{:class => direction_for(post.text)}
!= markdownify(post, :youtube_maps => post[:youtube_titles]) != markdownify(post)
...@@ -16,6 +16,6 @@ ...@@ -16,6 +16,6 @@
= link_to (image_tag photo.url(:thumb_small), :class => 'stream-photo thumb_small', 'data-small-photo' => photo.url(:thumb_medium), 'data-full-photo' => photo.url), photo_path(photo), :class => 'stream-photo-link' = link_to (image_tag photo.url(:thumb_small), :class => 'stream-photo thumb_small', 'data-small-photo' => photo.url(:thumb_medium), 'data-full-photo' => photo.url), photo_path(photo), :class => 'stream-photo-link'
%div{:class => [direction_for(post.text), 'collapsible']} %div{:class => [direction_for(post.text), 'collapsible']}
!= markdownify(post, :youtube_maps => post[:youtube_titles]) != markdownify(post)
- if post.o_embed_cache_id.present? - if post.o_embed_cache_id.present?
= o_embed_html(post.o_embed_cache) = o_embed_html(post.o_embed_cache)
...@@ -15,4 +15,4 @@ ...@@ -15,4 +15,4 @@
= image_tag post.image_url = image_tag post.image_url
%div{:class => direction_for(post.text)} %div{:class => direction_for(post.text)}
!= markdownify(post, :youtube_maps => post[:youtube_titles]) != markdownify(post)
module YoutubeTitles
def self.included(model)
model.class_eval do
before_save do
get_youtube_title text
end
end if model.respond_to?(:before_save)
end
def youtube_title_for video_id
http = Net::HTTP.new('gdata.youtube.com', 80)
path = "/feeds/api/videos/#{video_id}?v=2"
resp, data = http.get(path)
title = data.match(/<title>(.*)<\/title>/)
unless title.nil?
title = title.to_s[7..-9]
end
title || I18n.t('application.helper.video_title.unknown')
end
def get_youtube_title text
self.youtube_titles = {}
youtube_match = text.enum_for(:scan, YOUTUBE_ID_REGEX).map { Regexp.last_match }
return if youtube_match.empty?
matches = {}
youtube_match.each do |match_data|
matches[match_data[1]] = CGI::escape(youtube_title_for(match_data[1]))
end
self.youtube_titles = matches unless matches.empty?
end
def unserialize_attribute attr_name
if attr_name == "youtube_titles"
begin
super
rescue ActiveRecord::SerializationTypeMismatch
{}
end
else
super
end
end
YOUTUBE_ID_REGEX = /(?:https?:\/\/)(?:youtu\.be\/|(?:[a-z]{2,3}\.)?youtube\.com\/watch(?:\?|#!|.+&|.+&amp;)v=)([\w-]{11})(?:\S*(#[^ ]+)|\S+)?/im unless defined? YOUTUBE_ID_REGEX
end
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
describe '#youtube_title_for' do
before do
@expected_title = "UP & down & UP & down &amp;"
@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).and_return(
[nil, "Foobar <title>#{@expected_title}</title> hallo welt <asd><dasdd><a>dsd</a>"])
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).and_return(
[nil, "Foobar #{@expected_title}</title> hallo welt <asd><dasdd><a>dsd</a>"])
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'
mock_http = mock("http")
Net::HTTP.stub!(:new).with('gdata.youtube.com', 80).and_return(mock_http)
mock_http.should_receive(:get).with(@api_path).and_return(
[nil, "Foobar <title>#{@expected_title}</title> hallo welt <asd><dasdd><a>dsd</a>"])
@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
it 'can be re-marshalled if it is serializaed incorrectly' do
StatusMessage.where(:id => @post.id).update_all(:youtube_titles => "this is not yaml")
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
...@@ -99,29 +99,6 @@ describe Comment do ...@@ -99,29 +99,6 @@ describe Comment do
end end
end end
# NOTE(move this to the youtube module spec)
describe 'youtube' do
before do
@message = alice.post :status_message, :text => "hi", :to => @alices_aspect.id
end
it 'should process youtube titles on the way in' do
first_video_id = "ABYnqp-1111"
second_video_id = "ABYnqp-2222"
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 &amp;"
mock_http = 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/).twice.and_return(
[nil, 'Foobar <title>'+expected_title+'</title> hallo welt <asd><dasdd><a>dsd</a>'])
comment = alice.build_comment :text => url, :post => @message
comment.save!
Comment.find(comment.id).youtube_titles.should == { first_video_id => CGI::escape(expected_title), second_video_id => CGI::escape(expected_title) }
end
end
describe 'it is relayable' do describe 'it is relayable' do
before do before do
......
...@@ -275,23 +275,6 @@ STR ...@@ -275,23 +275,6 @@ STR
end end
end end
describe 'youtube' do
it 'should process youtube titles on the way in' do
video_id = "ABYnqp-bxvg"
url="http://www.youtube.com/watch?v=#{video_id}&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
expected_title = "UP & down & UP & down &amp;"
mock_http = mock("http")
Net::HTTP.stub!(:new).with('gdata.youtube.com', 80).and_return(mock_http)
mock_http.should_receive(:get).with('/feeds/api/videos/'+video_id+'?v=2').and_return(
[nil, 'Foobar <title>'+expected_title+'</title> hallo welt <asd><dasdd><a>dsd</a>'])
post = @user.build_post :status_message, :text => url, :to => @aspect.id
post.save!
Post.find(post.id).youtube_titles.should == {video_id => CGI::escape(expected_title)}
end
end
describe '#after_dispatch' do describe '#after_dispatch' do
before do before do
@photos = [alice.build_post(:photo, :pending => true, :user_file=> File.open(photo_fixture_name)), @photos = [alice.build_post(:photo, :pending => true, :user_file=> File.open(photo_fixture_name)),
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter