Class: BigSitemap

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

Direct Known Subclasses

BigSitemapMerb, BigSitemapRails

Defined Under Namespace

Classes: Builder, IndexBuilder

Constant Summary collapse

DEFAULTS =
{
  :max_per_sitemap => Builder::MAX_URLS,
  :batch_size      => 1001, # TODO: Deprecate
  :document_path   => '/',
  :gzip            => true,

  # Opinionated
  :ping_google => true,
  :ping_yahoo  => false, # needs :yahoo_app_id
  :ping_bing   => false,
  :ping_ask    => false
}
COUNT_METHODS =

TODO: Deprecate

[:count_for_sitemap, :count]
FIND_METHODS =
[:find_for_sitemap, :all]
TIMESTAMP_METHODS =
[:updated_at, :updated_on, :updated, :created_at, :created_on, :created]
PARAM_METHODS =
[:to_param, :id]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BigSitemap

Returns a new instance of BigSitemap.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/big_sitemap.rb', line 52

def initialize(options={})
  @options = DEFAULTS.merge options

  if @options[:max_per_sitemap] <= 1
    raise ArgumentError, '":max_per_sitemap" must be greater than 1'
  end

  if @options[:url_options] && !@options[:base_url]
    @options[:base_url] = URI::Generic.build( {:scheme => "http"}.merge(@options.delete(:url_options)) ).to_s
  end

  unless @options[:base_url]
    raise ArgumentError, 'you must specify either ":url_options" hash or ":base_url" string'
  end
  @options[:url_path] ||= @options[:document_path]

  unless @options[:document_root]
    raise ArgumentError, 'Document root must be specified with the ":document_root" option"'
  end

  @options[:document_full] ||= File.join(@options[:document_root], @options[:document_path])
  unless @options[:document_full]
    raise ArgumentError, 'Document root must be specified with the ":document_root" option, the full path with ":document_full"'
  end

  Dir.mkdir(@options[:document_full]) unless File.exists?(@options[:document_full])

  @sources       = []
  @models        = []
  @sitemap_files = []
end

Class Method Details

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



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

def generate(options={}, &block)
  @sitemap = self.new(options)

  @sitemap.first_id_of_last_sitemap = first_id_of_last_sitemap

  instance_eval(&block)

  @sitemap.with_lock do
    @sitemap.generate(options)
  end
end

Instance Method Details

#add(model, options = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/big_sitemap.rb', line 96

def add(model, options={})
  warn 'BigSitemap#add is deprecated.  Please use BigSitemap.generate and call add inside the block (in BigSitemap 1.0.0+).  You will have to perform the find and generate the path for each record yourself.'
  @models << model

  filename_suffix = @models.count(model) - 1

  options[:path]           ||= table_name(model)
  options[:filename]       ||= file_name(model)
  options[:primary_column] ||= 'id' if model.new.respond_to?('id')
  options[:partial_update]   = @options[:partial_update] && options[:partial_update] != false

  options[:filename] << "_#{filename_suffix}" unless filename_suffix == 0

  @sources << [model, options.dup]

  self
end

#add_path(path, options) ⇒ Object



114
115
116
117
118
# File 'lib/big_sitemap.rb', line 114

def add_path(path, options)
  @paths ||= []
  @paths << [path, options]
  self
end

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



120
121
122
123
124
125
# File 'lib/big_sitemap.rb', line 120

def add_static(url, time = nil, frequency = nil, priority = nil)
  warn 'BigSitemap#add_static is deprecated.  Please use BigSitemap#add_path instead'
  @static_pages ||= []
  @static_pages << [url, time, frequency, priority]
  self
end

#add_urlsObject



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/big_sitemap.rb', line 177

def add_urls
  return self if Array(@paths).empty?

  with_sitemap do |builder|
    @paths.uniq!
    @paths.each do |path, options|
      url = URI.join(@options[:base_url], path)
      builder.add_url! url, options
    end
  end

  self
end

#cleanObject



149
150
151
152
153
154
155
# File 'lib/big_sitemap.rb', line 149

def clean
  Dir[dir_files].each do |file|
    FileUtils.rm file
  end

  self
end

#dir_filesObject



145
146
147
# File 'lib/big_sitemap.rb', line 145

def dir_files
  File.join(@options[:document_full], "sitemap*.{xml,xml.gz}")
end

#document_fullObject



92
93
94
# File 'lib/big_sitemap.rb', line 92

def document_full
  @options[:document_full]
end

#file_name(name = nil) ⇒ Object



138
139
140
141
142
143
# File 'lib/big_sitemap.rb', line 138

def file_name(name=nil)
  name   = table_name(name) unless (name.nil? || name.is_a?(String))
  prefix = 'sitemap'
  prefix << '_' unless name.nil?
  File.join(@options[:document_full], "#{prefix}#{name}")
end

#first_id_of_last_sitemapObject



84
85
86
# File 'lib/big_sitemap.rb', line 84

def first_id_of_last_sitemap
  @first_id_of_last_sitemap
end

#first_id_of_last_sitemap=(first_id) ⇒ Object



88
89
90
# File 'lib/big_sitemap.rb', line 88

def first_id_of_last_sitemap=(first_id)
  @first_id_of_last_sitemap = first_id
end

#generate(options = {}) ⇒ Object

TODO: Deprecate (move to private)



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/big_sitemap.rb', line 158

def generate(options={})
  clean unless options[:partial_update]

  # TODO: Ddeprecate
  prepare_update

  add_urls

  # TODO: Deprecate
  generate_models
  generate_static

  generate_sitemap_index

  ping_search_engines

  self
end

#generate_sitemap_index(files = nil) ⇒ Object

Create a sitemap index document



192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/big_sitemap.rb', line 192

def generate_sitemap_index(files=nil)
  files ||= Dir[dir_files]

  with_sitemap({:name => 'index', :type => 'index'}) do |sitemap|
    for path in files
      next if path =~ /index/
      sitemap.add_url! url_for_sitemap(path), :last_modified => File.stat(path).mtime
    end
  end

  self
end

#get_last_id(filename) ⇒ Object

TODO: Deprecate



236
237
238
239
240
# File 'lib/big_sitemap.rb', line 236

def get_last_id(filename)
  Dir["#{filename}*.{xml,xml.gz}"].map do |file|
    file.to_s.scan(/#{filename}_(.+).xml/).flatten.last.to_i
  end.sort.last
end

#ping_search_enginesObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/big_sitemap.rb', line 205

def ping_search_engines
  require 'net/http'
  require 'cgi'

  sitemap_uri = CGI::escape(url_for_sitemap(@sitemap_files.last))

  if @options[:ping_google]
    Net::HTTP.get('www.google.com', "/webmasters/tools/ping?sitemap=#{sitemap_uri}")
  end

  if @options[:ping_yahoo]
    if @options[:yahoo_app_id]
      Net::HTTP.get(
        'search.yahooapis.com', "/SiteExplorerService/V1/updateNotification?" +
          "appid=#{@options[:yahoo_app_id]}&url=#{sitemap_uri}"
      )
    else
      STDERR.puts 'unable to ping Yahoo: no ":yahoo_app_id" provided'
    end
  end

  if @options[:ping_bing]
    Net::HTTP.get('www.bing.com', "/webmaster/ping.aspx?siteMap=#{sitemap_uri}")
  end

  if @options[:ping_ask]
    Net::HTTP.get('submissions.ask.com', "/ping?sitemap=#{sitemap_uri}")
  end
end

#with_lockObject



127
128
129
130
131
132
133
134
135
136
# File 'lib/big_sitemap.rb', line 127

def with_lock
  lock!
  begin
    yield
  ensure
    unlock!
  end
rescue Errno::EACCES => e
  STDERR.puts 'Lockfile exists' if $VERBOSE
end