Class: SitemapGenerator::LinkSet

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper
Defined in:
lib/sitemap_generator/link_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(public_path = nil, sitemaps_path = nil, default_host = nil) ⇒ LinkSet

public_path (optional) full path to the directory to write sitemaps in.

Defaults to your Rails <tt>public/</tt> directory.

sitemaps_path (optional) path fragment within public to write sitemaps

to e.g. 'en/'.  Sitemaps are written to <tt>public_path</tt> + <tt>sitemaps_path</tt>

default_host hostname including protocol to use in all sitemap links

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


39
40
41
42
43
44
45
46
# File 'lib/sitemap_generator/link_set.rb', line 39

def initialize(public_path = nil, sitemaps_path = nil, default_host = nil)
  self.default_host = default_host
  self.public_path = public_path
  self.sitemaps_path = sitemaps_path

  # Completed sitemaps
  self.sitemaps = []
end

Instance Attribute Details

#default_hostObject

Returns the value of attribute default_host.



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

def default_host
  @default_host
end

#public_pathObject

Returns the value of attribute public_path.



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

def public_path
  @public_path
end

#sitemapObject

Returns the value of attribute sitemap.



11
12
13
# File 'lib/sitemap_generator/link_set.rb', line 11

def sitemap
  @sitemap
end

#sitemap_indexObject

Returns the value of attribute sitemap_index.



11
12
13
# File 'lib/sitemap_generator/link_set.rb', line 11

def sitemap_index
  @sitemap_index
end

#sitemapsObject

Returns the value of attribute sitemaps.



11
12
13
# File 'lib/sitemap_generator/link_set.rb', line 11

def sitemaps
  @sitemaps
end

#sitemaps_pathObject

Returns the value of attribute sitemaps_path.



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

def sitemaps_path
  @sitemaps_path
end

#verboseObject

Returns the value of attribute verbose.



12
13
14
# File 'lib/sitemap_generator/link_set.rb', line 12

def verbose
  @verbose
end

#yahoo_app_idObject

Returns the value of attribute yahoo_app_id.



12
13
14
# File 'lib/sitemap_generator/link_set.rb', line 12

def yahoo_app_id
  @yahoo_app_id
end

Instance Method Details

Called from Mapper.

Add a link to the current sitemap.



70
71
72
73
74
75
# File 'lib/sitemap_generator/link_set.rb', line 70

def add_link(link)
  unless self.sitemap << link
    new_sitemap
    self.sitemap << link
  end
end

Called within the user’s eval’ed sitemap config file. Add links to sitemap files passing a block.

TODO: Refactor. The call chain is confusing and convoluted here.

Yields:

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
# File 'lib/sitemap_generator/link_set.rb', line 56

def add_links
  raise ArgumentError, "Default hostname not set" if default_host.blank?

  # I'd rather have these calls in <tt>create</tt> but we have to wait
  # for <tt>default_host</tt> to be set by the user's sitemap config
  new_sitemap
  add_default_links

  yield Mapper.new(self)
end

#createObject

Evaluate the sitemap config file and write all sitemaps.

This should be refactored so that we can have multiple instances of LinkSet.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sitemap_generator/link_set.rb', line 18

def create
  require 'sitemap_generator/interpreter'

  self.public_path = File.join(::Rails.root, 'public/') if self.public_path.nil?

  start_time = Time.now
  SitemapGenerator::Interpreter.run
  finalize!
  end_time = Time.now

  puts "\nSitemap stats: #{number_with_delimiter(self.link_count)} links / #{self.sitemaps.size} files / " + ("%dm%02ds" % (end_time - start_time).divmod(60)) if verbose
end

#finalize!Object

Finalize all sitemap files



109
110
111
112
# File 'lib/sitemap_generator/link_set.rb', line 109

def finalize!
  new_sitemap
  self.sitemap_index.finalize!
end


48
49
50
# File 'lib/sitemap_generator/link_set.rb', line 48

def link_count
  self.sitemaps.inject(0) { |link_count_sum, sitemap| link_count_sum + sitemap.link_count }
end

#new_sitemapObject

Add the current sitemap to the sitemaps Array and start a new sitemap.

If the current sitemap is nil or empty it is not added.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/sitemap_generator/link_set.rb', line 81

def new_sitemap
  unless self.sitemap_index
    self.sitemap_index = SitemapGenerator::Builder::SitemapIndexFile.new(public_path, sitemap_index_path, default_host)
  end

  unless self.sitemap
    self.sitemap = SitemapGenerator::Builder::SitemapFile.new(public_path, new_sitemap_path, default_host)
  end

  # Mark the sitemap as complete and add it to the sitemap index
  unless self.sitemap.empty?
    self.sitemap.finalize!
    self.sitemap_index << Link.generate(self.sitemap)
    self.sitemaps << self.sitemap
    show_progress(self.sitemap) if verbose

    self.sitemap = SitemapGenerator::Builder::SitemapFile.new(public_path, new_sitemap_path, default_host)
  end
end

#ping_search_enginesObject

Ping search engines.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/sitemap_generator/link_set.rb', line 117

def ping_search_engines
  require 'open-uri'

  sitemap_index_url = CGI.escape(self.sitemap_index.full_url)
  search_engines = {
    :google         => "http://www.google.com/webmasters/sitemaps/ping?sitemap=#{sitemap_index_url}",
    :yahoo          => "http://search.yahooapis.com/SiteExplorerService/V1/ping?sitemap=#{sitemap_index_url}&appid=#{yahoo_app_id}",
    :ask            => "http://submissions.ask.com/ping?sitemap=#{sitemap_index_url}",
    :bing           => "http://www.bing.com/webmaster/ping.aspx?siteMap=#{sitemap_index_url}",
    :sitemap_writer => "http://www.sitemapwriter.com/notify.php?crawler=all&url=#{sitemap_index_url}"
  }

  puts "\n" if verbose
  search_engines.each do |engine, link|
    next if engine == :yahoo && !self.yahoo_app_id
    begin
      open(link)
      puts "Successful ping of #{engine.to_s.titleize}" if verbose
    rescue Timeout::Error, StandardError => e
      puts "Ping failed for #{engine.to_s.titleize}: #{e.inspect} (URL #{link})" if verbose
    end
  end

  if !self.yahoo_app_id && verbose
    puts "\n"
    puts <<-END.gsub(/^\s+/, '')
      To ping Yahoo you require a Yahoo AppID.  Add it to your config/sitemap.rb with:

      SitemapGenerator::Sitemap.yahoo_app_id = "my_app_id"

      For more information see http://developer.yahoo.com/search/siteexplorer/V1/updateNotification.html
    END
  end
end

#show_progress(sitemap) ⇒ Object

Report progress line.



102
103
104
105
106
# File 'lib/sitemap_generator/link_set.rb', line 102

def show_progress(sitemap)
  uncompressed_size = number_to_human_size(sitemap.filesize)
  compressed_size =   number_to_human_size(File.size?(sitemap.full_path))
  puts "+ #{sitemap.sitemap_path}   #{sitemap.link_count} links / #{uncompressed_size} / #{compressed_size} gzipped"
end