Class: Ehbrs::Tools::Runner::Google::Translate

Inherits:
Object
  • Object
show all
Defined in:
lib/ehbrs/tools/runner/google/translate.rb

Instance Method Summary collapse

Instance Method Details

#default_output_fileObject



45
46
47
48
49
50
# File 'lib/ehbrs/tools/runner/google/translate.rb', line 45

def default_output_file
  ::File.join(
    ::File.dirname(source_file),
    "#{::File.basename(source_file, '.*')}_translated.html"
  )
end

#output_fileObject



41
42
43
# File 'lib/ehbrs/tools/runner/google/translate.rb', line 41

def output_file
  parsed.output_file || default_output_file
end

#output_to_stdout?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/ehbrs/tools/runner/google/translate.rb', line 82

def output_to_stdout?
  output_file == '-'
end

#runObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/ehbrs/tools/runner/google/translate.rb', line 19

def run
  start_banner
  validate
  if output_to_stdout?
    out(translated_content)
  else
    ::File.write(output_file, translated_content)
  end
  success 'ConcluĂ­do'
end

#session_uncachedObject



52
53
54
# File 'lib/ehbrs/tools/runner/google/translate.rb', line 52

def session_uncached
  ::Ehbrs::Google::Translate::Session.new
end

#start_bannerObject



30
31
32
33
# File 'lib/ehbrs/tools/runner/google/translate.rb', line 30

def start_banner
  infov 'Source file', source_file
  infov 'Target file', output_file
end

#translated_content_uncachedObject



35
36
37
# File 'lib/ehbrs/tools/runner/google/translate.rb', line 35

def translated_content_uncached
  session.translate(source_file)
end

#validateObject



56
57
58
59
60
# File 'lib/ehbrs/tools/runner/google/translate.rb', line 56

def validate
  validate_source_file
  validate_output_file
  validate_output_content
end

#validate_output_contentObject



76
77
78
79
80
# File 'lib/ehbrs/tools/runner/google/translate.rb', line 76

def validate_output_content
  return if translated_content.present?

  fatal_error 'Output content is empty'
end

#validate_output_fileObject



68
69
70
71
72
73
74
# File 'lib/ehbrs/tools/runner/google/translate.rb', line 68

def validate_output_file
  return if output_to_stdout?
  return unless ::File.exist?(output_file)
  return if parsed.overwrite?

  fatal_error "Output file \"#{output_file}\" already exists"
end

#validate_source_fileObject



62
63
64
65
66
# File 'lib/ehbrs/tools/runner/google/translate.rb', line 62

def validate_source_file
  return if ::File.exist?(source_file)

  fatal_error "Source file \"#{source_file}\" does not exist"
end