Method: SitemapGenerator::Builder::SitemapUrl#initialize

Defined in:
lib/sitemap_generator/builder/sitemap_url.rb

#initialize(path, options = {}) ⇒ SitemapUrl

Return a new instance with options configured on it.

Arguments

  • sitemap - a Sitemap instance, or

  • path, options - a path string and options hash

Options

Requires a host to be set. If passing a sitemap, the sitemap must have a default_host configured. If calling with a path and options, you must include the :host option.

  • host

  • priority

  • changefreq

  • lastmod

  • images

  • video/videos

  • geo

  • news

  • mobile

  • alternate/alternates

  • pagemap



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sitemap_generator/builder/sitemap_url.rb', line 33

def initialize(path, options={})
  options = options.dup
  if sitemap = path.is_a?(SitemapGenerator::Builder::SitemapFile) && path
    SitemapGenerator::Utilities.reverse_merge!(options, :host => sitemap.location.host, :lastmod => sitemap.lastmod)
    path = sitemap.location.path_in_public
  end

  SitemapGenerator::Utilities.assert_valid_keys(options, :priority, :changefreq, :lastmod, :expires, :host, :images, :video, :geo, :news, :videos, :mobile, :alternate, :alternates, :pagemap)
  SitemapGenerator::Utilities.reverse_merge!(options, :priority => 0.5, :changefreq => 'weekly', :lastmod => Time.now, :images => [], :news => {}, :videos => [], :mobile => false, :alternates => [])
  raise "Cannot generate a url without a host" unless SitemapGenerator::Utilities.present?(options[:host])

  if video = options.delete(:video)
    options[:videos] = video.is_a?(Array) ? options[:videos].concat(video) : options[:videos] << video
  end
  if alternate = options.delete(:alternate)
    options[:alternates] = alternate.is_a?(Array) ? options[:alternates].concat(alternate) : options[:alternates] << alternate
  end

  path = path.to_s.sub(/^\//, '')
  loc  = path.empty? ? options[:host] : (options[:host].to_s.sub(/\/$/, '') + '/' + path)
  self.merge!(
    :priority   => options[:priority],
    :changefreq => options[:changefreq],
    :lastmod    => options[:lastmod],
    :expires    => options[:expires],
    :host       => options[:host],
    :loc        => loc,
    :images     => prepare_images(options[:images], options[:host]),
    :news       => prepare_news(options[:news]),
    :videos     => options[:videos],
    :geo        => options[:geo],
    :mobile     => options[:mobile],
    :alternates => options[:alternates],
    :pagemap    => options[:pagemap]
  )
end