Class: SiteMaps::SitemapReader

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

Constant Summary collapse

Error =
Class.new(SiteMaps::Error)
FileNotFoundError =
Class.new(Error)
MalformedFileError =
Class.new(Error)

Instance Method Summary collapse

Constructor Details

#initialize(location) ⇒ SitemapReader

Returns a new instance of SitemapReader.



11
12
13
# File 'lib/site_maps/sitemap_reader.rb', line 11

def initialize(location)
  @location = Pathname.new(location)
end

Instance Method Details

#readObject



15
16
17
18
19
20
21
22
23
# File 'lib/site_maps/sitemap_reader.rb', line 15

def read
  if compressed?
    Zlib::GzipReader.new(read_file).read
  else
    read_file.read
  end
rescue Zlib::GzipFile::Error => _e
  raise MalformedFileError.new("The file #{@location} is not a valid Gzip file")
end

#to_docObject



25
26
27
28
29
30
31
32
# File 'lib/site_maps/sitemap_reader.rb', line 25

def to_doc
  @doc ||= begin
    require "nokogiri"
    Nokogiri::XML(read)
  end
rescue LoadError
  raise SiteMaps::Error, "Nokogiri is required to parse the XML file"
end