class SrtSubtitleValidator::SrtFile

Attributes

blocks[R]
errors[RW]
length[R]

Public Class Methods

new(source, logger = nil) click to toggle source

@param [String] source content of SRT file @param [Logger] logger object, optionally - by default log to STDOUT

# File lib/srt_subtitle_validator/srt_file.rb, line 11
def initialize(source, logger = nil)
  @logger = logger || Logger.new(STDOUT)
  @errors = []
  @srt_dialog_blocks = {}
  @source = source.dup.encode(Encoding::UTF_8).gsub(/\r/, '')
end

Public Instance Methods

inspect() click to toggle source
# File lib/srt_subtitle_validator/srt_file.rb, line 30
def inspect
  "<SrtSubtitleValidator::SrtFile ...>"
end
valid?() click to toggle source
# File lib/srt_subtitle_validator/srt_file.rb, line 18
def valid?
  @blocks = @source.split(/^\r?\n+/m).map do |n|
    i = SrtBlock.new(n)
    @srt_dialog_blocks[i.dialog_number] = i
    i
  end
  @length = !@blocks.empty? && @blocks.last.dialog_number || 0
  @errors << 'File is zero size' if @length.zero?
  @errors << 'Numbers sequence is corrupted' unless @blocks.count == @length
  @errors.empty?
end