Class: SitemapGenerator::FileAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/sitemap_generator/adapters/file_adapter.rb

Overview

Class for writing out data to a file.

Instance Method Summary (collapse)

Instance Method Details

- (Object) gzip(stream, data)

Write `data` to a stream, passing the data through a GzipWriter to compress it.



31
32
33
34
35
# File 'lib/sitemap_generator/adapters/file_adapter.rb', line 31

def gzip(stream, data)
  gz = Zlib::GzipWriter.new(stream)
  gz.write data
  gz.close
end

- (Object) plain(stream, data)

Write `data` to a stream as is.



38
39
40
41
# File 'lib/sitemap_generator/adapters/file_adapter.rb', line 38

def plain(stream, data)
  stream.write data
  stream.close
end

- (Object) write(location, raw_data)

Write data to a file.

Parameters:

  • 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.

  • raw_data
    • data to write to the file.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sitemap_generator/adapters/file_adapter.rb', line 12

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