Class: WebVTT::Blob

Inherits:
Object
  • Object
show all
Defined in:
lib/webvtt/parser.rb

Direct Known Subclasses

File

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content = nil) ⇒ Blob

Returns a new instance of Blob.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/webvtt/parser.rb', line 36

def initialize(content = nil)
  @cues = []

  if content
    parse(
      content.gsub("\r\n", "\n").gsub("\r","\n") # normalizing new line character
    )
  else
    @header = 'WEBVTT'
  end
end

Instance Attribute Details

#cuesObject

Returns the value of attribute cues.



34
35
36
# File 'lib/webvtt/parser.rb', line 34

def cues
  @cues
end

#headerObject (readonly)

Returns the value of attribute header.



33
34
35
# File 'lib/webvtt/parser.rb', line 33

def header
  @header
end

Instance Method Details

#actual_total_lengthObject



56
57
58
# File 'lib/webvtt/parser.rb', line 56

def actual_total_length
  @cues.last.end_in_sec - @cues.first.start_in_sec
end

#parse(content) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/webvtt/parser.rb', line 60

def parse(content)
  # remove bom first
  content.gsub!("\uFEFF", '')

  cues = content.split(/\n\n+/)

  @header = cues.shift
  header_lines = @header.split("\n").map(&:strip)
  if (header_lines[0] =~ /^WEBVTT/).nil?
    raise MalformedFile, "Not a valid WebVTT file"
  end

  @cues = []
  cues.each do |cue|
    cue_parsed = Cue.parse(cue.strip)
    if !cue_parsed.text.nil?
      @cues << cue_parsed
    end
  end
  @cues
end

#to_webvttObject



48
49
50
# File 'lib/webvtt/parser.rb', line 48

def to_webvtt
  [@header, @cues.map(&:to_webvtt)].flatten.join("\n\n")
end

#total_lengthObject



52
53
54
# File 'lib/webvtt/parser.rb', line 52

def total_length
  @cues.last.end_in_sec
end