Class: SrtSubtitleValidator::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/srt_subtitle_validator/validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, encoding = nil, logger = nil) ⇒ Validator

Returns a new instance of Validator.

Raises:



14
15
16
17
18
19
20
21
# File 'lib/srt_subtitle_validator/validator.rb', line 14

def initialize(file_path, encoding = nil, logger = nil)
  @logger = logger || Logger.new(STDOUT)
  @path = File.absolute_path(file_path).strip
  raise InvalidFile unless File.extname(@path) == '.srt'

  @file_name = File.basename(@path)
  parse_srt(File.read(@path, encoding: (encoding || Encoding::UTF_8)))
end

Instance Attribute Details

#file_nameObject (readonly)

Returns the value of attribute file_name.



12
13
14
# File 'lib/srt_subtitle_validator/validator.rb', line 12

def file_name
  @file_name
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/srt_subtitle_validator/validator.rb', line 12

def path
  @path
end

#srtObject (readonly)

Returns the value of attribute srt.



12
13
14
# File 'lib/srt_subtitle_validator/validator.rb', line 12

def srt
  @srt
end

Instance Method Details

#convert_srt(output, skip_backup = false) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/srt_subtitle_validator/validator.rb', line 27

def convert_srt(output, skip_backup = false)
  skip_backup ||= output_file(output) != @path
  backup_original_file unless !!skip_backup
  @new_srt = Tempfile.new([@file_name.gsub('.srt', ''), '.srt'])
  recalculate_number_sequence

  @logger.info ' > Save as UTF-8 encoded file...'
  @new_srt.flush
  FileUtils.copy(@new_srt.path, output_file(output))
  @new_srt.close
  @new_srt.unlink
end

#missing_numbersObject



40
41
42
# File 'lib/srt_subtitle_validator/validator.rb', line 40

def missing_numbers
  @missing_numbers ||= (Array(1..@srt.length) - @srt.blocks.map(&:dialog_number))
end

#valid?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/srt_subtitle_validator/validator.rb', line 23

def valid?
  @srt.valid?
end