Class: Viddler::SitemapFile

Inherits:
Object
  • Object
show all
Defined in:
lib/viddler/sitemap_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSitemapFile

Returns a new instance of SitemapFile.



6
7
8
# File 'lib/viddler/sitemap_file.rb', line 6

def initialize
  @videos = []
end

Instance Attribute Details

#videosObject (readonly)

An array of video hash object that will be included in the SitemapFile



4
5
6
# File 'lib/viddler/sitemap_file.rb', line 4

def videos
  @videos
end

Instance Method Details

#add_video(video) ⇒ Object

Add a video to this sitemap file

Parameters:

  • video (Hash)

    the video details hash from the API response



12
13
14
15
# File 'lib/viddler/sitemap_file.rb', line 12

def add_video(video)
  @videos << video
  return true
end

#to_xmlString

The XML string for this SitemapFile

Returns:

  • (String)

    the XML



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/viddler/sitemap_file.rb', line 19

def to_xml
  @builder = Builder::XmlMarkup.new(:indent => 2)
  @builder.instruct!(:xml, :version => '1.0', :encoding => 'UTF-8')

  @builder.urlset('xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9',
                  'xmlns:video' => 'http://www.google.com/schemas/sitemap-video/1.1') do | xml|
    @videos.each do |video|
      downloadable = video['permissions'] && video['permissions']['download'] != 'private'
      embedable    = video['permissions'] && video['permissions']['embed'] != 'private'

      xml.url do |xml|
        xml.loc(video['permalink'])
        xml.tag!('video:video') do
          xml.tag!('video:thumbnail_loc', "http://www.viddler.com/thumbnail/#{video['id']}")
          xml.tag!('video:title', video['title'])
          xml.tag!('video:description', video['description'])
          if downloadable
            xml.tag!('video:content_loc', video['files'].first['url'])
          end
          xml.tag!('video:player_loc', "http://www.viddler.com/embed/#{video['id']}", :allow_embed => embedable ? 'yes' : 'no')
          xml.tag!('video:duration', video['length'])
          xml.tag!('video:view_count', video['view_count'])
          xml.tag!('video:publication_date', Time.at(video['upload_time'].to_i).strftime('%Y-%m-%d'))
          xml.tag!('video:family_friendly', 'yes')
          xml.tag!('video:live', 'no')
        end
      end
    end
  end
  return @builder.target!
end

#write!(folder, file_number) ⇒ Object

Write the xml content for this SitemapFile to disk

Parameters:

  • folder (String)

    The path to the folder where the sitemaps should be written

  • file_number (Integer)

    The number of this file. Because there is a maximum number of videos per file, there may be multiple files.



54
55
56
57
# File 'lib/viddler/sitemap_file.rb', line 54

def write!(folder, file_number)
  path = "#{folder}/sitemap-#{file_number}.xml"
  File.open(path, 'w') { |f| f.write(self.to_xml) }
end