Class: DynamicSitemaps::SitemapGenerator
- Inherits:
-
Object
- Object
- DynamicSitemaps::SitemapGenerator
- Defined in:
- lib/dynamic_sitemaps/sitemap_generator.rb
Instance Attribute Summary collapse
- #counter ⇒ Object
- #page ⇒ Object
-
#sitemap ⇒ Object
readonly
Returns the value of attribute sitemap.
Instance Method Summary collapse
- #ensure_host! ⇒ Object
- #file ⇒ Object
- #file_name ⇒ Object
- #files ⇒ Object
- #folder ⇒ Object
- #folder_path ⇒ Object
- #format_date(date) ⇒ Object
- #format_url(url) ⇒ Object
- #generate ⇒ Object
- #handle_collection ⇒ Object
- #host ⇒ Object
- #increment_counter ⇒ Object
-
#initialize(sitemap) ⇒ SitemapGenerator
constructor
A new instance of SitemapGenerator.
- #next_page ⇒ Object
- #path ⇒ Object
- #per_page ⇒ Object
- #reset_counter ⇒ Object
- #url(url, options = {}) ⇒ Object
- #write(*lines) ⇒ Object
- #write_beginning ⇒ Object
- #write_end ⇒ Object
- #write_url(url, options = {}) ⇒ Object
- #write_urls ⇒ Object
Constructor Details
#initialize(sitemap) ⇒ SitemapGenerator
Returns a new instance of SitemapGenerator.
6 7 8 9 10 11 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 6 def initialize(sitemap) unless self.class.included_modules.include?(Rails.application.routes.url_helpers) self.class.send :include, Rails.application.routes.url_helpers end @sitemap = sitemap end |
Instance Attribute Details
#counter ⇒ Object
78 79 80 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 78 def counter @counter ||= 0 end |
#page ⇒ Object
74 75 76 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 74 def page @page ||= 1 end |
#sitemap ⇒ Object (readonly)
Returns the value of attribute sitemap.
3 4 5 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 3 def sitemap @sitemap end |
Instance Method Details
#ensure_host! ⇒ Object
110 111 112 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 110 def ensure_host! raise "No host specified. Please specify a host using `host \"www.mydomain.com\"` at the top of your sitemap configuration file." if sitemap.host.blank? end |
#file ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 114 def file @file ||= begin files << file_name FileUtils.mkdir_p folder_path File.open(path, "w") end end |
#file_name ⇒ Object
90 91 92 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 90 def file_name sitemap.name.to_s + (page > 1 ? page.to_s : "") + ".xml" end |
#files ⇒ Object
122 123 124 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 122 def files @files ||= [] end |
#folder ⇒ Object
94 95 96 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 94 def folder sitemap.folder end |
#folder_path ⇒ Object
98 99 100 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 98 def folder_path "#{DynamicSitemaps.temp_path}/#{folder}" end |
#format_date(date) ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 150 def format_date(date) if date.is_a?(Date) date.strftime("%Y-%m-%d") else date.to_datetime.utc.strftime("%Y-%m-%dT%H:%M:%S%:z") end end |
#format_url(url) ⇒ Object
141 142 143 144 145 146 147 148 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 141 def format_url(url) case url when ActiveRecord::Base polymorphic_url(url) else url end end |
#generate ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 13 def generate ensure_host! write_beginning write_urls write_end file.close SitemapResult.new(sitemap, files) end |
#handle_collection ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 60 def handle_collection sitemap.collection.find_each do |record| if sitemap.block instance_exec record, &sitemap.block else url record, last_mod: (record.respond_to?(:updated_at) ? record.updated_at : nil) end end end |
#host ⇒ Object
106 107 108 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 106 def host sitemap.host end |
#increment_counter ⇒ Object
82 83 84 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 82 def increment_counter self.counter += 1 end |
#next_page ⇒ Object
126 127 128 129 130 131 132 133 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 126 def next_page write_end reset_counter file.close @file = nil self.page += 1 write_beginning end |
#path ⇒ Object
102 103 104 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 102 def path "#{folder_path}/#{file_name}" end |
#per_page ⇒ Object
70 71 72 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 70 def per_page sitemap.per_page end |
#reset_counter ⇒ Object
86 87 88 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 86 def reset_counter @counter = 1 end |
#url(url, options = {}) ⇒ Object
135 136 137 138 139 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 135 def url(url, = {}) increment_counter next_page if counter > per_page write_url url, end |
#write(*lines) ⇒ Object
56 57 58 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 56 def write(*lines) file.puts lines end |
#write_beginning ⇒ Object
24 25 26 27 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 24 def write_beginning write '<?xml version="1.0" encoding="UTF-8"?>', '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' end |
#write_end ⇒ Object
52 53 54 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 52 def write_end write '</urlset>' end |
#write_url(url, options = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 37 def write_url(url, = {}) write '<url>' write '<loc>' + format_url(url) + '</loc>' if last_mod = [:last_mod] write '<lastmod>' + format_date(last_mod) + '</lastmod>' end if change_freq = [:change_freq] write '<changefreq>' + change_freq + '</changefreq>' end if priority = [:priority] write '<priority>' + priority.to_s + '</priority>' end write '</url>' end |
#write_urls ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 29 def write_urls if sitemap.collection handle_collection else instance_eval &sitemap.block end end |