Class: Tomereader::Parser
- Inherits:
-
Object
- Object
- Tomereader::Parser
- Defined in:
- lib/tomereader/parser.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#format_pattern ⇒ Object
readonly
Returns the value of attribute format_pattern.
-
#reader ⇒ Object
readonly
Returns the value of attribute reader.
Instance Method Summary collapse
- #format ⇒ Object
-
#initialize(filename) ⇒ Parser
constructor
A new instance of Parser.
- #pages_count ⇒ Object
- #read ⇒ Object
Constructor Details
#initialize(filename) ⇒ Parser
Returns a new instance of Parser.
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
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
3 4 5 |
# File 'lib/tomereader/parser.rb', line 3 def filename @filename end |
#format_pattern ⇒ Object (readonly)
Returns the value of attribute format_pattern.
3 4 5 |
# File 'lib/tomereader/parser.rb', line 3 def format_pattern @format_pattern end |
#reader ⇒ Object (readonly)
Returns the value of attribute reader.
3 4 5 |
# File 'lib/tomereader/parser.rb', line 3 def reader @reader end |
Instance Method Details
#format ⇒ Object
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_count ⇒ Object
32 33 34 |
# File 'lib/tomereader/parser.rb', line 32 def pages_count reader.page_count end |
#read ⇒ Object
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 |