youtube_search 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- youtube_search (0.1.0)
4
+ youtube_search (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/Readme.md CHANGED
@@ -25,10 +25,24 @@ Usage
25
25
  ...
26
26
  }
27
27
 
28
+ page / per_page are supported
29
+
30
+ YoutubeSearch.search('cats', :page => 10, :per_page => 4).first
31
+
32
+ and [standard youtube options](http://code.google.com/apis/youtube/2.0/developers_guide_protocol.html#Standard_parameters)
33
+
34
+ YoutubeSearch.search('cats', :page => 10, :per_page => 4).first
35
+
36
+ I can haz iframe:
37
+
38
+ # DISCLAIMER this iframe may steal 4 minutes of your life ;)
39
+ id = YoutubeSearch.search('lolcats').first['video_id']
40
+ %{<iframe src="http://www.youtube.com/embed/#{id}" width=640 height=480 frameborder=0></iframe>}
41
+
28
42
  TODO
29
43
  ====
30
44
  - more detailed xml parsing (atm only name and value is read)
31
- - parse dates into objects
45
+ - parse dates into ruby objects
32
46
 
33
47
 
34
48
  Author
@@ -1,3 +1,3 @@
1
1
  module YoutubeSearch
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -3,8 +3,10 @@ require 'cgi'
3
3
  require 'open-uri'
4
4
 
5
5
  module YoutubeSearch
6
- def self.search(query)
7
- xml = open("http://gdata.youtube.com/feeds/api/videos/-/#{CGI.escape(query)}").read
6
+ def self.search(query, options={})
7
+ options = options_with_per_page_and_page(options)
8
+ query = options.merge(:q => query).map{|k,v| "#{CGI.escape k.to_s}=#{CGI.escape v.to_s}" }.join('&')
9
+ xml = open("http://gdata.youtube.com/feeds/api/videos?#{query}").read
8
10
  parse(xml)
9
11
  end
10
12
 
@@ -21,4 +23,21 @@ module YoutubeSearch
21
23
  end
22
24
  entries
23
25
  end
26
+
27
+ private
28
+
29
+ def self.options_with_per_page_and_page(options)
30
+ options = options.dup
31
+ if per_page = options.delete(:per_page)
32
+ options['max-results'] = per_page
33
+ else
34
+ per_page = options['max-results']
35
+ end
36
+
37
+ if per_page and page = options.delete(:page)
38
+ options['start-index'] = per_page.to_i * ([page.to_i, 1].max - 1)
39
+ end
40
+
41
+ options
42
+ end
24
43
  end
@@ -5,26 +5,64 @@ describe YoutubeSearch do
5
5
  YoutubeSearch::VERSION.should =~ /^[\.\da-z]+$/
6
6
  end
7
7
 
8
- it "can parse xml" do
9
- # convert to array so we get a nice diff in case of errors
10
- YoutubeSearch.parse(File.read('spec/fixtures/search_boat.xml')).first.sort.should == [
11
- ["author",nil],
12
- ["category", nil],
13
- ["comments",nil],
14
- ["content","Top YouTube Videos on tubecrunch.blogspot.com A killer whale swims right up to a boat and shows off his best sounding motor impression."],
15
- ["group", nil],
16
- ["id","http://gdata.youtube.com/feeds/api/videos/0b2U5r7Jwkc"],
17
- ["link",nil],
18
- ["published","2011-09-29T15:30:43.000Z"],
19
- ["rating", nil],
20
- ["statistics",nil],
21
- ["title","Killer Whale Imitates Boat Motor"],
22
- ["updated","2011-10-14T07:40:00.000Z"],
23
- ["video_id", "0b2U5r7Jwkc"],
24
- ]
8
+ describe 'parse' do
9
+ it "can parse xml" do
10
+ # convert to array so we get a nice diff in case of errors
11
+ YoutubeSearch.parse(File.read('spec/fixtures/search_boat.xml')).first.sort.should == [
12
+ ["author",nil],
13
+ ["category", nil],
14
+ ["comments",nil],
15
+ ["content","Top YouTube Videos on tubecrunch.blogspot.com A killer whale swims right up to a boat and shows off his best sounding motor impression."],
16
+ ["group", nil],
17
+ ["id","http://gdata.youtube.com/feeds/api/videos/0b2U5r7Jwkc"],
18
+ ["link",nil],
19
+ ["published","2011-09-29T15:30:43.000Z"],
20
+ ["rating", nil],
21
+ ["statistics",nil],
22
+ ["title","Killer Whale Imitates Boat Motor"],
23
+ ["updated","2011-10-14T07:40:00.000Z"],
24
+ ["video_id", "0b2U5r7Jwkc"],
25
+ ]
26
+ end
25
27
  end
26
28
 
27
- it "can search" do
28
- YoutubeSearch.search('boat').size.should == 25
29
+ describe 'search' do
30
+ it "can search" do
31
+ YoutubeSearch.search('boat').size.should == 25
32
+ end
33
+
34
+ it "can search for complex examples" do
35
+ YoutubeSearch.search('Left 4 Dead 2').size.should == 25
36
+ end
37
+
38
+ it "can search with options" do
39
+ YoutubeSearch.search('Left 4 Dead 2', 'max-results' => 2).size.should == 2
40
+ end
41
+
42
+ it "can search with :per_page" do
43
+ YoutubeSearch.search('Left 4 Dead 2', :per_page => 2).size.should == 2
44
+ end
45
+ end
46
+
47
+ describe 'options_with_per_page_and_page' do
48
+ it "converts :page and :per_page" do
49
+ YoutubeSearch.send(:options_with_per_page_and_page, {:page => 2, :per_page => 4}).should ==
50
+ {"max-results"=>4, "start-index"=>4}
51
+ end
52
+
53
+ it "assumes page 1 for nil/0 page" do
54
+ YoutubeSearch.send(:options_with_per_page_and_page, {:page => '', :per_page => 4}).should ==
55
+ {"max-results"=>4, "start-index"=>0}
56
+ end
57
+
58
+ it "uses :page + max-results" do
59
+ YoutubeSearch.send(:options_with_per_page_and_page, {:page => 2, 'max-results' => 4}).should ==
60
+ {"max-results"=>4, "start-index"=>4}
61
+ end
62
+
63
+ it "uses start-index + :per_page" do
64
+ YoutubeSearch.send(:options_with_per_page_and_page, {'start-index' => 2, :per_page => 4}).should ==
65
+ {"max-results"=>4, "start-index"=>2}
66
+ end
29
67
  end
30
68
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: youtube_search
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Grosser