Skip to content
This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
edgarjs edited this page Sep 14, 2010 · 3 revisions

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.

Patch for Rails 2.1.1

There was a change in Rails 2.1.1 that causes a 406 Not Acceptable HTTP error. There is a patch
to solve this though.

Just apply the patch to your frozen copy of rails:

curl http://rails.lighthouseapp.com/attachments/46685/http_accept_header.diff | git am

See details here

Note about wikis and docs

Currently, the wikis are empty. So I’d appreciate if you want to contribute with wikis documentation; just drop me
a line at edgar.js[at]gmail.com.

But, it doesn’t mean there isn’t enough documentation. The full plugin is rdoced so you just can rake rdoc and
check the details.


Repository

Find it at http://github.com/edgarjs/youtube-model

Documentation

Installing

Simply as:

./script/plugin install git://github.com/edgarjs/youtube-model.git

Generating the system

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

./script/generate youtube_model

This generator will create four files:

  • An ActiveResource based model under app/models directory.
  • A yaml configuration file under config directory.
  • An initializer that loads the configuration file, under config/initializers 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 page.

Listing videos

Let’s say you generated a model named YouTube. So now you can use that model very similar
to an ActiveRecord model.

To search for videos passing a simple query use the method find

YouTube.find(‘rails conference 2008’)

To get the videos uploaded by a user you can use the method uploaded_by passing the username

YouTube.uploaded_by(‘envyads’)

And so on. To see the full list of available methods see the Wikis

All methods will return an instance of Entry. The Entry object encapsulates information about a video, playlist,
suscription, contact or other entity. This class is parsed from the <entry> tag of the xml response, wich is the root
tag in all YouTube data API requests. (To see more about the structure of the xml response go
to the YouTube API

The returned model instance has a main mehod: videos, wich is more than an alias for entry.

Example


  1. VideosController
    def index
    @youtube = YouTube.find(‘ruby on rails’, :max_results => 5)
    end
def show @video = YouTube.find_by_id(params[:id]) end
  1. app/views/videos/index.html.erb

    <%= image_tag youtube.logo %></p> <h1><%= @youtube.title %></h1> <p><%= "Displaying #{youtube.startIndex} – #{@youtube.itemsPerPage} of #{@youtube.totalResults}" >


    <= link_to ‘Upload’, youtube_auth_url(new_video_url) >


    <- for video in @youtube.videos >
    <
    = video.title > in <= video.group.category >
    by <
    = link_to video.author.name, “http://youtube.com/#{video.author.name}” >
    <
    = simple_format truncate(video.content, :length => 100) >
    <
    = link_to image_tag(video.group.thumbnail.first.url, :alt => video.title), video_path(video.id) >
    <
    = link_to ‘Watch on youtube’, video.link.first.href >

     


    <
    end -%>
  1. app/views/videos/show.html.erb

    <%= video.title %></h1> <em>by <%= link_to @video.author.name, "http://youtube.com/#{video.author.name}" >

    <

    = youtube_embed @video >
    <
    = link_to ‘Back’, videos_path %>

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

Uploading videos

As seen in the index example above, there is a helper named youtube_auth_url wich generates an url to the
YouTube authentication page. Pass another url as param to wich be redirected after authentication.
This helper method takes the session and secure options from the configuration file. See the details of this options
at the YouTube API

Upload process has two steps:

  1. Meta-info for the video.
  2. File choose.

So basically we have two actions in our controller to upload a video. Here’s a quick code example:


  1. routes.rb
    map.resources :videos, :new => {:upload => :post}
  1. VideosController
    def new
    @categories ||= YouTube.categories_collection
    end
def upload @upload_info = YouTube.get_upload_url(params[:video]) end
  1. app/views/videos/new.html.erb
    <% form_for ‘video’, :url => upload_new_video_path do |f| >
    <
    = f.hidden_field :auth_sub, :value => params[:token] >

    <

    = 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 ‘Continue to Step 2’ %>

<% end %>
  1. app/views/videos/upload.html.erb
    <% form_tag @upload_info[:url], :multipart => true do >
    <
    = hidden_field_tag :token, @upload_info[:token] >

    <

    = label_tag :file >
    <
    = file_field_tag :file >

    <

    = submit_tag ‘Upload video’ >
    <
    end %>

Pay special attention to the routes file, I’ve added a new route called :upload so I can use it
in the first step to set the form post url by calling upload_new_video_path.
Also don’t forget to set :multipart => true in the second step.

Very easy isn’t it? Yep, that is all.

And for a more specific speech… the class method get_upload_url receives a hash containing
the meta-info of the first step, and returns a hash with the upload url and token.

For extra information don’t forget to read the wikis
or if you wanna go further just generate the rdoc for the plugin, it will be uber helpful.

Feedback

I’ll really appreciate your feedback, please contact me at edgar.js[at]gmail.com

License

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