Class: Polites::File

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

Overview

A Polites::File represents a file saved to disk by Polites with a .ulyz extension. This is a zip file containing the text contents in XML format along with all assets.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ File

Returns a new instance of File.

Parameters:

  • path (String, #to_path)

    path the file to read.



20
21
22
# File 'lib/polites/file.rb', line 20

def initialize(path)
  @path = Pathname(path)
end

Class Method Details

.open(path) ⇒ Object

Parameters:

  • path (String, #to_path)

    path the file to read.



11
12
13
14
15
16
17
# File 'lib/polites/file.rb', line 11

def self.open(path)
  file = new(path)
  file.open
  yield file
ensure
  file.close
end

Instance Method Details

#closePolites::File

Close the source file.

Returns:



37
38
39
40
# File 'lib/polites/file.rb', line 37

def close
  @zip_file&.close
  self
end

#contentString

Read the XML contents of the file.

Returns:

  • (String)

    the XML contents of the file.



45
46
47
# File 'lib/polites/file.rb', line 45

def content
  @zip_file.glob('**/Content.xml').first.get_input_stream.read
end

#extract_to(subpath, dest) ⇒ Zip::Entry

Extract file subpath from the source zip file to a given dest on disk.

Parameters:

  • subpath (String)
  • dest (String)

Returns:

  • (Zip::Entry)


63
64
65
# File 'lib/polites/file.rb', line 63

def extract_to(subpath, dest)
  @zip_file.extract(subpath, dest)
end

#media(fingerprint) ⇒ Zip::Entry?

Get a zip entry for a media file with a particular fingerprint substring in its filename.

Parameters:

  • fingerprint (String)

Returns:

  • (Zip::Entry, nil)


54
55
56
# File 'lib/polites/file.rb', line 54

def media(fingerprint)
  @zip_file.glob("**/Media/*#{fingerprint}*").first
end

#openPolites::File

Open the source file for reading.

This needs to be called before other operations can be used.

Returns:



29
30
31
32
# File 'lib/polites/file.rb', line 29

def open
  @zip_file = Zip::File.open(@path.open)
  self
end