Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Video Sitemap Support #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ PATH

GEM
specs:
mocha (0.9.10)
rake
metaclass (0.0.1)
mocha (0.12.0)
metaclass (~> 0.0.1)
nokogiri (1.4.4)
rake (0.8.7)
shoulda (2.11.3)

PLATFORMS
Expand Down
25 changes: 25 additions & 0 deletions lib/big_sitemap/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ class Builder
HEADER_NAME = 'urlset'
HEADER_ATTRIBUTES = {
'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9',
'xmlns:video' => "http://www.google.com/schemas/sitemap-video/1.1",
'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
'xsi:schemaLocation' => "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
}

# as per http://support.google.com/webmasters/bin/answer.py?hl=en&answer=80472#1
VIDEO_ATTRIBUTES = %w(thumbnail_loc title description content_loc player_loc duration expiration_date rating view_count
publication_date family_friendly restriction gallery_loc price requires_subscription uploader platform live)

def initialize(options)
@gzip = options.delete(:gzip)
@max_urls = options.delete(:max_urls) || MAX_URLS
Expand Down Expand Up @@ -38,6 +43,26 @@ def add_url!(location, options={})
tag! 'changefreq', options[:change_frequency] || 'weekly'
tag! 'priority', options[:priority] if options[:priority]

if options[:video]
_open_tag 'video:video'

options[:video].each do |attribute, value_or_hash|
if value_or_hash.is_a?(Hash)
tag_value = value_or_hash.delete(:value)
opts = value_or_hash
else
tag_value = value_or_hash
opts = {}
end

tag_value = tag_value.utc.strftime('%Y-%m-%dT%H:%M:%S+00:00') if attribute.to_s[0..-5] == "_date"

tag! "video:#{attribute}", tag_value, opts
end

_close_tag 'video:video'
end

_close_tag 'url'

@urls += 1
Expand Down
24 changes: 21 additions & 3 deletions test/big_sitemap_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ def teardown
end

should 'contain one sitemap element' do
generate_sitemap { add '/' }
generate_sitemap { add '/' }
assert_equal 1, num_elements(sitemaps_index_file, 'sitemap')
end

should 'contain one loc element' do
generate_sitemap { add '/' }
generate_sitemap { add '/' }
assert_equal 1, num_elements(sitemaps_index_file, 'loc')
end

should 'contain one lastmod element' do
generate_sitemap { add '/' }
generate_sitemap { add '/' }
assert_equal 1, num_elements(sitemaps_index_file, 'lastmod')
end

Expand Down Expand Up @@ -178,6 +178,24 @@ def teardown
assert_equal 2, num_elements(second_sitemap_file, 'priority')
end

should 'allow adding video elements' do
video_title = "Maru Motel"
video_url = "http://www.youtube.com/embed/cvnPBZbth3E?rel=0"
video_date = Time.mktime(2010, 8, 29)

generate_sitemap do
add '/', :video => { :title => video_title,
:player_loc => { :value => video_url,
:allow_embed => true },
:publication_date => video_date
}
end

assert_equal video_title, elements(first_sitemap_file, "*[name()='video:title']").first.text
assert_equal video_url, elements(first_sitemap_file, "*[name()='video:player_loc']").first.text
assert_equal "true", elements(first_sitemap_file, "*[name()='video:player_loc']").first.attributes["allow_embed"].value
end

should 'not be gzipped' do
generate_sitemap(:gzip => false) { add '/' }
assert File.exists?(unzipped_first_sitemap_file)
Expand Down
4 changes: 2 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ def tmp_dir
end

def ns
{'s' => 'http://www.sitemaps.org/schemas/sitemap/0.9'}
{'s' => 'http://www.sitemaps.org/schemas/sitemap/0.9', 'v' => "http://www.google.com/schemas/sitemap-video/1.1"}
end

def elements(filename, el)
file_class = filename.include?('.gz') ? Zlib::GzipReader : File
data = Nokogiri::XML.parse(file_class.open(filename).read)
data.search("//s:#{el}", ns)
data.search("//s:#{el}", "//v:#{el}", ns)
end

def num_elements(filename, el)
Expand Down