class EPUB::Parser

Constants

VERSION

Public Class Methods

new(datasource, options = {}) click to toggle source
# File lib/epub/parser.rb, line 46
def initialize(datasource, options = {})
  if options[:io]
    raise "IO source not readable" unless datasource.respond_to?(:read)

    @io_stream = datasource
    @book = create_book options
    file = Tempfile.new('epub_string')
    file.write(@io_stream)
    @filepath = file.path
    @book.epub_file = @filepath
  else
    raise "File #{datasource} not readable" unless File.readable_real? datasource

    @filepath = File.realpath datasource
    @book = create_book options
    @book.epub_file = @filepath
  end
end
parse(filepath, options = {}) click to toggle source

Parse an EPUB file

@example

EPUB::Parser.parse('path/to/book.epub') # => EPUB::Book object

@example

class MyBook
  include EPUB
end
book = MyBook.new
parsed_book = EPUB::Parser.parse('path/to/book.epub', :book => book) # => #<MyBook:0x000000019760e8 @epub_file=..>
parsed_book.equal? book # => true

@example

book = EPUB::Parser.parse('path/to/book.epub', :class => MyBook) # => #<MyBook:0x000000019b0568 @epub_file=...>
book.instance_of? MyBook # => true

@param [String] filepath @param [Hash] options the type of return is specified by this argument.

If no options, returns {EPUB::Book} object.
For details of options, see below.

@option options [EPUB] :book instance of class which includes {EPUB} module @option options [Class] :class class which includes {EPUB} module @return [EPUB] object which is an instance of class including {EPUB} module.

When option :book passed, returns the same object whose attributes about EPUB are set.
When option :class passed, returns the instance of the class.
Otherwise returns {EPUB::Book} object.
# File lib/epub/parser.rb, line 37
def parse(filepath, options = {})
  new(filepath, options).parse
end
parse_io(io_stream, options = {}) click to toggle source
# File lib/epub/parser.rb, line 41
def parse_io(io_stream, options = {})
  new(io_stream, options.merge(io: true)).parse
end

Public Instance Methods

parse() click to toggle source
# File lib/epub/parser.rb, line 65
def parse
  Zip::Archive.open @filepath do |zip|
    @book.ocf = OCF.parse(zip)
    @book.package = Publication.parse(zip, @book.ocf.container.rootfile.full_path.to_s)
  end

  @book
end
parse_io() click to toggle source
# File lib/epub/parser.rb, line 74
def parse_io # unnecessary, but desirable maybe?
  Zip::Archive.open_buffer @io_stream do |zip|
    @book.ocf = OCF.parse(zip)
    @book.package = Publication.parse(zip, @book.ocf.container.rootfile.full_path.to_s)
  end

  @book
end

Private Instance Methods

create_book(params) click to toggle source
# File lib/epub/parser.rb, line 85
def create_book(params)
  case
  when params[:book]
    params[:book]
  when params[:class]
    params[:class].new
  else
    require 'epub/book'
    Book.new
  end
end