Class: RTesseract::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/rtesseract/command.rb

Constant Summary collapse

FIXED =
%i[command psm oem lang tessdata_dir user_words user_patterns config_file].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, output_path, errors, options) ⇒ Command

Returns a new instance of Command.



9
10
11
12
13
14
15
# File 'lib/rtesseract/command.rb', line 9

def initialize(source, output_path, errors, options)
  @source = source
  @output_path = output_path
  @options = options
  @errors = errors
  @full_command = [options.command, @source, @output_path]
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/rtesseract/command.rb', line 7

def options
  @options
end

Instance Method Details

#add_option(*args) ⇒ Object



32
33
34
35
36
# File 'lib/rtesseract/command.rb', line 32

def add_option(*args)
  return unless args.last

  @full_command << args.map(&:to_s)
end

#full_commandObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rtesseract/command.rb', line 17

def full_command
  add_option('--psm', options.psm)
  add_option('--oem', options.oem)
  add_option('-l', options.lang)
  add_option('--tessdata-dir', options.tessdata_dir)
  add_option('--user-words', options.user_words)
  add_option('--user-patterns', options.user_patterns)

  other_configs

  add_option(options.config_file)

  @full_command
end

#other_configsObject



38
39
40
41
42
43
44
# File 'lib/rtesseract/command.rb', line 38

def other_configs
  @options.to_h.map do |key, value|
    next if FIXED.include?(key)

    add_option('-c', "#{key}=#{value}")
  end
end

#runObject

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rtesseract/command.rb', line 46

def run
  output, error, status = Open3.capture3(*full_command.flatten)

  @errors.push(error)

  if status.success?
    return yield(@output_path) if block_given?

    return output
  end

  raise RTesseract::Error, error
end