Skip to content

Commit

Permalink
Adding support for oEmbed format embedding.
Browse files Browse the repository at this point in the history
  • Loading branch information
bamnet committed Aug 5, 2009
1 parent 70d64a4 commit 6849143
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 0 deletions.
15 changes: 15 additions & 0 deletions app/controllers/services_controller.rb
@@ -0,0 +1,15 @@
class ServicesController < ApplicationController

def oembed
id = params[:url].scan(/\/(\d+)$/i).first.first
@video = Video.find(id)

@thumbnail = @video.thumbnails.first
@thumb_size = @thumbnail.guess_size('thumb')

respond_to do |format|
format.xml # oembed.xml.erb
format.js # oembed.js.erb
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/services_helper.rb
@@ -0,0 +1,2 @@
module ServicesHelper
end
22 changes: 22 additions & 0 deletions app/models/thumbnail.rb
Expand Up @@ -9,4 +9,26 @@ class Thumbnail < ActiveRecord::Base

default_scope select_without_file_columns_for(:image)

def guess_size(style = '')
if(style == 'thumb' || style == 'small')
if(style == 'thumb')
max = 75
elsif(style == 'small')
max = 150
else
max = 0
end
#Compute how big the thumbnail should be
if self.video.width >= self.video.height
width = max
height = (self.video.height.to_f / self.video.width.to_f * width).to_i
else
height = max
width = (self.video.width.to_f / self.video.height.to_f * height).to_i
end
return {:width => width, :height => height}
else
return {:width => self.video.width, :height => self.video.height}
end
end
end
15 changes: 15 additions & 0 deletions app/views/services/oembed.js.erb
@@ -0,0 +1,15 @@
<% data = {:type => 'video',
:version => 1.0,
:provider_name => 'Bonsai Video',
:provider_url => url_for(:controller => 'videos', :action => 'index', :only_path => false),
:title => @video.name,
:html => (render :partial => 'videos/playback/handler', :locals => {:video => @video}),
:width => @video.width,
:height => @video.height,
:duration => @video.duration,
:thumbnail_url => path_to_url(@thumbnail.image.url(:thumb)),
:thumbnail_width => @thumb_size[:width],
:thumbnail_height => @thumb_size[:height],
:video_id => @video.id
}
<%= "#{params[:callback]}(" if params[:callback]%><%= data.to_json %><%= ")" if params[:callback] %>
14 changes: 14 additions & 0 deletions app/views/services/oembed.xml.erb
@@ -0,0 +1,14 @@
<% data = {:type => 'video',
:version => 1.0,
:provider_name => 'Bonsai Video',
:provider_url => url_for(:controller => 'videos', :action => 'index', :only_path => false),
:title => @video.name,
:html => (render :partial => 'videos/playback/handler', :locals => {:video => @video}),
:width => @video.width,
:height => @video.height,
:duration => @video.duration,
:thumbnail_url => path_to_url(@thumbnail.image.url(:thumb)),
:thumbnail_width => @thumb_size[:width],
:thumbnail_height => @thumb_size[:height],
:video_id => @video.id
} %><%= data.to_xml({:root => 'oembed'}) %>
2 changes: 2 additions & 0 deletions config/routes.rb
Expand Up @@ -5,10 +5,12 @@

map.resources :profiles


map.root :controller => "videos"

#Exposes the video download interface
map.connect ':controller/:action/:id/:filename.:format'
map.connect ':controller/:action.:format'

#Regular routes
map.connect ':controller/:action/:id'
Expand Down
8 changes: 8 additions & 0 deletions test/functional/services_controller_test.rb
@@ -0,0 +1,8 @@
require 'test_helper'

class ServicesControllerTest < ActionController::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
4 changes: 4 additions & 0 deletions test/unit/helpers/services_helper_test.rb
@@ -0,0 +1,4 @@
require 'test_helper'

class ServicesHelperTest < ActionView::TestCase
end

0 comments on commit 6849143

Please sign in to comment.