Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Handle multiple video providers

  • Loading branch information...
commit cbc6cbf223cb81f06b2b6ef240671d6bdc33d269 1 parent e055275
Brian Cardarella bcardarella authored
71 app/models/presentation.rb
... ... @@ -1,7 +1,7 @@
1 1 class Presentation < ActiveRecord::Base
2 2 validates :title, :presence => true
3 3 validates :description, :presence => true
4   - 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 }
  4 + 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 }
5 5 validates :presented_at, :presence => true
6 6 validates :presenter_name, :presence => true
7 7
@@ -9,9 +9,72 @@ def self.group_by_date
9 9 order('presented_at desc').group_by(&:presented_at)
10 10 end
11 11
  12 + VideoProviders = %w{youtube vimeo blip}
  13 + VideoDimensions = { :width => 625, :height => 370 }
  14 +
  15 + def video_url=(url)
  16 + if url.present?
  17 + self.video_provider = get_video_provider(url)
  18 + self.video_id = get_video_id(url)
  19 + end
  20 + end
  21 +
  22 + def video_url
  23 + if video_provider && video_id
  24 + self.send("#{video_provider}_url")
  25 + end
  26 + end
  27 +
12 28 def embed_video
13   -<<-HTML
14   -<iframe src="http://player.vimeo.com/video/#{vimeo_id}" width="625" height="370" frameborder="0"></iframe>
15   -HTML
  29 + self.send("embed_#{video_provider}")
  30 + end
  31 +
  32 + private
  33 +
  34 + def get_video_provider(url)
  35 + VideoProviders.detect { |provider| url.match(provider) }
  36 + end
  37 +
  38 + def get_video_id(url)
  39 + self.send("#{self.video_provider}_match", url)
  40 + end
  41 +
  42 + # Vimeo methods
  43 + def vimeo_match(url)
  44 + url.match(/http:\/\/(\w+\.)?vimeo.com\/(.+)/)[2]
  45 + end
  46 +
  47 + def vimeo_url
  48 + %{http://vimeo.com/#{video_id}}
  49 + end
  50 +
  51 + def embed_vimeo
  52 + %{<iframe src="http://player.vimeo.com/video/#{video_id}" width="#{VideoDimensions[:width]}" height="#{VideoDimensions[:height]}" frameborder="0"></iframe>}
  53 + end
  54 +
  55 + # Youtube methods
  56 + def youtube_match(url)
  57 + url.match(/http:\/\/www\.youtube\.com\/watch\/?\?v=([^&]+)/)[1]
  58 + end
  59 +
  60 + def youtube_url
  61 + %{http://www.youtube.com/watch?v=#{video_id}}
  62 + end
  63 +
  64 + def embed_youtube
  65 + %{<iframe width="#{VideoDimensions[:width]}" height="#{VideoDimensions[:height]}" src="http://www.youtube.com/embed/#{video_id}" frameborder="0" allowfullscreen></iframe>}
  66 + end
  67 +
  68 + # Blip.tv methods
  69 + def blip_match(url)
  70 + url.match(/http:\/\/blip\.tv\/play\/(.+)/)[1]
  71 + end
  72 +
  73 + def blip_url
  74 + %{http://blip.tv/play/#{video_id}}
  75 + end
  76 +
  77 + def embed_blip
  78 + %{<embed src="http://blip.tv/play/#{video_id}" type="application/x-shockwave-flash" width="#{VideoDimensions[:width]}" height="#{VideoDimensions[:height]}" allowscriptaccess="always" allowfullscreen="true"></embed>}
16 79 end
17 80 end
2  app/views/presentations/_presentation.html.erb
@@ -8,6 +8,6 @@
8 8 <% end %>
9 9 </span>
10 10 </h3>
11   - <%= presentation.embed_video.html_safe if presentation.vimeo_id.present? %>
  11 + <%= presentation.embed_video.html_safe if presentation.video_id.present? %>
12 12 <p><%= presentation.description %></p>
13 13 </li>
15 db/migrate/20110514143114_video_attrs_for_presentations.rb
... ... @@ -0,0 +1,15 @@
  1 +class VideoAttrsForPresentations < ActiveRecord::Migration
  2 + def self.up
  3 + add_column :presentations, :video_provider, :string
  4 + t = Presentation.arel_table
  5 + Presentation.where(t[:vimeo_id].not_eq(nil)).each do |presentation|
  6 + presentation.update_attribute(:video_provider, 'vimeo')
  7 + end
  8 + rename_column :presentations, :vimeo_id, :video_id
  9 + end
  10 +
  11 + def self.down
  12 + rename_column :presentations, :video_id, :vimeo_id
  13 + remove_column :presentations, :video_provider
  14 + end
  15 +end
5 db/schema.rb
@@ -10,17 +10,18 @@
10 10 #
11 11 # It's strongly recommended to check this file into your version control system.
12 12
13   -ActiveRecord::Schema.define(:version => 20110512173547) do
  13 +ActiveRecord::Schema.define(:version => 20110514143114) do
14 14
15 15 create_table "presentations", :force => true do |t|
16 16 t.date "presented_at"
17   - t.string "vimeo_id"
  17 + t.string "video_id"
18 18 t.string "title"
19 19 t.string "slides_url"
20 20 t.string "presenter_name"
21 21 t.text "description"
22 22 t.datetime "created_at"
23 23 t.datetime "updated_at"
  24 + t.string "video_provider"
24 25 end
25 26
26 27 end
3  spec/factories/presentation.rb
... ... @@ -1,7 +1,8 @@
1 1 Factory.define :presentation do |p|
2 2 p.title { "Test Presentation" }
3 3 p.presented_at { "May 10, 2011" }
4   - p.vimeo_id { "123" }
  4 + p.video_id { "123" }
  5 + p.video_provider { "vimeo" }
5 6 p.presenter_name { "Person 1" }
6 7 p.description { "Test Description" }
7 8 p.slides_url { "http://slides.com/1" }
28 spec/models/presentation_spec.rb
@@ -9,6 +9,7 @@
9 9 it { should_not have_valid(:presented_at).when(nil, '') }
10 10 it { should_not have_valid(:presenter_name).when(nil, '') }
11 11 end
  12 +
12 13 describe '.group_by_date' do
13 14 before do
14 15 @date_1 = Date.parse("May 10, 2011")
@@ -25,12 +26,27 @@
25 26 end
26 27 end
27 28
28   - describe '#embed_video' do
29   - before { @presentation = Presentation.new(:vimeo_id => "123") }
30   - it 'embeds the video' do
31   - @presentation.embed_video.should == <<-HTML
32   -<iframe src="http://player.vimeo.com/video/123" width="625" height="370" frameborder="0"></iframe>
33   - HTML
  29 + describe 'video' do
  30 + context 'vimeo' do
  31 + subject { Presentation.new(:video_url => 'http://vimeo.com/123') }
  32 + its(:video_provider) { should == 'vimeo' }
  33 + its(:video_id) { should == '123' }
  34 + its(:embed_video) { should == %{<iframe src="http://player.vimeo.com/video/123" width="625" height="370" frameborder="0"></iframe>} }
  35 + end
  36 + context 'youtube' do
  37 + subject { Presentation.new(:video_url => 'http://www.youtube.com/watch?v=123abc') }
  38 + its(:video_provider) { should == 'youtube' }
  39 + its(:video_id) { should == '123abc' }
  40 + its(:embed_video) { should == %{<iframe width="625" height="370" src="http://www.youtube.com/embed/123abc" frameborder="0" allowfullscreen></iframe>} }
  41 + end
  42 + context 'blip' do
  43 + # Blip.tv url must be taken from the embed code
  44 + # The regular video URL cannot be easily translated to embed code style
  45 + subject { Presentation.new(:video_url => 'http://blip.tv/play/123abc') }
  46 + its(:video_provider) { should == 'blip' }
  47 + its(:video_id) { should == '123abc' }
  48 + 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>} }
34 49 end
35 50 end
  51 +
36 52 end

0 comments on commit cbc6cbf

Please sign in to comment.
Something went wrong with that request. Please try again.