Class: Cartography::Sitemap

Inherits:
Object
  • Object
show all
Defined in:
lib/cartography/sitemap.rb

Instance Method Summary collapse

Constructor Details

#initialize(baseUrl, options = {}) ⇒ Sitemap

Returns a new instance of Sitemap.



4
5
6
7
8
9
# File 'lib/cartography/sitemap.rb', line 4

def initialize(baseUrl, options = {})
  @options = {}
  @options.merge(options)
  @baseUrl = baseUrl
  @urls = []
end

Instance Method Details

#add(url, lastmod = nil, changefreq = nil, priority = nil) ⇒ Object

Adds a url to the array of urls.



41
42
43
# File 'lib/cartography/sitemap.rb', line 41

def add(url, lastmod = nil, changefreq = nil, priority = nil)
  @urls << {:url => url, :lastmod => lastmod, :changefreq => changefreq, :priority => priority}
end

#to_file(path) ⇒ Object

writes an XML string of the sitemap to the specified file.



36
37
38
# File 'lib/cartography/sitemap.rb', line 36

def to_file(path)
  return 
end

#to_sObject

Returns an XML string of the sitemap



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cartography/sitemap.rb', line 12

def to_s
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.urlset(:xmlns=>"http://www.sitemaps.org/schemas/sitemap/0.9"){
      @urls.each do |url|
        xml.url{
          xml.loc_ "#{@baseUrl}#{url[:url]}"
          unless url[:lastmod].nil?
            xml.lastmod url[:lastmod].to_s
          end
          unless url[:changefreq].nil?
            xml.changefreq url[:changefreq].to_s
          end
          unless url[:priority].nil?
            xml.priority url[:priority].to_s
          end
        }
      end
    }
  end

  return builder.to_xml
end