Class: BetterSeo::Sitemap::Builder

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/better_seo/sitemap/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(urls: []) ⇒ Builder

Returns a new instance of Builder.



10
11
12
# File 'lib/better_seo/sitemap/builder.rb', line 10

def initialize(urls: [])
  @urls = urls
end

Instance Attribute Details

#urlsObject (readonly)

Returns the value of attribute urls.



8
9
10
# File 'lib/better_seo/sitemap/builder.rb', line 8

def urls
  @urls
end

Instance Method Details

#add_url(location, lastmod: nil, changefreq: "weekly", priority: 0.5) ⇒ Object



14
15
16
17
# File 'lib/better_seo/sitemap/builder.rb', line 14

def add_url(location, lastmod: nil, changefreq: "weekly", priority: 0.5)
  @urls << UrlEntry.new(location, lastmod: lastmod, changefreq: changefreq, priority: priority)
  self
end

#add_urls(locations, lastmod: nil, changefreq: "weekly", priority: 0.5) ⇒ Object



19
20
21
22
23
24
# File 'lib/better_seo/sitemap/builder.rb', line 19

def add_urls(locations, lastmod: nil, changefreq: "weekly", priority: 0.5)
  locations.each do |location|
    add_url(location, lastmod: lastmod, changefreq: changefreq, priority: priority)
  end
  self
end

#clearObject



31
32
33
34
# File 'lib/better_seo/sitemap/builder.rb', line 31

def clear
  @urls = []
  self
end

#each(&block) ⇒ Object



60
61
62
# File 'lib/better_seo/sitemap/builder.rb', line 60

def each(&block)
  @urls.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/better_seo/sitemap/builder.rb', line 56

def empty?
  @urls.empty?
end

#remove_url(location) ⇒ Object



26
27
28
29
# File 'lib/better_seo/sitemap/builder.rb', line 26

def remove_url(location)
  @urls.reject! { |url| url.loc == location }
  self
end

#sizeObject



52
53
54
# File 'lib/better_seo/sitemap/builder.rb', line 52

def size
  @urls.size
end

#to_xmlObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/better_seo/sitemap/builder.rb', line 36

def to_xml
  xml = []
  xml << '<?xml version="1.0" encoding="UTF-8"?>'
  xml << '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
  @urls.each do |url|
    xml << url.to_xml
  end
  xml << "</urlset>"
  xml.join("\n")
end

#validate!Object



47
48
49
50
# File 'lib/better_seo/sitemap/builder.rb', line 47

def validate!
  @urls.each(&:validate!)
  true
end