Class: SitemapReader

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

Overview

Parse sitemap

Example:

>> sm = SitemapReader.new('http://example.com/sitemap.xml').get_urls
=> [{:loc=>"http://example.com/page1", :lastmod=>"2013-08-18"},{:loc=>"http://example.com/page2", :lastmod=>nil}]

… or read from file like this:

>> sm = SitemapReader.new('./sitemap.xml').get_urls
=> [{:loc=>"http://example.com/page1", :lastmod=>"2013-08-18"},{:loc=>"http://example.com/page2", :lastmod=>nil}]

Instance Method Summary collapse

Constructor Details

#initialize(file_or_url) ⇒ SitemapReader

Arguments:

file_or_url: (String)


16
17
18
19
# File 'lib/sitemap_reader.rb', line 16

def initialize(file_or_url)
	@doc = Nokogiri::XML(get_sitemap(file_or_url))
	@urls= get_urls
end

Instance Method Details

#get_urlsObject



21
22
23
24
25
26
27
# File 'lib/sitemap_reader.rb', line 21

def get_urls
	@doc.css('url').map do |u|
		loc = u.css('loc').first.content
		lastmod = u.css('lastmod').first.content unless u.css('lastmod').first.nil?
		{loc: loc, lastmod: lastmod}
	end
end