Archive for March, 2009

Ruby Rails Beginner try active youtube ( activeyoutube )

March 16, 2009

Hi All

101 Howto for activeyoutube & ruby on rails . I tried to investigate web services and ruby on rails . My simple thought was try to access youtube . So I found activeyoutube (http://www.quarkruby.com/2008/1/15/activeresource-and-youtube , gem version http://www.quarkruby.com/2008/2/12/active-youtube) which I thought to use . I run in a few issues , maybe because I am a Beginner .

First my Installation :

  • Ruby on Rails 2.2.2 and 2.3.1
  • activeresource 2.2.2 and 2.3.1
  • ruby 1.86

As I run into issues I tried both Rails Versions .

Let’s start :

install activeyoutube :

gem install active_youtube

or

sudo gem install active youtube

Create Rails youtube project :

rails youtube

Create Youtube Controller

cd youtube

ruby script/generate controller youtube index

edit app/controllers/youtube_controller.rb

Add Require for activeyoutube to app/controllers/youtube_controller.rb : require ‘active_youtube’

Add Index in the controller : def index , end .

Use the the activeyoutube example in the youtube controller for index :

copy the example section from http://www.quarkruby.com/2008/2/12/active-youtube in your controller (app/controllers/youtube_controller.rb):

app/controllers/youtube_controller.rb :

require ‘active_youtube’

class YoutubeController {:vq => ‘ruby’, :”max-results” => ‘5’})

puts search.entry.length

## video information of id = ZTUVgYoeN_o

vid = Youtube::Video.find(“ZTUVgYoeN_o”)

puts vid.group.content[0].url

## video comments

comments = Youtube::Video.find_custom(“ZTUVgYoeN_o”).get(:comments)

puts comments.entry[0].link[2].href

## searching with category/tags

results = Youtube::Video.search_by_tags(“Comedy”)

puts results[0].entry[0].title

#### STANDARDFEED

## retrieving standard feeds

most_viewed = Youtube::Standardfeed.find(:most_viewed, :params => {:time => ‘today’})

puts most_viewed.entry[0].group.content[0].url

#### USER

## user’s profile – guthrie

user_profile = Youtube::User.find(“guthrie”)

puts user_profile.link[1].href

#### PLAYLIST

## get playlist – multiple elements in playlist

playlist = Youtube::Playlist.find(“EBF5D6DC4589D7B7”)

puts playlist.entry[0].group.content[0].url

end

end

Start the rails :

ruby script/server

Connect to Youtube :

http://localhost:3000/youtube

You might get this error :

ActiveResource::ClientError in YoutubeController#index

Failed with 406 Not Acceptable

Issue Change in activersource from version 2.1.1 ( more info http://rails.lighthouseapp.com/projects/8994/tickets/1053-removed-http-header-accept-by-default)

Fix :

create file lib/connection.rb

file : lib/connection.rb :

module ActiveResource

# overrides the default ActiveResource Connection Class build_request_headers method because of this error:

# http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1053-removed-http-header-accept-by-default

class Connection

def build_request_headers(headers, http_method=nil)

authorization_header.update(default_header).update(headers).update(http_format_header(http_method))

authorization_header.update(default_header).update(headers)

end

end

end

Add line on the end connection.rb to your config config/environement.rb

additional line at the end config/environement.rb:

require ‘lib/connection.rb’

Restart Rails :

ruby scribt/server

You might get the next error :

NoMethodError in YoutubeController#index

undefined method `send!’ for Youtube::Video:Class

Issue Ruby 1.8 doesn’t seem to have send! even not all 1.9 Versions ( more Info : http://deaddeadgood.com/2008/11/17/rubys-send-method)

I changed the send! to send /Library/Ruby/Gems/1.8/gems/active_youtube-1.0.0/lib/activeyoutube.rb

New : “#{method_name}#{self.class.send(:query_string, options)}”

Old : “#{method_name}#{self.class.send!(:query_string, options)}”

As I am a beginner there might be a better way , this was my quick and dirty fix .

Restart Rails :

ruby script/server

Connect again with the browser everything seem to work , the requested Info is in shell , not in the browser yet .

Advertisements

Welcome

March 16, 2009

Welcome to my Blog .  I am working a number of years in the IT and I allways try new stuff , I somehow experienced but not a specialist . I am amazed with the information you find , but on the other hand a lot of Info is only for specialist , so I share my Information when I try new stuff and wher I struggled . I hope this helps others to save time . I also may share thoughts how some technologies can change the Business or the IT .