Class: DynamicSitemaps::SitemapGenerator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#counterObject



78
79
80
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 78

def counter
  @counter ||= 0
end

#pageObject



74
75
76
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 74

def page
  @page ||= 1
end

#sitemapObject (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

#fileObject



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_nameObject



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

#filesObject



122
123
124
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 122

def files
  @files ||= []
end

#folderObject



94
95
96
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 94

def folder
  sitemap.folder
end

#folder_pathObject



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

#generateObject



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_collectionObject



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

#hostObject



106
107
108
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 106

def host
  sitemap.host
end

#increment_counterObject



82
83
84
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 82

def increment_counter
  self.counter += 1
end

#next_pageObject



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

#pathObject



102
103
104
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 102

def path
  "#{folder_path}/#{file_name}"
end

#per_pageObject



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

def per_page
  sitemap.per_page
end

#reset_counterObject



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, options = {})
  increment_counter
  next_page if counter > per_page
  write_url url, options
end

#write(*lines) ⇒ Object



56
57
58
# File 'lib/dynamic_sitemaps/sitemap_generator.rb', line 56

def write(*lines)
  file.puts lines
end

#write_beginningObject



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_endObject



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, options = {})
  write '<url>'
  write '<loc>' + format_url(url) + '</loc>'
  if last_mod = options[:last_mod]
    write '<lastmod>' + format_date(last_mod) + '</lastmod>'
  end
  if change_freq = options[:change_freq]
    write '<changefreq>' + change_freq + '</changefreq>'
  end
  if priority = options[:priority]
    write '<priority>' + priority.to_s + '</priority>'
  end
  write '</url>'
end

#write_urlsObject



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