youtube-model icon indicating copy to clipboard operation
youtube-model copied to clipboard

Allows you to generate an ActiveResource model ready to interact with the You Tube API.

h1. YouTube Model

This plugin allows you to interact with the new YouTube API (yes, that means uploading is now featured) through a simple ActiveResource model. The uploaded video can also be updated and deleted. The changes will be reflected at the youtube.

UPDATE: This fork allow youtube model to work as an active resource like. Allowing validation, callback, etc.. The project is steel on going on my side, some things may still not work properly, but the main functionality (upload, update, remote video collection retrievals) should work properly)

h2. Documentation

h3. Installing

rails plugin install git://github.com/itkin/youtube-model.git

h3. Generating the system

The plugin includes a generator to create a YouTubeModel based on ActiveResource

rails g youtube_model ModelName

This generator will create four files:

  • An YouTubeModel inherited from ActiveResource::Base under app/models directory.
  • A yaml configuration file under config directory.

And that's all, but be sure to check the configuration file to set up your Developer and Client Keys. You can get these keys at the "YouTube APIs and Tools":http://code.google.com/apis/youtube/dashboard/ page.

h3. AuthSub for web applications

A link is provided to authorise the website to access to the logged in user's youtube account.

The token obtained after doing authorisation process will become a session token. So we will set the session parameter to 1 in yaml	configuration file. This token is used to upload, edit and delete video.

  #app/views/videos/index.html.erb
	Please Click  first to authorise access to your youtube account.
	
An authorise method will be added to make the single use token to a session token.

  #VideosController
  def authorise
    client = GData::Client::DocList.new
    if params[:token] 
      client.authsub_token = params[:token] # extract the single-use token from the URL query params
      session[:token] = client.auth_handler.upgrade() 
      client.authsub_token = session[:token] if session[:token]
    end
    redirect_to videos_path
  end
	
Also add 'include GData' in the videos controller, which includes the module GData for authorisation process. 

<pre><code>

#config/environment.rb config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net" config.gem 'gdata', :lib => 'gdata'

h3. Listing videos

Example

All the api finders (except find_by_id) render collections of youtube_model resources. The collection are basically an aray of objects, extended with the following 3 accessors:

  • start_index

  • items_per_page

  • total_results You can pass any youtube argument to the api finders, or set them as default in your youtube model class

    
    class Video  10 }
    end
    
    Video.top_rated(:startIndex => 10, :itemPerPage => 30)
    Video.uploaded_by('nicolas')
    

@uploaded_by_user@ requires authentication and @find_by_id@ needs it optionally; To use them you have to pass the session[:token] we saved during authorisation process Accessing the API with theses finders gives you private information about video status (also accessible via @video.status(:token => session[:token])


  Video.uploaded_by_user(:token => session[:token])
  Video.find_by_id(video_id,:token => session[:token])
  

  #VideosController
  def index
    @videos= Video.uploaded_by_user(:token => session[:token])
  end
  
  def show
    @video = Video.find_by_id(params[:id], :token => session[:token])
    flash[:message] = "Sorry the video is not found at Youtube" and redirect_to videos_path unless @video
  end
	

  #app/views/videos/index.html.erb

  

  
     in 
    by 

     100) %>
     video.title), video_path(video) %>
    
    
	   "delete" %>
  
  

  #app/views/videos/show.html.erb
	
	by 
  
   
  
  
   "delete" %>
  

As you can see, the @youtube_embed@ method is used to display a video.

h3. Uploading videos

To upload a new video to youtube, just instanciate the generated class you inherited from youtube model with a token param and save it


  #routes.rb
  map.resources :videos, :collection => {:authorise => :get }

  #VideosController
  def new
    @categories ||= YouTube.video_categories
    @video = Video.new(:token => session[:token])
  end

  def create
    @video = video.new(params[:video])
    unless @video.save
      render :action => :new
    end
  end
  

h3. Updating Videos

To get the videos updated by a user, find it with find_by_id. You must pass a token option with the google api authsub token in order retrieve a video event it hasn't been processed or accepted by youtube (have a look here http://code.google.com/intl/fr/apis/youtube/2.0/developers_guide_protocol_video_entries.html)


  #VideosController
  def edit
    @video = Video.find_by_id(params[:id], :token => session[:token])
  end
  def update
    @video.find_by_id(params[:id
    if @video.update_attributes(params[:video])
      render :action => :update
    else
      render :action => :edit
    end
  end
  

#app/views/videos/edit.html.erb <% form_for @youtube, :url => video_url do |f| %> <%= f.label :title %> <%= f.text_field :title %>

<%= f.label :description %>
<%= f.text_area :description, :rows => 10 %>

<%= f.label :category %>
<%= f.select :category, @categories %>

<%= f.label :keywords %>
  <%= f.text_field :keywords %>

<%= f.submit 'Update video' %>

<% end %> <%= link_to 'Back', videos_path %>

h3. Deleting Videos

To delete the videos of any user you can use the destroy method on a video instance

@video.destroy@


  #VideosController

  def destroy  
    @video = Video.find_by_id(params[:id], :token => session[:token])
    if @video.destroy
      flash[:message] = "Video has been sucessfully deleted." 
    else
      flash[:message] = "Sorry the video has not been deleted."
    end 
    redirect_to videos_path
  end
  

h2. Feedback

I'll really appreciate your feedback, please contact me at vibha[at]vinsol.com

h2. License

This code is released under Creative Commons Attribution-Share Alike 3.0 license.

!http://i.creativecommons.org/l/by-sa/3.0/88x31.png!:http://creativecommons.org/licenses/by-sa/3.0/