Sha256: 6a248655b7995c0bb403a55363ca8aa473e80c4e4e311dbbc47767da18c3ac63

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

require "open-uri"

module SiteMaps
  class SitemapReader
    Error = Class.new(SiteMaps::Error)
    FileNotFound = Class.new(Error)
    MalformedFile = Class.new(Error)

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

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

    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

    protected

    def read_file
      if remote?
        ::URI.parse(@location.to_s).open
      else
        ::File.open(@location, "r")
      end
    rescue Errno::ENOENT
      raise FileNotFound.new("The file #{@location} does not exist")
    rescue OpenURI::HTTPError
      raise FileNotFound.new("The file #{@location} could not be opened")
    end

    def compressed?
      @location.extname == ".gz"
    end

    def remote?
      %r{^https?://}.match?(@location.to_s)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
site_maps-0.0.1.beta2 lib/site_maps/sitemap_reader.rb
site_maps-0.0.1.beta1 lib/site_maps/sitemap_reader.rb