Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Handle multiple video providers

  • Loading branch information...
commit cbc6cbf223cb81f06b2b6ef240671d6bdc33d269 1 parent e055275
@bcardarella bcardarella authored
View
71 app/models/presentation.rb
@@ -1,7 +1,7 @@
class Presentation < ActiveRecord::Base
validates :title, :presence => true
validates :description, :presence => true
- validates :slides_url, :format => { :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix, :allow_blank => true }
+ validates :slides_url, :video_url, :format => { :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix, :allow_blank => true }
validates :presented_at, :presence => true
validates :presenter_name, :presence => true
@@ -9,9 +9,72 @@ def self.group_by_date
order('presented_at desc').group_by(&:presented_at)
end
+ VideoProviders = %w{youtube vimeo blip}
+ VideoDimensions = { :width => 625, :height => 370 }
+
+ def video_url=(url)
+ if url.present?
+ self.video_provider = get_video_provider(url)
+ self.video_id = get_video_id(url)
+ end
+ end
+
+ def video_url
+ if video_provider && video_id
+ self.send("#{video_provider}_url")
+ end
+ end
+
def embed_video
-<<-HTML
-<iframe src="http://player.vimeo.com/video/#{vimeo_id}" width="625" height="370" frameborder="0"></iframe>
-HTML
+ self.send("embed_#{video_provider}")
+ end
+
+ private
+
+ def get_video_provider(url)
+ VideoProviders.detect { |provider| url.match(provider) }
+ end
+
+ def get_video_id(url)
+ self.send("#{self.video_provider}_match", url)
+ end
+
+ # Vimeo methods
+ def vimeo_match(url)
+ url.match(/http:\/\/(\w+\.)?vimeo.com\/(.+)/)[2]
+ end
+
+ def vimeo_url
+ %{http://vimeo.com/#{video_id}}
+ end
+
+ def embed_vimeo
+ %{<iframe src="http://player.vimeo.com/video/#{video_id}" width="#{VideoDimensions[:width]}" height="#{VideoDimensions[:height]}" frameborder="0"></iframe>}
+ end
+
+ # Youtube methods
+ def youtube_match(url)
+ url.match(/http:\/\/www\.youtube\.com\/watch\/?\?v=([^&]+)/)[1]
+ end
+
+ def youtube_url
+ %{http://www.youtube.com/watch?v=#{video_id}}
+ end
+
+ def embed_youtube
+ %{<iframe width="#{VideoDimensions[:width]}" height="#{VideoDimensions[:height]}" src="http://www.youtube.com/embed/#{video_id}" frameborder="0" allowfullscreen></iframe>}
+ end
+
+ # Blip.tv methods
+ def blip_match(url)
+ url.match(/http:\/\/blip\.tv\/play\/(.+)/)[1]
+ end
+
+ def blip_url
+ %{http://blip.tv/play/#{video_id}}
+ end
+
+ def embed_blip
+ %{<embed src="http://blip.tv/play/#{video_id}" type="application/x-shockwave-flash" width="#{VideoDimensions[:width]}" height="#{VideoDimensions[:height]}" allowscriptaccess="always" allowfullscreen="true"></embed>}
end
end
View
2  app/views/presentations/_presentation.html.erb
@@ -8,6 +8,6 @@
<% end %>
</span>
</h3>
- <%= presentation.embed_video.html_safe if presentation.vimeo_id.present? %>
+ <%= presentation.embed_video.html_safe if presentation.video_id.present? %>
<p><%= presentation.description %></p>
</li>
View
15 db/migrate/20110514143114_video_attrs_for_presentations.rb
@@ -0,0 +1,15 @@
+class VideoAttrsForPresentations < ActiveRecord::Migration
+ def self.up
+ add_column :presentations, :video_provider, :string
+ t = Presentation.arel_table
+ Presentation.where(t[:vimeo_id].not_eq(nil)).each do |presentation|
+ presentation.update_attribute(:video_provider, 'vimeo')
+ end
+ rename_column :presentations, :vimeo_id, :video_id
+ end
+
+ def self.down
+ rename_column :presentations, :video_id, :vimeo_id
+ remove_column :presentations, :video_provider
+ end
+end
View
5 db/schema.rb
@@ -10,17 +10,18 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110512173547) do
+ActiveRecord::Schema.define(:version => 20110514143114) do
create_table "presentations", :force => true do |t|
t.date "presented_at"
- t.string "vimeo_id"
+ t.string "video_id"
t.string "title"
t.string "slides_url"
t.string "presenter_name"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
+ t.string "video_provider"
end
end
View
3  spec/factories/presentation.rb
@@ -1,7 +1,8 @@
Factory.define :presentation do |p|
p.title { "Test Presentation" }
p.presented_at { "May 10, 2011" }
- p.vimeo_id { "123" }
+ p.video_id { "123" }
+ p.video_provider { "vimeo" }
p.presenter_name { "Person 1" }
p.description { "Test Description" }
p.slides_url { "http://slides.com/1" }
View
28 spec/models/presentation_spec.rb
@@ -9,6 +9,7 @@
it { should_not have_valid(:presented_at).when(nil, '') }
it { should_not have_valid(:presenter_name).when(nil, '') }
end
+
describe '.group_by_date' do
before do
@date_1 = Date.parse("May 10, 2011")
@@ -25,12 +26,27 @@
end
end
- describe '#embed_video' do
- before { @presentation = Presentation.new(:vimeo_id => "123") }
- it 'embeds the video' do
- @presentation.embed_video.should == <<-HTML
-<iframe src="http://player.vimeo.com/video/123" width="625" height="370" frameborder="0"></iframe>
- HTML
+ describe 'video' do
+ context 'vimeo' do
+ subject { Presentation.new(:video_url => 'http://vimeo.com/123') }
+ its(:video_provider) { should == 'vimeo' }
+ its(:video_id) { should == '123' }
+ its(:embed_video) { should == %{<iframe src="http://player.vimeo.com/video/123" width="625" height="370" frameborder="0"></iframe>} }
+ end
+ context 'youtube' do
+ subject { Presentation.new(:video_url => 'http://www.youtube.com/watch?v=123abc') }
+ its(:video_provider) { should == 'youtube' }
+ its(:video_id) { should == '123abc' }
+ its(:embed_video) { should == %{<iframe width="625" height="370" src="http://www.youtube.com/embed/123abc" frameborder="0" allowfullscreen></iframe>} }
+ end
+ context 'blip' do
+ # Blip.tv url must be taken from the embed code
+ # The regular video URL cannot be easily translated to embed code style
+ subject { Presentation.new(:video_url => 'http://blip.tv/play/123abc') }
+ its(:video_provider) { should == 'blip' }
+ its(:video_id) { should == '123abc' }
+ its(:embed_video) { should == %{<embed src="http://blip.tv/play/123abc" type="application/x-shockwave-flash" width="625" height="370" allowscriptaccess="always" allowfullscreen="true"></embed>} }
end
end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.