Class: Eepub::Epub

Inherits:
Object
  • Object
show all
Defined in:
lib/eepub/epub.rb

Constant Summary collapse

XMLNS =
{
  'container' => 'urn:oasis:names:tc:opendocument:xmlns:container',
  'dc'        => 'http://purl.org/dc/elements/1.1/',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Epub

Returns a new instance of Epub.



22
23
24
# File 'lib/eepub/epub.rb', line 22

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



26
27
28
# File 'lib/eepub/epub.rb', line 26

def path
  @path
end

#titleObject



28
29
30
31
32
33
34
# File 'lib/eepub/epub.rb', line 28

def title
  @title ||= Zip::File.open(path) {|zip|
    rootfile_doc = parse_xml(find_rootfile_entry(zip))

    get_title_element(rootfile_doc).text
  }
end

Instance Method Details

#save!(to: path) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/eepub/epub.rb', line 38

def save!(to: path)
  FileUtils.cp path, to unless path == to

  Zip::File.open to do |zip|
    rootfile_entry = find_rootfile_entry(zip)
    rootfile_doc   = parse_xml(rootfile_entry)

    get_title_element(rootfile_doc).text = title

    zip.get_output_stream rootfile_entry.name, &rootfile_doc.method(:write)
    zip.commit
  end
end