youtube 0.8.0 → 0.8.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/CHANGELOG +13 -0
  2. data/README +2 -0
  3. data/Rakefile +1 -1
  4. data/lib/youtube.rb +57 -7
  5. data/test/test_api.rb +38 -2
  6. metadata +2 -2
data/CHANGELOG CHANGED
@@ -1,3 +1,16 @@
1
+ * 2007/02/12
2
+
3
+ - [drummr77] Added fix to check if returned video response contains an
4
+ Array or not. Return nil instead of erroring out.
5
+
6
+ - [drummr77] Fixed Video.length accessor. Changed to Video.length_seconds.
7
+
8
+ - [drummr77] Added new API methods:
9
+ youtube.videos.list_by_related (with paging),
10
+ youtube.videos.list_by_playlist (with paging),
11
+ youtube.videos.list_by_category (with paging)
12
+ youtube.videos.list_by_category_and_tag (with paging)
13
+
1
14
  * 2006/11/15
2
15
 
3
16
  - [drummr77] Applied a modified version of the patch sent in by Lucas
data/README CHANGED
@@ -8,6 +8,8 @@ http://www.youtube.com/my_profile_dev.
8
8
 
9
9
  The RubyForge project is at http://rubyforge.org/projects/youtube.
10
10
 
11
+ The Google Group to discuss issues and bugs is at http://groups.google.com/group/ruby-youtube-library?hl=en.
12
+
11
13
  == About
12
14
 
13
15
  Implements version 2 of YouTube's API.
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'rake/gempackagetask'
6
6
 
7
7
  spec = Gem::Specification.new do |s|
8
8
  s.name = 'youtube'
9
- s.version = '0.8.0'
9
+ s.version = '0.8.5'
10
10
  s.author = 'Shane Vitarana'
11
11
  s.email = 'shanev@gmail.com'
12
12
  s.platform = Gem::Platform::RUBY
@@ -57,14 +57,15 @@ module YouTube
57
57
  # videos of the supplied +username+.
58
58
  def favorite_videos(username)
59
59
  response = users_list_favorite_videos(:user => username)
60
- response['video_list']['video'].compact.map { |video| Video.new(video) }
60
+ _parse_video_response(response)
61
61
  end
62
62
 
63
63
  # Returns a list of YouTube::Friend objects detailing the friends of
64
64
  # the supplied +username+.
65
65
  def friends(username)
66
66
  response = users_list_friends(:user => username)
67
- response['friend_list']['friend'].compact.map { |friend| Friend.new(friend) }
67
+ friends = response['friend_list']['friend']
68
+ friends.is_a?(Array) ? friends.compact.map { |friend| Friend.new(friend) } : nil
68
69
  end
69
70
 
70
71
  # Returns a list of YouTube::Video objects detailing the videos
@@ -75,21 +76,65 @@ module YouTube
75
76
  # +per_page+ = the number of results per page (default: 20, max 100).
76
77
  def videos_by_tag(tag, page = 1, per_page = 20)
77
78
  response = videos_list_by_tag(:tag => tag, :page => page, :per_page => per_page)
78
- response['video_list']['video'].compact.map { |video| Video.new(video) }
79
+ _parse_video_response(response)
80
+ end
81
+
82
+ # Returns a list of YouTube::Video objects that match any of the
83
+ # specified +tag+s.
84
+ #
85
+ # Optional parameters are:
86
+ # +page+ = the "page" of results to retrieve (e.g. 1, 2, 3)
87
+ # +per_page+ = the number of results per page (default: 20, max 100).
88
+ def videos_by_related(tag, page = 1, per_page = 20)
89
+ response = videos_list_by_related(:tag => tag, :page => page, :per_page => per_page)
90
+ _parse_video_response(response)
91
+ end
92
+
93
+ # Returns a list of YouTube::Video objects with the specified
94
+ # +playlist id+.
95
+ #
96
+ # Optional parameters are:
97
+ # +page+ = the "page" of results to retrieve (e.g. 1, 2, 3)
98
+ # +per_page+ = the number of results per page (default: 20, max 100).
99
+ def videos_by_playlist(id, page = 1, per_page = 20)
100
+ response = videos_list_by_playlist(:id => id, :page => page, :per_page => per_page)
101
+ _parse_video_response(response)
102
+ end
103
+
104
+ # Returns a list of YouTube::Video objects with the specified
105
+ # +category id+.
106
+ #
107
+ # Optional parameters are:
108
+ # +page+ = the "page" of results to retrieve (e.g. 1, 2, 3)
109
+ # +per_page+ = the number of results per page (default: 20, max 100).
110
+ def videos_by_category(id, page = 1, per_page = 20)
111
+ response = videos_list_by_category(:category_id => id, :page => page, :per_page => per_page)
112
+ _parse_video_response(response)
113
+ end
114
+
115
+ # Returns a list of YouTube::Video objects detailing the videos
116
+ # matching the supplied +category id+ and +tag+.
117
+ #
118
+ # Optional parameters are:
119
+ # +page+ = the "page" of results to retrieve (e.g. 1, 2, 3)
120
+ # +per_page+ = the number of results per page (default: 20, max 100).
121
+ def videos_by_category_and_tag(id, tag, page = 1, per_page = 20)
122
+ response = videos_list_by_category_and_tag(:category_id => id, :tag => tag, :page => page, :per_page => per_page)
123
+ _parse_video_response(response)
79
124
  end
80
125
 
81
126
  # Returns a list of YouTube::Video objects detailing the videos
82
127
  # uploaded by the specified +username+.
83
128
  def videos_by_user(username)
84
129
  response = videos_list_by_user(:user => username)
85
- response['video_list']['video'].compact.map { |video| Video.new(video) }
130
+ _parse_video_response(response)
86
131
  end
87
132
 
88
133
  # Returns a list of YouTube::Video objects detailing the current
89
134
  # global set of featured videos on YouTube.
90
135
  def featured_videos
91
136
  response = videos_list_featured
92
- response['video_list']['video'].compact.map { |video| Video.new(video) }
137
+ _parse_video_response(response)
93
138
  end
94
139
 
95
140
  # Returns a YouTube::VideoDetails object detailing additional
@@ -131,6 +176,11 @@ module YouTube
131
176
  def _http_get(url)
132
177
  Net::HTTP.get_response(URI.parse(url)).body.to_s
133
178
  end
179
+
180
+ def _parse_video_response(response)
181
+ videos = response['video_list']['video']
182
+ videos.is_a?(Array) ? videos.compact.map { |video| Video.new(video) } : nil
183
+ end
134
184
  end
135
185
 
136
186
  class Friend
@@ -199,7 +249,7 @@ module YouTube
199
249
  attr_reader :description
200
250
  attr_reader :embed_url
201
251
  attr_reader :id
202
- attr_reader :length
252
+ attr_reader :length_seconds
203
253
  attr_reader :rating_avg
204
254
  attr_reader :rating_count
205
255
  attr_reader :tags
@@ -214,7 +264,7 @@ module YouTube
214
264
  @comment_count = payload['comment_count'].to_i
215
265
  @description = payload['description']
216
266
  @id = payload['id']
217
- @length = payload['length']
267
+ @length_seconds = payload['length_seconds'].to_i
218
268
  @rating_avg = payload['rating_avg'].to_f
219
269
  @rating_count = payload['rating_count'].to_i
220
270
  @tags = payload['tags']
@@ -1,6 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'test/unit'
3
3
  require 'youtube'
4
+ require 'pp'
4
5
 
5
6
  # This test class assumes an active internet connection
6
7
  class TestAPI < Test::Unit::TestCase
@@ -30,6 +31,11 @@ class TestAPI < Test::Unit::TestCase
30
31
  # pull out one to scrutinize
31
32
  sample = favorites.first
32
33
  _test_video(sample)
34
+
35
+ # user doesn't exist / test exception handling
36
+ assert_raise RuntimeError do
37
+ favorites = @client.favorite_videos('dsfa98sd7fa9s8d7fd8')
38
+ end
33
39
  end
34
40
 
35
41
  def test_friends
@@ -50,18 +56,47 @@ class TestAPI < Test::Unit::TestCase
50
56
  end
51
57
 
52
58
  def test_videos_by_tag
59
+ # test case where videos exist
53
60
  videos = @client.videos_by_tag('iron maiden')
54
61
  _test_video_list(videos)
55
-
56
62
  videos = @client.videos_by_tag('caffe trieste')
57
63
  _test_video_list(videos)
64
+
65
+ # test case where no videos exist
66
+ videos = @client.videos_by_tag('u423oi4j23oi4j234io')
67
+ assert_nil videos
58
68
  end
59
69
 
60
70
  def test_videos_by_user
71
+ # test case where videos exist
61
72
  videos = @client.videos_by_user('whytheluckystiff')
62
73
  _test_video_list(videos)
74
+
75
+ # test case where videos don't exist
76
+ videos = @client.videos_by_user('br0wnpunk')
77
+ assert_nil videos
63
78
  end
64
79
 
80
+ def test_videos_by_related
81
+ videos = @client.videos_by_related('squats')
82
+ _test_video_list(videos)
83
+ end
84
+
85
+ def test_videos_by_playlist
86
+ videos = @client.videos_by_playlist('4009D81B1E0277A1')
87
+ _test_video_list(videos)
88
+ end
89
+
90
+ # def test_videos_by_category
91
+ # videos = @client.videos_by_category('10')
92
+ # _test_video_list(videos)
93
+ # end
94
+ #
95
+ # def test_videos_by_category_and_tag
96
+ # videos = @client.videos_by_category_and_tag('10', 'punk')
97
+ # _test_video_list(videos)
98
+ # end
99
+
65
100
  def test_featured_videos
66
101
  videos = @client.featured_videos
67
102
  _test_video_list(videos)
@@ -149,9 +184,10 @@ class TestAPI < Test::Unit::TestCase
149
184
  # check other attributes
150
185
  assert (video.thumbnail_url =~ /\.jpg$/)
151
186
  assert (video.title && video.title.length > 0)
187
+ assert (video.length_seconds > 0)
152
188
  assert (video.upload_time && video.upload_time.is_a?(Time))
153
189
  _assert_youtube_url video.url
154
- assert (video.view_count > 0)
190
+ assert (video.view_count >= 0)
155
191
  assert (video.tags && video.tags.length > 0)
156
192
  assert (video.author && video.author.length > 0)
157
193
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: youtube
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.8.0
7
- date: 2006-11-18 00:00:00 -06:00
6
+ version: 0.8.5
7
+ date: 2007-02-12 00:00:00 -06:00
8
8
  summary: A Ruby object-oriented interface to the YouTube REST API.
9
9
  require_paths:
10
10
  - lib