Hey Everyone
I need your help regarding ruby programming. I have two classes.
-
Companies Class
-
Writer Class
I want to generate a site-map XML file according to the companies names.
In the Companies class I get the data about companies and I want to pass
it to the writer class to write site-map
Following are the classes… Anyone plz help me its very urgent
++++++++++++++Companies Class++++++++++++++
module Sitemap
=begin
Class Name: Companies
Function: This class is responsible for getting the data and passing it
to write.
=end
class Companiesdef initialize(country_version, directory, country_host)
@country_version = country_version
@directory = directory
@country_host = country_host
yield_data
end#def slices
#CountryVersion.each do |cv|
#yield :country_version => cv
#end
#enddef yield_data
Company.find_each(:conditions => {:country_version_id =>
@country_version.id}) do |company|
yield entry(company)
end
endprivate
def entry(company)
Entry.new(
:url =>
ActionController::Integration::Session.new.url_for(:controller =>
‘companies’, :action => ‘show’, :company_name => company.name, :host =>
@country_host.value),
:filepath => @directory + “companies_sitemap.xml”,
:frequency => 0.8,
:priority => ‘monthly’
)
end
end
end
+++++++++++Writer++++++++++++
module Sitemap
=begin
Class Name: Writer
Function: This class is responsible for creating the sitemap for each
country
version.
=end
class Writer
include ActionController::UrlWriter
def initialize(filepath)
@filepath=filepath
end
def self.generate(filepath,&block)
writer = new(filepath)
writer.fill(&block)
end
def fill
File.open(@filepath,"w") do |f|
f.write(%[<?xml version="1.0" encoding="UTF-8"?>\n])
f.write(%[<urlset
xmlns=“www.sitemaps.org - /schemas/sitemap/0.9/”>\n])
yield self
f.write(%[])
f.close
end
zipped_file = ‘gzip -f #{@filename}’
return zipped_file
end
def write_entry(entry)
xml.url do
xml.loc entry.url
xml.lastmod entry.lastmod
xml.changefreq entry.frequency
xml.priority entry.priority
end
end
end
end