Class: Tomereader::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/tomereader/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Parser

Returns a new instance of Parser.

Raises:

  • (ArgumentError)


4
5
6
7
8
9
# 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

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



3
4
5
# File 'lib/tomereader/parser.rb', line 3

def filename
  @filename
end

#format_patternObject (readonly)

Returns the value of attribute format_pattern.



3
4
5
# File 'lib/tomereader/parser.rb', line 3

def format_pattern
  @format_pattern
end

#readerObject (readonly)

Returns the value of attribute reader.



3
4
5
# File 'lib/tomereader/parser.rb', line 3

def reader
  @reader
end

Instance Method Details

#formatObject

Raises:

  • (StandardError)


10
11
12
13
14
15
# 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_countObject



32
33
34
# File 'lib/tomereader/parser.rb', line 32

def pages_count
  reader.page_count
end

#readObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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