Sha256: 9dab585792526dabd0b7766e062313ed6007f51c514797158438eb8ec201d36b

Contents?: true

Size: 1.29 KB

Versions: 17

Compression:

Stored size: 1.29 KB

Contents

module SitemapGenerator
  # Class for writing out data to a file.
  class FileAdapter

    # Write data to a file.
    # @param location - File object giving the full path and file name of the file.
    #    If the location specifies a directory(ies) which does not exist, the directory(ies)
    #    will be created for you.  If the location path ends with `.gz` the data will be
    #    compressed prior to being written out.  Otherwise the data will be written out
    #    unchanged.
    # @param raw_data - data to write to the file.
    def write(location, raw_data)
      # Ensure that the directory exists
      dir = location.directory
      if !File.exists?(dir)
        FileUtils.mkdir_p(dir)
      elsif !File.directory?(dir)
        raise SitemapError.new("#{dir} should be a directory!")
      end

      stream = open(location.path, 'wb')
      if location.path.to_s =~ /.gz$/
        gzip(stream, raw_data)
      else
        plain(stream, raw_data)
      end
    end

    # Write `data` to a stream, passing the data through a GzipWriter
    # to compress it.
    def gzip(stream, data)
      gz = Zlib::GzipWriter.new(stream)
      gz.write data
      gz.close
    end

    # Write `data` to a stream as is.
    def plain(stream, data)
      stream.write data
      stream.close
    end
  end
end

Version data entries

17 entries across 17 versions & 4 rubygems

Version Path
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/sitemap_generator-5.3.1/lib/sitemap_generator/adapters/file_adapter.rb
sitemap_generator-5.3.1 lib/sitemap_generator/adapters/file_adapter.rb
sitemap_generator-5.3.0 lib/sitemap_generator/adapters/file_adapter.rb
sitemap_generator-5.2.0 lib/sitemap_generator/adapters/file_adapter.rb
sitemap_generator-5.1.0 lib/sitemap_generator/adapters/file_adapter.rb
sitemap_generator-5.0.5 lib/sitemap_generator/adapters/file_adapter.rb
ftbpro_sitemap_generator-5.0.8 lib/sitemap_generator/adapters/file_adapter.rb
sitemap_generator_ftbpro-5.0.6 lib/sitemap_generator/adapters/file_adapter.rb
sitemap_generator_ftbpro-5.0.5 lib/sitemap_generator/adapters/file_adapter.rb
sitemap_generator_ftbpro-5.0.4 lib/sitemap_generator/adapters/file_adapter.rb
sitemap_generator-5.0.4 lib/sitemap_generator/adapters/file_adapter.rb
sitemap_generator-5.0.3 lib/sitemap_generator/adapters/file_adapter.rb
sitemap_generator-5.0.2 lib/sitemap_generator/adapters/file_adapter.rb
sitemap_generator-5.0.1 lib/sitemap_generator/adapters/file_adapter.rb
sitemap_generator-5.0.0 lib/sitemap_generator/adapters/file_adapter.rb
sitemap_generator-5.0.0.beta1 lib/sitemap_generator/adapters/file_adapter.rb
sitemap_generator-5.0.0.beta lib/sitemap_generator/adapters/file_adapter.rb