ActiveYoutube is a gem to access YouTube API using ActiveResource. This gem wraps code from our
previous post on extending ActiveResource to access YouTube. There have been minor changes, which are :
- Namespace in class names: Video, User, StandardFeed and Playlist classes have been moved to "Youtube" module, to prevent any conflicts with your ActiveRecord models.
- CustomMethods related change: In last version, only response from "find" was converting "entry" object to array of "entry" object. Now, the same behavior is implemented for custom http calls like Video.find().get(:comments)
- Small patch for better namespacing: Its basically some code from the rails trunk on ActiveResource, for better handling of namespaces while creating ActiveResource objects.
Gem Installation:
|
sudo gem install active_youtube |
Example Usage:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
#### Video
## search for videos on 'ruby'
search = Youtube::Video.find(:first, :params => {: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 |
Thank you for doing it simple. :)
NameError in Rap starsController#search
uninitialized constant RapStarsController::Youtube
RAILS_ROOT: C:/rails_apps/hardestout Application Trace | Framework Trace | Full Trace
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:478:in `const_missing’ app/controllers/rap_stars_controller.rb:24:in `search’
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:478:in `const_missing’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:1158:in `send’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:1158:in `perform_action_without_filters’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:697:in `call_filters’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:689:in `perform_action_without_benchmark’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue’ C:/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/rescue.rb:199:in `perform_action_without_caching’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/caching.rb:678:in `perform_action’ C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract/query_cache.rb:33:in `cache’ C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/query_cache.rb:8:in `cache’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/caching.rb:677:in `perform_action’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:524:in `send’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:524:in `process_without_filters’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:685:in `process_without_session_management_support’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/session_management.rb:123:in `process’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:388:in `process’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:171:in `handle_request’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:115:in `dispatch’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:126:in `dispatch_cgi’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:9:in `dispatch’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/rails.rb:76:in `process’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/rails.rb:74:in `synchronize’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/rails.rb:74:in `process’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:159:in `process_client’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:158:in `each’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:158:in `process_client’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:285:in `run’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:285:in `initialize’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:285:in `new’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:285:in `run’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:268:in `initialize’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:268:in `new’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:268:in `run’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/configurator.rb:282:in `run’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/configurator.rb:281:in `each’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/configurator.rb:281:in `run’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/bin/mongrel_rails:128:in `run’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/command.rb:212:in `run’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/bin/mongrel_rails:281 C:/ruby/bin/mongrel_rails:19:in `load’ C:/ruby/bin/mongrel_rails:19
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:478:in `const_missing’ app/controllers/rap_stars_controller.rb:24:in `search’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:1158:in `send’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:1158:in `perform_action_without_filters’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:697:in `call_filters’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:689:in `perform_action_without_benchmark’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue’ C:/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/rescue.rb:199:in `perform_action_without_caching’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/caching.rb:678:in `perform_action’ C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract/query_cache.rb:33:in `cache’ C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/query_cache.rb:8:in `cache’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/caching.rb:677:in `perform_action’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:524:in `send’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:524:in `process_without_filters’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:685:in `process_without_session_management_support’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/session_management.rb:123:in `process’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:388:in `process’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:171:in `handle_request’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:115:in `dispatch’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:126:in `dispatch_cgi’ C:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:9:in `dispatch’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/rails.rb:76:in `process’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/rails.rb:74:in `synchronize’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/rails.rb:74:in `process’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:159:in `process_client’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:158:in `each’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:158:in `process_client’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:285:in `run’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:285:in `initialize’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:285:in `new’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:285:in `run’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:268:in `initialize’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:268:in `new’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel.rb:268:in `run’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/configurator.rb:282:in `run’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/configurator.rb:281:in `each’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/configurator.rb:281:in `run’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/bin/mongrel_rails:128:in `run’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/command.rb:212:in `run’ C:/ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.2-x86-mswin32/bin/mongrel_rails:281 C:/ruby/bin/mongrel_rails:19:in `load’ C:/ruby/bin/mongrel_rails:19
Request
Parameters:
None
very good informative. thanks so much.
Nice gem, Though i’m 100% new to rails so i not quite sure how to use it yet.
Simple and clear defitinion and sample codes thank you
komik videolar
Active-Youtube is useful and nice.
But it output warning message that is “Gems/1.8/gems/active_youtube-1.0.0/lib/patch.rb:30: warning: already initialized constant Rating”.
I guess that patch.rb:30 should replace to the following code.
Is it correct?
In the words of Ted Nugent, “this is just what the doctor ordered”.
Thanks much.
Also, I too am getting the warning that yutaka is along with a few other similar ones about “Group”, “Link”, “Entry”, etc. It doesn’t seem to be breaking anything, just throwing a warning.
After applying utaka’s code change I no longer get the “initialized constant” errors. I think I read somewhere in the usage examples that GCode (or at least YouTube’s) API reports back arrays that start the index at 1 instead of 0, so the “-1” in the code was borking things.
So that fixed it for me at least.
However, I have another problem, that may not be with Active YouTube, but rather with my limited understanding of the way the YT API works. I’m trying to get the video type (as the YT API docs recommend), but calling “vidtype = entry.group.content.first.type” seems to conflict with some in-built ruby method for objects, because it throws me a message that says, “warning: Object#type is deprecated; use Object#class” in the console. Am i misunderstanding how this whole thing works?
For instance, in a line like this:
<media:content />
I can get everything but the “type” and “yt:format” properties. I’m not sure how to go about dealing with the “yt:format” one, but I think the “type” property is just conflicting with a standard ruby method.
Sorry if I’m being a pain. The gem is great, I’m just stuck on a couple little tidbits :)
Hi, itsalljustaride
You can get the type by the following code.
Also, I too am getting the warning that yutaka is along with a few other similar ones about “Group”, “Link”, “Entry”, etc. It doesn’t seem to be breaking anything, just throwing a warning.
I understand and very good code
Great code
Looks very interesting, thanks for article.
Looks very interesting, thanks for article.
thank you
thank you very much..
thank you. l’m love you tech :)
For newbies to rails it would be nice to see a simple tutorial for this gem. After installing the gem successfully, i am not sure what in addition to say putting <% results = Video.search_by_tags(“Comedy”) puts results0.entry0.title %>
in my application layout file (as a simple test) i need to do.
i know this is prob rails 101 …trying to learn as i go … thanks for the effort creating this.
Ahh solved my issue .. make sure and restart server after adding gem…. basic ror knowledge …but easy trip up if you’re new.
Is there a way to make this work with the will_paginate gem? i tried but seemed to run into road block … undefined method `paginate’ for Youtube::Video:Class
would love to find easy way to paginate results….
thank you very much
thank you very much
Thanks good article Guncel driver download sitesi,actual driver download site Driver download sitemap Driver download portal
This is fantastic…if you’re running Rails 2.0.2. Unfortunately, 2.1.1 breaks this gem.
See here: http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1053-removed-http-header-accept-by-default
Hopefully the author (anybody else?) can monkey patch the gem so we 2.1.1 users can use it!? Any advice?
Great work, and thank you.
How to make it work with Rails 2.1.1
create connection.rb file in your lib directory with this code:
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
require that file in your envrionment.rb file rock-and-roll!
thanks kevin.
make sure to do the require for the connection.rb at the end of the environment.rb file.
how would one use will_paginate with this plug? i tried unsuccessfully to just use Youtube::Video.paginate(...) i get undefined method error.
is it simple/possible?
hi there,
where would you put this code in?under the app/controllers folder? or..?
i’m really new to Ruby but i also really really need to use this API for my project.. will appreciate any help that can be rendered…
Adam,
Yes, put it in a controller.. Refer to the code contained within the controller from a view.
—Andy
I’ve got “Failed with 406 Not Acceptable” on Youtube::Video.find(:first, :params => {:vq => ‘ruby’}) How to fix ?
Hi all great stuff even for a Beginner , I run also in Failed with 406 as well as send! issue ruby 1.8.6 doesn’t seem to have send! . I got it running . I created a small tutorial what I did allwaysbeginner.wordpress.com .
Nice gem, Though i’m 100% new to rails so i not quite sure how to use it yet.
Tons of thanks Allwasybeginner!
As well as Kevin and the authors. Open source is a beautiful thing.
Got it running! Thank you for this interesting article.
I’m not an expert but this gem is a really brilliant (that’s the right word here ;)) piece of coding work, both clean and efficient! Thanks for displaying this work as an example of how a properly coded gem should look like! Wendy Buchanan, Real Estate Auctions
Got it ! Thx for sharing
Thanks for the detailed code explanations. Ist’s very useful in this manner.
Thanks, Active-Youtube is useful and nice.
I am trying to search and get back a list of results based on a query. I tried: search = Youtube::Video.find(:first, :params => {:vq => ‘query’, :”max-results” => ‘5’}) serach.each_with_index do |result, i| result.entry[i].title end
and various others but always only get back one result.
Any Suggestions?
Thanks
Sorry that was a typo above. I tried find :all too!
Very good informative. thanks so much for sharing.
How it will work with rails 2.3.2 ?? Im still getting this error:
Failed with 406 Not Acceptable
I have added the file connection.rb in lib, & called it from environment.rb
Please Help
I’m not an expert but this gem is a really brilliant (that’s the right word here ;)) piece of coding work, both clean and efficient! Thanks for displaying this work as an example of how a properly coded gem should look like! Wendy Buchanan, Real Estate Auctions
I agree with this idea!
one of the best tutorials i’ve read
Got it running! Thank you for this interesting article.
I am just new to your blog and just spent about 1 hour and 30 minutes lurking and reading. I think I will frequent your blog from now on after going through some of your posts. I will definitely learn a lot from them. Regards – Gerry
Great sharing. It does work
Echoing the previous comments, this is by far the best quickstart I’ve been able to find about accessing YouTube API using ActiveResource. Thank you very much.
Fresh thinking – well done!
Thanks a lot for sharing! Useful indeed…
Looks very interesting, thanks for article.
Very nice…
granite countertops nj