Class: EeePub::OCF

Inherits:
Object
  • Object
show all
Defined in:
lib/eeepub/ocf.rb

Overview

Class to create OCF

Defined Under Namespace

Classes: Container

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ OCF

Returns a new instance of OCF.

Examples:

EeePub::OCF.new(
  :dir => '/path/to/dir',
  :container => 'container.opf'
)

Parameters:

  • values (Hash<Symbol, Object>)

    the values of symbols and objects for OCF


70
71
72
73
74
# File 'lib/eeepub/ocf.rb', line 70

def initialize(values)
  values.each do |k, v|
    self.send(:"#{k}=", v)
  end
end

Instance Attribute Details

#containerObject

Returns the value of attribute container


61
62
63
# File 'lib/eeepub/ocf.rb', line 61

def container
  @container
end

#dirObject

Returns the value of attribute dir


61
62
63
# File 'lib/eeepub/ocf.rb', line 61

def dir
  @dir
end

Instance Method Details

#renderString

Stream OCF

Returns:

  • (String)

    streaming output of the zip/epub file.


111
112
113
114
115
116
117
# File 'lib/eeepub/ocf.rb', line 111

def render
  create_epub do
    temp_file = Tempfile.new("ocf")
    self.save(temp_file.path)
    return temp_file.read
  end
end

#save(output_path) ⇒ Object

Save as OCF

Parameters:

  • output_path (String)

    the output file path of ePub


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/eeepub/ocf.rb', line 91

def save(output_path)
  output_path = File.expand_path(output_path)

  create_epub do
    mimetype = Zip::ZipOutputStream::open(output_path) do |os|
      os.put_next_entry("mimetype", nil, nil, Zip::ZipEntry::STORED, Zlib::NO_COMPRESSION)
      os << "application/epub+zip"
    end
    zipfile = Zip::ZipFile.open(output_path)
    Dir.glob('**/*').each do |path|
      zipfile.add(path, path)
    end
    zipfile.commit
  end
  FileUtils.remove_entry_secure dir
end