Class: SrtSubtitleValidator::Validator
- Inherits:
-
Object
- Object
- SrtSubtitleValidator::Validator
- Defined in:
- lib/srt_subtitle_validator/validator.rb
Instance Attribute Summary collapse
-
#file_name ⇒ Object
readonly
Returns the value of attribute file_name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#srt ⇒ Object
readonly
Returns the value of attribute srt.
Instance Method Summary collapse
- #convert_srt(output, skip_backup = false) ⇒ Object
-
#initialize(file_path, encoding = nil, logger = nil) ⇒ Validator
constructor
A new instance of Validator.
- #missing_numbers ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(file_path, encoding = nil, logger = nil) ⇒ Validator
Returns a new instance of Validator.
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_name ⇒ Object (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 |
#path ⇒ Object (readonly)
Returns the value of attribute path.
12 13 14 |
# File 'lib/srt_subtitle_validator/validator.rb', line 12 def path @path end |
#srt ⇒ Object (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_numbers ⇒ Object
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
23 24 25 |
# File 'lib/srt_subtitle_validator/validator.rb', line 23 def valid? @srt.valid? end |