Class: SrtSubtitleValidator::SrtFile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, logger = nil) ⇒ SrtFile

Returns a new instance of SrtFile.

Parameters:

  • source (String)

    content of SRT file

  • logger (Logger) (defaults to: nil)

    object, optionally - by default log to STDOUT



11
12
13
14
15
16
# 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

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



6
7
8
# File 'lib/srt_subtitle_validator/srt_file.rb', line 6

def blocks
  @blocks
end

#errorsObject

Returns the value of attribute errors.



7
8
9
# File 'lib/srt_subtitle_validator/srt_file.rb', line 7

def errors
  @errors
end

#lengthObject (readonly)

Returns the value of attribute length.



6
7
8
# File 'lib/srt_subtitle_validator/srt_file.rb', line 6

def length
  @length
end

Instance Method Details

#inspectObject



30
31
32
# File 'lib/srt_subtitle_validator/srt_file.rb', line 30

def inspect
  "<SrtSubtitleValidator::SrtFile ...>"
end

#valid?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
# 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