Class: SitemapBuilder::Sitemap
- Inherits:
-
Object
- Object
- SitemapBuilder::Sitemap
- Defined in:
- lib/sitemap_builder/sitemap.rb
Instance Attribute Summary collapse
-
#default_host ⇒ Object
Returns the value of attribute default_host.
-
#links ⇒ Object
Returns the value of attribute links.
Instance Method Summary collapse
- #add(link, options = {}) ⇒ Object
- #create_sitemap_folder(sitemap_folder) ⇒ Object
- #deleting_previous_sitemap(sitemap) ⇒ Object
- #fullpath ⇒ Object
- #generate ⇒ Object
-
#initialize(options = {}) ⇒ Sitemap
constructor
A new instance of Sitemap.
- #loc ⇒ Object
- #ping_search_engines ⇒ Object
- #save_file(xml) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Sitemap
Returns a new instance of Sitemap.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/sitemap_builder/sitemap.rb', line 5 def initialize(={}) = { :debug => false, :default_host => 'http://localhost:3000', :filename => 'sitemap.xml', :sitemap_folder => '', :ping_search_engines => false }.merge @debug = [:debug] @default_host = [:default_host] @filename = [:filename] @sitemap_folder = [:sitemap_folder] @links = [] deleting_previous_sitemap(fullpath) create_sitemap_folder(@sitemap_folder) end |
Instance Attribute Details
#default_host ⇒ Object
Returns the value of attribute default_host.
3 4 5 |
# File 'lib/sitemap_builder/sitemap.rb', line 3 def default_host @default_host end |
#links ⇒ Object
Returns the value of attribute links.
3 4 5 |
# File 'lib/sitemap_builder/sitemap.rb', line 3 def links @links end |
Instance Method Details
#add(link, options = {}) ⇒ Object
24 25 26 27 28 |
# File 'lib/sitemap_builder/sitemap.rb', line 24 def add(link, ={}) link = Link.generate(link, @default_host, ) p link[:loc] if @debug @links << link end |
#create_sitemap_folder(sitemap_folder) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/sitemap_builder/sitemap.rb', line 101 def create_sitemap_folder(sitemap_folder) return if sitemap_folder.blank? path = RAILS_ROOT + "/public/" + sitemap_folder unless File.exists?(path) && File.directory?(path) FileUtils.mkdir path, :mode => 0755, :verbose => true end end |
#deleting_previous_sitemap(sitemap) ⇒ Object
96 97 98 99 |
# File 'lib/sitemap_builder/sitemap.rb', line 96 def deleting_previous_sitemap(sitemap) sitemap_files = Dir[File.join(RAILS_ROOT, sitemap)] FileUtils.rm sitemap_files, :verbose => true end |
#fullpath ⇒ Object
84 85 86 87 88 |
# File 'lib/sitemap_builder/sitemap.rb', line 84 def fullpath fullpath = "/public/" fullpath << @sitemap_folder + '/' unless @sitemap_folder.blank? fullpath << @filename end |
#generate ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/sitemap_builder/sitemap.rb', line 30 def generate puts "GENERATING XML FILE ..." if @debug xml_str = "" xml = Builder::XmlMarkup.new(:target => xml_str, :indent=>2) xml.instruct! xml.urlset "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", "xsi:schemaLocation" => "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd", "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do links.each do |link| xml.url do xml.loc link[:loc] xml.lastmod w3c_date(link[:lastmod]) if link[:lastmod] xml.changefreq link[:changefreq] if link[:changefreq] xml.priority link[:priority] if link[:priority] end end end save_file(xml_str) ping_search_engines if @ping_search_engines end |
#loc ⇒ Object
90 91 92 93 94 |
# File 'lib/sitemap_builder/sitemap.rb', line 90 def loc loc = @default_host + '/' loc << @sitemap_folder + '/' unless @sitemap_folder.blank? loc << @filename end |
#ping_search_engines ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/sitemap_builder/sitemap.rb', line 53 def ping_search_engines puts "PINGING SEARCH ENGINES ..." if @debug sitemap_uri = self.loc index_location = URI.escape(sitemap_uri) search_engines = { :google => "http://www.google.com/webmasters/sitemaps/ping?sitemap=#{index_location}", :yahoo => "http://search.yahooapis.com/SiteExplorerService/V1/ping?sitemap=#{index_location}", :ask => "http://submissions.ask.com/ping?sitemap=#{index_location}", :bing => "http://www.bing.com/webmaster/ping.aspx?siteMap=#{index_location}" } search_engines.each do |engine, link| begin if @debug puts "pinging #{engine.to_s.titleize} : #{link}" else open(link) puts "Successful ping of #{engine.to_s.titleize}" end rescue Timeout::Error, StandardError => e puts "Ping failed for #{engine.to_s.titleize}: #{e.inspect}" end end end |
#save_file(xml) ⇒ Object
78 79 80 81 82 |
# File 'lib/sitemap_builder/sitemap.rb', line 78 def save_file(xml) File.open(RAILS_ROOT + fullpath, "w+") do |f| f.write(xml) end end |