Class: DynamicSitemaps::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic_sitemaps/generator.rb

Instance Method Summary collapse

Instance Method Details

#create_temp_dirObject



23
24
25
26
# File 'lib/dynamic_sitemaps/generator.rb', line 23

def create_temp_dir
  remove_temp_dir
  FileUtils.mkdir_p DynamicSitemaps.temp_path
end

#ensure_valid_sitemap_name!(sitemap) ⇒ Object

Raises:

  • (ArgumentError)


69
70
71
72
# File 'lib/dynamic_sitemaps/generator.rb', line 69

def ensure_valid_sitemap_name!(sitemap)
  raise ArgumentError, "Sitemap name :#{sitemap.name} has already been defined for the folder \"#{sitemap.folder}\". Please use `sitemap :other_name do ... end` or `sitemap_for <relation>, name: :other_name`." if sitemap_names[sitemap.folder].include?(sitemap.name)
  raise ArgumentError, "Sitemap name :#{sitemap.name} conflicts with the index file name #{DynamicSitemaps.index_file_name}. Please change it using `sitemap :other_name do ... end`." if "#{sitemap.name}.xml" == DynamicSitemaps.index_file_name
end

#folder(*args) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/dynamic_sitemaps/generator.rb', line 103

def folder(*args)
  if args.any?
    @folder = args.first
    raise ArgumentError, "Folder can't be blank." if @folder.blank?
  else
    # Ensure that the default folder is set and cleaned.
    folder DynamicSitemaps.folder if @folder.blank?

    @folder
  end
end

#generate(&block) ⇒ Object

Generates the sitemap(s) and index based on the configuration file specified in DynamicSitemaps.config_path. If you supply a block, that block is evaluated instead of the configuration file.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/dynamic_sitemaps/generator.rb', line 5

def generate(&block)
  create_temp_dir
  if block
    instance_eval &block
  else
    instance_eval open(DynamicSitemaps.config_path).read, DynamicSitemaps.config_path
  end
  generate_index
  move_to_destination
  ping_search_engines
ensure
  remove_temp_dir
end

#generate_indexObject



19
20
21
# File 'lib/dynamic_sitemaps/generator.rb', line 19

def generate_index
  IndexGenerator.new(sitemaps).generate
end

#host(*args) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/dynamic_sitemaps/generator.rb', line 89

def host(*args)
  if args.any?
    @host = args.first
    Rails.application.routes.default_url_options[:host] = @host
  else
    @host
  end
end

#move_to_destinationObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dynamic_sitemaps/generator.rb', line 32

def move_to_destination
  sitemaps.map(&:folder).uniq.each do |folder|
    destination = File.join(DynamicSitemaps.path, folder)
    FileUtils.mkdir_p destination
    FileUtils.rm_rf Dir.glob(File.join(destination, "*"))

    temp_files = File.join(DynamicSitemaps.temp_path, folder, "*.xml")
    FileUtils.mv Dir.glob(temp_files), destination
  end
  remove_temp_dir
end

#ping_search_enginesObject



44
45
46
# File 'lib/dynamic_sitemaps/generator.rb', line 44

def ping_search_engines
  Pinger.ping_search_engines_with ping_urls
end

#ping_urlsObject

URLs to ping after generation



85
86
87
# File 'lib/dynamic_sitemaps/generator.rb', line 85

def ping_urls
  @ping_urls ||= []
end

#ping_with(sitemap_url) ⇒ Object

Adds a sitemap URL to ping search engines with after generation.



99
100
101
# File 'lib/dynamic_sitemaps/generator.rb', line 99

def ping_with(sitemap_url)
  ping_urls << sitemap_url
end

#remove_temp_dirObject



28
29
30
# File 'lib/dynamic_sitemaps/generator.rb', line 28

def remove_temp_dir
  FileUtils.rm_rf DynamicSitemaps.temp_path
end

#sitemap(*args, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dynamic_sitemaps/generator.rb', line 48

def sitemap(*args, &block)
  args << {} unless args.last.is_a?(Hash)
  args.last[:host] ||= host
  args.last[:folder] ||= folder
  sitemap = Sitemap.new(*args, &block)

  ensure_valid_sitemap_name! sitemap
  sitemap_names[sitemap.folder] << sitemap.name

  sitemaps << SitemapGenerator.new(sitemap).generate
end

#sitemap_for(collection, options = {}, &block) ⇒ Object

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
# File 'lib/dynamic_sitemaps/generator.rb', line 60

def sitemap_for(collection, options = {}, &block)
  raise ArgumentError, "The collection given to `sitemap_for` must respond to #find_each. This is for performance. Use `Model.scoped` to get an ActiveRecord relation that responds to #find_each." unless collection.respond_to?(:find_each)

  name = options.delete(:name) || collection.model_name.to_s.underscore.pluralize.to_sym
  options[:collection] = collection

  sitemap(name, options, &block)
end

#sitemap_namesObject

Generated sitemap names



80
81
82
# File 'lib/dynamic_sitemaps/generator.rb', line 80

def sitemap_names
  @sitemap_names ||= Hash.new { |h, k| h[k] = [] }
end

#sitemapsObject

Array of SitemapResult



75
76
77
# File 'lib/dynamic_sitemaps/generator.rb', line 75

def sitemaps
  @sitemaps ||= []
end