Class: IP::Sitemap::Processor
- Inherits:
-
Object
- Object
- IP::Sitemap::Processor
- Defined in:
- lib/ip/processor.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#output_directory ⇒ Object
Returns the value of attribute output_directory.
-
#urlsets ⇒ Object
Returns the value of attribute urlsets.
Instance Method Summary collapse
- #create_file(filename, xml, klass) ⇒ Object
- #create_gz_file(filename, xml) ⇒ Object
- #create_index_file ⇒ Object
- #create_xml_file(filename, xml) ⇒ Object
-
#initialize(output_directory, host) ⇒ Processor
constructor
A new instance of Processor.
- #process ⇒ Object
- #process_group(klass, group) ⇒ Object
- #sitemap_classes ⇒ Object
Constructor Details
#initialize(output_directory, host) ⇒ Processor
Returns a new instance of Processor.
28 29 30 31 32 |
# File 'lib/ip/processor.rb', line 28 def initialize(output_directory, host) self.urlsets = [] self.output_directory = output_directory self.host = host end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
26 27 28 |
# File 'lib/ip/processor.rb', line 26 def host @host end |
#output_directory ⇒ Object
Returns the value of attribute output_directory.
26 27 28 |
# File 'lib/ip/processor.rb', line 26 def output_directory @output_directory end |
#urlsets ⇒ Object
Returns the value of attribute urlsets.
26 27 28 |
# File 'lib/ip/processor.rb', line 26 def urlsets @urlsets end |
Instance Method Details
#create_file(filename, xml, klass) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/ip/processor.rb', line 53 def create_file(filename, xml, klass) if klass.[:compress] create_gz_file(filename, xml) else create_xml_file(filename, xml) end end |
#create_gz_file(filename, xml) ⇒ Object
61 62 63 64 65 |
# File 'lib/ip/processor.rb', line 61 def create_gz_file(filename, xml) ::Zlib::GzipWriter.open(output_directory + filename) do |gz| gz.write xml end end |
#create_index_file ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ip/processor.rb', line 73 def create_index_file sitemap_index = IP::Sitemap::SitemapIndex.new sitemap_index.sitemaps = [] urlsets.each do |urlset| sitemap = IP::Sitemap::Sitemap.new sitemap.loc = host + urlset sitemap_index.sitemaps << sitemap end doc = ROXML::XML::Document.new doc.root = sitemap_index.to_xml doc.save(output_directory + '/sitemap_index.xml') end |
#create_xml_file(filename, xml) ⇒ Object
67 68 69 70 71 |
# File 'lib/ip/processor.rb', line 67 def create_xml_file(filename, xml) File.open(output_directory + filename, "w") do |file| file << xml end end |
#process ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ip/processor.rb', line 38 def process batch = 1 sitemap_classes.each do |klass| klass.find_in_batches(:batch_size => klass.[:batch_size]).each do |group| xml = process_group(klass, group) filename = "/#{klass.to_s.underscore}_urlset_#{batch}.xml" filename += '.gz' if klass.[:compress] urlsets << filename create_file(filename, xml, klass) create_index_file batch += 1 end end end |
#process_group(klass, group) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ip/processor.rb', line 86 def process_group(klass, group) doc = ROXML::XML::Document.new urlset = IP::Sitemap::Urlset.new urlset.urls = [] group.each do |instance| url = IP::Sitemap::Url.new IP::Sitemap::RouteMaker.instance.set_host(host) url.loc = IP::Sitemap::RouteMaker.instance.send(klass.[:route], instance) url.lastmod = instance.updated_at.utc.strftime("%Y-%m-%dT%H:%M:%S+00:00") url.changefreq = klass.[:changefreq] || 'daily' url.priority = klass.[:priority] || instance.try(:priority) || 1 urlset.urls << url end doc.root = urlset.to_xml doc.to_s end |
#sitemap_classes ⇒ Object
34 35 36 |
# File 'lib/ip/processor.rb', line 34 def sitemap_classes classes = ::ActiveRecord::Base.send(:subclasses).select{|klass| !klass..nil? } end |