Class: IP::Sitemap::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/ip/processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#hostObject

Returns the value of attribute host.



26
27
28
# File 'lib/ip/processor.rb', line 26

def host
  @host
end

#output_directoryObject

Returns the value of attribute output_directory.



26
27
28
# File 'lib/ip/processor.rb', line 26

def output_directory
  @output_directory
end

#urlsetsObject

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.sitemap_options[: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_fileObject



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

#processObject



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.sitemap_options[:batch_size]).each do |group|
      xml = process_group(klass, group)
      filename = "/#{klass.to_s.underscore}_urlset_#{batch}.xml"
      filename += '.gz' if klass.sitemap_options[: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.sitemap_options[:route], instance)
    url.lastmod = instance.updated_at.utc.strftime("%Y-%m-%dT%H:%M:%S+00:00")
    url.changefreq = klass.sitemap_options[:changefreq] || 'daily'
    url.priority = klass.sitemap_options[:priority] || instance.try(:priority) || 1
    urlset.urls << url
  end
  doc.root = urlset.to_xml
  doc.to_s
end

#sitemap_classesObject



34
35
36
# File 'lib/ip/processor.rb', line 34

def sitemap_classes
  classes = ::ActiveRecord::Base.send(:subclasses).select{|klass| !klass.sitemap_options.nil? }
end