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

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

Instance Method Summary collapse

Instance Method Details

#default_output_fileObject



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

def default_output_file
  ::File.join(
    ::File.dirname(source_file),
    ::File.basename(source_file, '.*') + '_translated.html'
  )
end

#output_fileObject



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

def output_file
  parsed.output_file || default_output_file
end

#output_to_stdout?Boolean

Returns:

  • (Boolean)


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

def output_to_stdout?
  output_file == '-'
end

#runObject



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

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



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

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

#start_bannerObject



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

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

#translated_content_uncachedObject



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

def translated_content_uncached
  session.translate(source_file)
end

#validateObject



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

def validate
  validate_source_file
  validate_output_file
  validate_output_content
end

#validate_output_contentObject



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

def validate_output_content
  return if translated_content.present?

  fatal_error 'Output content is empty'
end

#validate_output_fileObject



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

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



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

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

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