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



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

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

Instance Attribute Details

#containerObject

Returns the value of attribute container.



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

def container
  @container
end

#dirObject

Returns the value of attribute dir.



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

def dir
  @dir
end

Instance Method Details

#renderString

Stream OCF

Returns:

  • (String)

    streaming output of the zip/epub file.



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

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



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

def save(output_path)
  output_path = File.expand_path(output_path)
  create_epub do
    mimetype = Zip::OutputStream::open(output_path) do |os|
      os.put_next_entry("mimetype")
      os << "application/epub+zip"
    end
    zipfile = Zip::File.open(output_path)
    Dir.glob('**/*').each do |path|
      zipfile.add(path, path)
    end
    zipfile.commit
  end
  FileUtils.remove_entry_secure dir
end