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



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

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

#output_fileObject



37
38
39
# File 'lib/ehbrs/tools/runner/google/translate.rb', line 37

def output_file
  parsed.output_file || default_output_file
end

#output_to_stdout?Boolean

Returns:

  • (Boolean)


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

def output_to_stdout?
  output_file == '-'
end

#runObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/ehbrs/tools/runner/google/translate.rb', line 15

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



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

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

#start_bannerObject



26
27
28
29
# File 'lib/ehbrs/tools/runner/google/translate.rb', line 26

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

#translated_content_uncachedObject



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

def translated_content_uncached
  session.translate(source_file)
end

#validateObject



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

def validate
  validate_source_file
  validate_output_file
  validate_output_content
end

#validate_output_contentObject



72
73
74
75
76
# File 'lib/ehbrs/tools/runner/google/translate.rb', line 72

def validate_output_content
  return if translated_content.present?

  fatal_error 'Output content is empty'
end

#validate_output_fileObject



64
65
66
67
68
69
70
# File 'lib/ehbrs/tools/runner/google/translate.rb', line 64

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



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

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

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