class Tomereader::Parser

Attributes

filename[R]
format_pattern[R]
reader[R]

Public Class Methods

new(filename) click to toggle source
# File lib/tomereader/parser.rb, line 4
def initialize(filename)
  raise ArgumentError, "Specify correct filename" if not filename and filename.empty?
  raise StandardError, "File #{filename} not exists" unless File.exists? filename
  @filename = filename
  @format_pattern = /[a-z0-9_\-\.]+\.([a-z0-9]{3,4})$/
end

Public Instance Methods

format() click to toggle source
# File lib/tomereader/parser.rb, line 10
def format
  @match = format_pattern.match(filename)
  format = @match[1]
  raise StandardError, "Format is undefined" unless @match && format
  format
end
pages_count() click to toggle source
# File lib/tomereader/parser.rb, line 32
def pages_count
  reader.page_count
end
read() click to toggle source
# File lib/tomereader/parser.rb, line 16
def read
  case format
  when 'pdf'
    #TODO: check if pdftotext installed
    open("|pdftotext #{filename} -").read() 
  when 'txt'
    File.read(filename)
  else
    temp_file = Tempfile.new([@match[0], '.txt'])
    system("ebook-convert #{filename} #{temp_file.path}")
    content = temp_file.read
    temp_file.close
    temp_file.unlink
    content
  end
end