30分で Rails youtubeアプリ

30分でできる,簡単簡単!こんな感じで,もちろんちゃんと再生できる.


youtube ID取得

youtube gem インストール

sudo gem install youtube
Successfully installed xml-simple-1.0.11
Successfully installed youtube-0.8.6

railsアプリ作成

$ rails _1.2.6_ flyertube -d mysql
$ ruby script/generate controller test
コントローラ
class TestController < ApplicationController
  def index
    require 'youtube'

    dev_id = 'XXXXXX'

    youtube = YouTube::Client.new(dev_id)
    @videos = youtube.videos_by_category_and_tag(YouTube::Category::MUSIC, 'simple plan')
  end

end
ビュー
<html>
<head>
<title>flyertube</title>
</head>
<body>
  <% for video in @videos %>
  <object width="170" height="142"><param name="movie" value="<%=h video.embed_url%>"></param><param name="wmode" value="transparent"></param><embed src="<%=h video.embed_url%>" type="application/x-shockwave-flash" wmode="transparent" width="170" height="142"></embed></object>
  <% end %>
</body>
</html>
ruby script/server

http://localhost:3000/test/index にアクセスする.