Class: SitemapGenerator::Builder::SitemapFile

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/sitemap_generator/builder/sitemap_file.rb

Direct Known Subclasses

SitemapIndexFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#w3c_date

Constructor Details

#initialize(public_path, sitemap_path, hostname) ⇒ SitemapFile

public_path full path of the directory to write sitemaps in.

Usually your Rails <tt>public/</tt> directory.

sitemap_path relative path including filename of the sitemap

file relative to <tt>public_path</tt>

hostname hostname including protocol to use in all links

e.g. http://en.google.ca


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sitemap_generator/builder/sitemap_file.rb', line 20

def initialize(public_path, sitemap_path, hostname)
  self.sitemap_path = sitemap_path
  self.public_path = public_path
  self.hostname = hostname
  self.link_count = 0

  @xml_content       = ''     # XML urlset content
  @xml_wrapper_start = <<-HTML
    <?xml version="1.0" encoding="UTF-8"?>
      <urlset
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
          http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
        xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      >
  HTML
  @xml_wrapper_start.gsub!(/\s+/, ' ').gsub!(/ *> */, '>').strip!
  @xml_wrapper_end   = %q[</urlset>]
  self.filesize = @xml_wrapper_start.length + @xml_wrapper_end.length
end

Instance Attribute Details

#filesizeObject

Returns the value of attribute filesize.



10
11
12
# File 'lib/sitemap_generator/builder/sitemap_file.rb', line 10

def filesize
  @filesize
end

#hostnameObject

Returns the value of attribute hostname.



10
11
12
# File 'lib/sitemap_generator/builder/sitemap_file.rb', line 10

def hostname
  @hostname
end

Returns the value of attribute link_count.



10
11
12
# File 'lib/sitemap_generator/builder/sitemap_file.rb', line 10

def link_count
  @link_count
end

#public_pathObject

Returns the value of attribute public_path.



10
11
12
# File 'lib/sitemap_generator/builder/sitemap_file.rb', line 10

def public_path
  @public_path
end

#sitemap_pathObject

Returns the value of attribute sitemap_path.



10
11
12
# File 'lib/sitemap_generator/builder/sitemap_file.rb', line 10

def sitemap_path
  @sitemap_path
end

Instance Method Details

Add a link to the sitemap file and return a boolean indicating whether the link was added.

If a link cannot be added, the file is too large or the link limit has been reached.



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sitemap_generator/builder/sitemap_file.rb', line 68

def add_link(link)
  xml = build_xml(::Builder::XmlMarkup.new, link)
  unless file_can_fit?(xml.length)
    self.finalize!
    return false
  end

  @xml_content << xml
  self.filesize += xml.length
  self.link_count += 1
  true
end

#build_xml(builder, link) ⇒ Object

Return XML as a String



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sitemap_generator/builder/sitemap_file.rb', line 83

def build_xml(builder, link)
  builder.url do
    builder.loc        link[:loc]
    builder.lastmod    w3c_date(link[:lastmod])   if link[:lastmod]
    builder.changefreq link[:changefreq]          if link[:changefreq]
    builder.priority   link[:priority]            if link[:priority]

    unless link[:images].blank?
      link[:images].each do |image|
        builder.image:image do
          builder.image :loc, image[:loc]
          builder.image :caption, image[:caption]             if image[:caption]
          builder.image :geo_location, image[:geo_location]   if image[:geo_location]
          builder.image :title, image[:title]                 if image[:title]
          builder.image :license, image[:license]             if image[:license]
        end
      end
    end
  end
  builder << ''
end

#empty?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/sitemap_generator/builder/sitemap_file.rb', line 46

def empty?
  self.link_count == 0
end

#file_can_fit?(bytes) ⇒ Boolean

Return a boolean indicating whether the sitemap file can fit another link of bytes bytes in size.

Returns:

  • (Boolean)


60
61
62
# File 'lib/sitemap_generator/builder/sitemap_file.rb', line 60

def file_can_fit?(bytes)
  (self.filesize + bytes) < SitemapGenerator::MAX_SITEMAP_FILESIZE && self.link_count < SitemapGenerator::MAX_SITEMAP_LINKS
end

#finalize!Object

Insert the content into the XML “wrapper” and write and close the file.

All the xml content in the instance is cleared, but attributes like filesize are still available.



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/sitemap_generator/builder/sitemap_file.rb', line 109

def finalize!
  return if self.frozen?

  open(self.full_path, 'wb') do |file|
    gz = Zlib::GzipWriter.new(file)
    gz.write @xml_wrapper_start
    gz.write @xml_content
    gz.write @xml_wrapper_end
    gz.close
  end
  @xml_content = @xml_wrapper_start = @xml_wrapper_end = ''
  self.freeze
end

#full_pathObject



54
55
56
# File 'lib/sitemap_generator/builder/sitemap_file.rb', line 54

def full_path
  @full_path ||= File.join(self.public_path, self.sitemap_path)
end

#full_urlObject



50
51
52
# File 'lib/sitemap_generator/builder/sitemap_file.rb', line 50

def full_url
  URI.join(self.hostname, self.sitemap_path).to_s
end

#lastmodObject



42
43
44
# File 'lib/sitemap_generator/builder/sitemap_file.rb', line 42

def lastmod
  File.mtime(self.full_path) rescue nil
end