Class: Eepub::Epub

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Epub

Returns a new instance of Epub.



7
8
9
# File 'lib/eepub/epub.rb', line 7

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/eepub/epub.rb', line 11

def path
  @path
end

#titleObject



13
14
15
16
17
18
19
# File 'lib/eepub/epub.rb', line 13

def title
  @title ||= Zip::File.open(@path) {|zip|
    package_xml = REXML::Document.new(package_entry(zip).get_input_stream)

    REXML::XPath.first(package_xml, '//dc:title', 'dc' => 'http://purl.org/dc/elements/1.1/').text
  }
end

Instance Method Details

#save!(to: path) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/eepub/epub.rb', line 23

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

  Zip::File.open to do |zip|
    entry      = package_entry(zip)
    xml        = REXML::Document.new(entry.get_input_stream)
    title_node = REXML::XPath.first(xml, '//dc:title', 'dc' => 'http://purl.org/dc/elements/1.1/')

    title_node.text = title

    zip.get_output_stream entry.name do |stream|
      stream.write xml.to_s
    end

    zip.commit
  end
end