Class: WebVTT::Blob
- Inherits:
-
Object
- Object
- WebVTT::Blob
- Defined in:
- lib/webvtt/parser.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#cues ⇒ Object
Returns the value of attribute cues.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
Instance Method Summary collapse
- #actual_total_length ⇒ Object
-
#initialize(content = nil) ⇒ Blob
constructor
A new instance of Blob.
- #parse(content) ⇒ Object
- #to_webvtt ⇒ Object
- #total_length ⇒ Object
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
#cues ⇒ Object
Returns the value of attribute cues.
34 35 36 |
# File 'lib/webvtt/parser.rb', line 34 def cues @cues end |
#header ⇒ Object (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_length ⇒ Object
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_webvtt ⇒ Object
48 49 50 |
# File 'lib/webvtt/parser.rb', line 48 def to_webvtt [@header, @cues.map(&:to_webvtt)].flatten.join("\n\n") end |
#total_length ⇒ Object
52 53 54 |
# File 'lib/webvtt/parser.rb', line 52 def total_length @cues.last.end_in_sec end |