Skip to content

Commit

Permalink
can search for recorded dates
Browse files Browse the repository at this point in the history
  • Loading branch information
winescout authored and kylejginavan committed Jun 3, 2011
1 parent 80b4168 commit 45c82cf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/youtube_it/request/video_search.rb
Expand Up @@ -41,6 +41,7 @@ def initialize(params={})
end

@url << build_query_params(to_youtube_params)
@url << fields_to_params(params.delete(:fields))
end

private
Expand All @@ -64,6 +65,18 @@ def to_youtube_params
}
end

def fields_to_params(fields)
return "" unless fields

if fields[:recorded]
if fields[:recorded].is_a? Range
"&fields=entry[xs:date(yt:recorded) > xs:date('#{formatted_date(fields[:recorded].first)}') and xs:date(yt:recorded) < xs:date('#{formatted_date(fields[:recorded].last)}')]"
else
"&fields=entry[xs:date(yt:recorded) = xs:date('#{formatted_date(fields[:recorded])}')]"
end
end
end

# Convert category symbols into strings and build the URL. GData requires categories to be capitalized.
# Categories defined like: categories => { :include => [:news], :exclude => [:sports], :either => [..] }
# or like: categories => [:news, :sports]
Expand Down Expand Up @@ -93,6 +106,15 @@ def tags_to_params(tags)
end
end

#youtube taked dates that look like 'YYYY-MM-DD'
def formatted_date(date)
return date if date.is_a? String
if date.respond_to? :strftime
date.strftime("%Y-%m-%d")
else
""
end
end
end
end
end
Expand Down
1 change: 1 addition & 0 deletions test/helper.rb
@@ -1,4 +1,5 @@
require 'rubygems'
require 'date'
require 'test/unit'
require 'pp'
require 'open-uri'
Expand Down
15 changes: 15 additions & 0 deletions test/test_video_search.rb
Expand Up @@ -137,4 +137,19 @@ def test_should_build_url_for_favorite_videos_by_user_paginate
request = YouTubeIt::Request::UserSearch.new(:favorites, :user => 'liz', :offset => 20, :max_results => 10)
assert_equal "http://gdata.youtube.com/feeds/api/users/liz/favorites?max-results=10&start-index=20&v=2", request.url
end

#-- Recorded Queries --------------------------------------------------------------------------------

def test_should_search_for_range_of_recoreded_on_dates
starts_at = Date.today - 4
ends_at = Date.today
request = YouTubeIt::Request::VideoSearch.new(:fields => {:recorded => (starts_at..ends_at)})
assert_equal "http://gdata.youtube.com/feeds/api/videos?v=2&fields=entry[xs:date(yt:recorded) > xs:date('#{starts_at.strftime('%Y-%m-%d')}') and xs:date(yt:recorded) < xs:date('#{ends_at.strftime('%Y-%m-%d')}')]", request.url
end

def test_should_search_for_range_of_recoreded_on_dates
recorded_at = Date.today
request = YouTubeIt::Request::VideoSearch.new(:fields => {:recorded => recorded_at})
assert_equal "http://gdata.youtube.com/feeds/api/videos?v=2&fields=entry[xs:date(yt:recorded) = xs:date('#{recorded_at.strftime('%Y-%m-%d')}')]", request.url
end
end

0 comments on commit 45c82cf

Please sign in to comment.