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:



11
12
13
14
15
16
17
# File 'lib/srt_subtitle_validator/validator.rb', line 11

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.



9
10
11
# File 'lib/srt_subtitle_validator/validator.rb', line 9

def file_name
  @file_name
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/srt_subtitle_validator/validator.rb', line 9

def path
  @path
end

#srtObject (readonly)

Returns the value of attribute srt.



9
10
11
# File 'lib/srt_subtitle_validator/validator.rb', line 9

def srt
  @srt
end

Instance Method Details

#convert_srt(output, skip_backup = false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/srt_subtitle_validator/validator.rb', line 23

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



36
37
38
# File 'lib/srt_subtitle_validator/validator.rb', line 36

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

#valid?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/srt_subtitle_validator/validator.rb', line 19

def valid?
  @srt.valid?
end