public
Rubygem
Description: An object-oriented Ruby wrapper for the YouTube GData API
Homepage: http://groups.google.com/group/ruby-youtube-library
Clone URL: git://github.com/tmm1/youtube-g.git
Added :page and :per_page options, this allows easier usage of the 
will_paginate
dinsley (author)
Mon Jul 07 16:32:18 -0700 2008
commit  2bca90063341874652f8f0c4045eab5c5fabc18d
tree    55e9a9da15d152fecf02763da5a541d3696d7087
parent  4d516a83e57ca96697c89eade8046787121efdd6
...
1
 
 
2
3
4
...
1
2
3
4
5
6
0
@@ -1,4 +1,6 @@
0
 == trunk
0
+* Added :page and :per_page options, this allows easier usage of the will_paginate
0
+ plugin with the library. The :offset and :max_results options are no longer available. [Daniel Insley]
0
 * Added ability to get video responses on the instances of the YouTube::Model::Video object. [Daniel Insley]
0
 * Added and improved the existing documentation [Daniel Insley]
0
 * Fixed usage of deprecated yt:racy, now using media:rating [Daniel Insley]
...
1
 
2
3
4
...
32
33
34
 
35
36
37
...
40
41
42
 
43
44
45
...
 
1
2
3
4
...
32
33
34
35
36
37
38
...
41
42
43
44
45
46
47
0
@@ -1,4 +1,4 @@
0
-youtube-g
0
+rakeyoutube-g
0
     by Shane Vitarana and Walter Korman
0
 
0
   Rubyforge: http://rubyforge.org/projects/youtube-g/
0
@@ -32,6 +32,7 @@ Create a client:
0
 Basic queries:
0
 
0
   client.videos_by(:query => "penguin")
0
+ client.videos_by(:query => "penguin", :page => 2, :per_page => 15)
0
   client.videos_by(:tags => ['tiger', 'leopard'])
0
   client.videos_by(:categories => [:news, :sports])
0
   client.videos_by(:categories => [:news, :sports], :tags => ['soccer', 'football'])
0
@@ -40,6 +41,7 @@ Basic queries:
0
 Standard feeds:
0
   
0
   client.videos_by(:most_viewed)
0
+ client.videos_by(:most_linked, :page => 3)
0
   client.videos_by(:top_rated, :time => :today)
0
   
0
 Advanced queries (with boolean operators OR (either), AND (include), NOT (exclude)):
...
17
18
19
20
 
 
21
22
23
24
25
26
 
 
27
28
29
...
33
34
35
 
 
 
 
 
36
37
 
38
39
 
40
41
 
42
43
44
...
59
60
61
 
 
 
 
 
 
62
63
...
17
18
19
 
20
21
22
23
24
25
 
 
26
27
28
29
30
...
34
35
36
37
38
39
40
41
42
 
43
44
 
45
46
 
47
48
49
50
...
65
66
67
68
69
70
71
72
73
74
75
0
@@ -17,13 +17,14 @@ class YouTubeG
0
     # You can find out more specific information about what each standard feed provides
0
     # by visiting: http://code.google.com/apis/youtube/reference.html#Standard_feeds
0
     #
0
- # options<Hash>:: Accepts the options of :time, :offset, and :max_results. (Optional)
0
+ # options<Hash> (optional):: Accepts the options of :time, :page (default is 1),
0
+ # and :per_page (default is 25).
0
     #
0
     #
0
     # If fetching videos by tags, categories, query:
0
     # params<Hash>:: Accepts the keys :tags, :categories, :query, :order_by,
0
- # :author, :racy, :response_format, :video_format, :offset,
0
- # and :max_results.
0
+ # :author, :racy, :response_format, :video_format, :page (default is 1),
0
+ # and :per_page(default is 25)
0
     #
0
     # options<Hash>:: Not used. (Optional)
0
     #
0
@@ -33,12 +34,17 @@ class YouTubeG
0
     # === Returns
0
     # YouTubeG::Response::VideoSearch
0
     def videos_by(params, options={})
0
+ request_params = params.respond_to?(:to_hash) ? params : options
0
+ request_params[:page] ||= 1
0
+ request_params[:max_results] = request_params[:per_page] || 25
0
+ request_params[:offset] = calculate_offset(request_params[:page], request_params[:max_results] )
0
+
0
       if params.respond_to?(:to_hash) and not params[:user]
0
- request = YouTubeG::Request::VideoSearch.new(params)
0
+ request = YouTubeG::Request::VideoSearch.new(request_params)
0
       elsif (params.respond_to?(:to_hash) && params[:user]) || (params == :favorites)
0
- request = YouTubeG::Request::UserSearch.new(params, options)
0
+ request = YouTubeG::Request::UserSearch.new(request_params, options)
0
       else
0
- request = YouTubeG::Request::StandardSearch.new(params, options)
0
+ request = YouTubeG::Request::StandardSearch.new(params, request_params)
0
       end
0
       
0
       logger.debug "Submitting request [url=#{request.url}]." if logger
0
@@ -59,5 +65,11 @@ class YouTubeG
0
       parser.parse
0
     end
0
     
0
+ private
0
+
0
+ def calculate_offset(page, per_page)
0
+ page == 1 ? 1 : ((per_page * page) - per_page + 1)
0
+ end
0
+
0
   end
0
 end
...
22
23
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
26
27
...
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
0
@@ -22,6 +22,24 @@ class TestClient < Test::Unit::TestCase
0
     response.videos.each { |v| assert_valid_video v }
0
   end
0
   
0
+ def test_should_respond_to_a_basic_query_with_paging
0
+ response = @client.videos_by(:query => "penguin")
0
+ assert_equal "http://gdata.youtube.com/feeds/api/videos", response.feed_id
0
+ assert_equal 25, response.max_result_count
0
+ assert_equal 1, response.offset
0
+
0
+ response = @client.videos_by(:query => "penguin", :page => 2)
0
+ assert_equal "http://gdata.youtube.com/feeds/api/videos", response.feed_id
0
+ assert_equal 25, response.max_result_count
0
+ assert_equal 26, response.offset
0
+
0
+ response2 = @client.videos_by(:query => "penguin", :page => 3)
0
+ assert_equal "http://gdata.youtube.com/feeds/api/videos", response2.feed_id
0
+ assert_equal 25, response2.max_result_count
0
+ assert_equal 51, response2.offset
0
+ end
0
+
0
+
0
   def test_should_get_videos_for_multiword_metasearch_query
0
     response = @client.videos_by(:query => 'christina ricci')
0
   

Comments

    No one has commented yet.