Class: BigSitemap::Builder

Inherits:
Builder::XmlMarkup
  • Object
show all
Defined in:
lib/big_sitemap/builder.rb

Constant Summary collapse

NAMESPACE =
'http://www.sitemaps.org/schemas/sitemap/0.9'
MAX_URLS =
50000

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Builder

Returns a new instance of Builder.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/big_sitemap/builder.rb', line 9

def initialize(options)
  @gzip = options.delete(:gzip)
  @max_urls = options.delete(:max_urls) || MAX_URLS
  @index = options.delete(:index)
  @paths = []
  @parts = 0

  if @filename = options.delete(:filename)
    options[:target] = _get_writer
  end

  super(options)

  @opened_tags = []
  _init_document
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)

add support for:

xml.open_foo!(attrs)
xml.close_foo!


85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/big_sitemap/builder.rb', line 85

def method_missing(method, *args, &block)
  if method.to_s =~ /^(open|close)_(.+)!$/
    operation, name = $1, $2
    name = "#{name}:#{args.shift}" if Symbol === args.first

    if 'open' == operation
      _open_tag(name, args.first)
    else
      _close_tag(name)
    end
  else
    super
  end
end

Instance Method Details

#add_url!(url, time = nil, frequency = nil, priority = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/big_sitemap/builder.rb', line 26

def add_url!(url, time = nil, frequency = nil, priority = nil)
  _rotate if @max_urls == @urls

  tag!(@index ? 'sitemap' : 'url') do
    loc url
    # W3C format is the subset of ISO 8601
    lastmod(time.utc.strftime('%Y-%m-%dT%H:%M:%S+00:00')) unless time.nil?
    changefreq(frequency) unless frequency.nil?
    priority(priority) unless priority.nil?
  end
  @urls += 1
end

#close!Object



39
40
41
42
# File 'lib/big_sitemap/builder.rb', line 39

def close!
  _close_document
  target!.close if target!.respond_to?(:close)
end

#paths!Object



44
45
46
# File 'lib/big_sitemap/builder.rb', line 44

def paths!
  @paths
end