Module: MassiveSitemap

Extended by:
MassiveSitemap
Included in:
MassiveSitemap
Defined in:
lib/massive_sitemap.rb,
lib/massive_sitemap/lock.rb,
lib/massive_sitemap/ping.rb,
lib/massive_sitemap/builder.rb,
lib/massive_sitemap/writer/base.rb,
lib/massive_sitemap/writer/file.rb,
lib/massive_sitemap/builder/base.rb,
lib/massive_sitemap/builder/index.rb,
lib/massive_sitemap/writer/string.rb,
lib/massive_sitemap/builder/rotating.rb,
lib/massive_sitemap/writer/gzip_file.rb

Overview

MassiveSitemap Writer GzipFile Extension to file writer for gzip support

Defined Under Namespace

Modules: Builder, Writer

Constant Summary collapse

DEFAULTS =
{
  # global
  :index_url       => nil,
  :index_filename  => "sitemap_index.xml",
  :gzip            => false,

  # writer
  :root            => '.',
  :force_overwrite => false,
  :filename        => "sitemap.xml",

  # builder
  :url             => nil,
  :indent_by       => 2,
}
LOCK_FILE =
'generator.lock'
ENGINES_URLS =
{
  :google => 'http://www.google.com/webmasters/tools/ping?sitemap=%s',
  :bing   => 'http://www.bing.com/webmaster/ping.aspx?siteMap=%s',
  :ask    => 'http://submissions.ask.com/ping?sitemap=%s',
  :yandex => 'http://webmaster.yandex.ru/wmconsole/sitemap_list.xml?host=%s',
}
DEFAULT_ENGINES =

ask seems to be down, so disable for now by default

[:google, :bing, :yandex]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate(options = {}, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/massive_sitemap.rb', line 39

def generate(options = {}, &block)
  lock! do
    @options = DEFAULTS.merge options

    unless @options[:url]
      raise ArgumentError, %Q(":url" not given)
    end
    @options[:index_url] ||= @options[:url]

    if @options[:max_urls] && !Builder::Rotating::NUM_URLS.member?(@options[:max_urls])
      raise ArgumentError, %Q(":max_urls" must be greater than #{Builder::Rotating::NUM_URLS.min} and smaller than #{Builder::Rotating::NUM_URLS.max})
    end

    @writer   = @options.delete(:writer)
    @writer ||= (@options.delete(:gzip) ? Writer::GzipFile : Writer::File).new

    Builder::Rotating.generate(@writer.set(@options), @options, &block)

    @writer.set(:filename => @options[:index_filename])
    Builder::Index.generate(@writer, @options.merge(:url => @options[:index_url]))
  end
end

.lock!(&block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/massive_sitemap/lock.rb', line 9

def lock!(&block)
  if block
    raise Errno::EACCES if ::File.exists?(LOCK_FILE)
    ::File.open(LOCK_FILE, 'w', ::File::EXCL)
    begin
      block.call
    ensure
      FileUtils.rm(LOCK_FILE) #unlock!
    end
  end
end

Instance Method Details

#ping(url, engines = DEFAULT_ENGINES) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/massive_sitemap/ping.rb', line 19

def ping(url, engines = DEFAULT_ENGINES)
  url =  verify_and_escape(url)

  Array(engines).each do |engine|
    if engine_url = ENGINES_URLS[engine]
      begin
        open(engine_url % url)
      rescue => e
        log_error(engine, e)
      end
    end
  end
end