Class: ShiftSubtitles::Subtitle

Inherits:
Object
  • Object
show all
Defined in:
lib/shift_subtitles/subtitle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subtitle_string) ⇒ Subtitle

Returns a new instance of Subtitle.



9
10
11
12
# File 'lib/shift_subtitles/subtitle.rb', line 9

def initialize subtitle_string
  subtitle_pattern =~ subtitle_string
  @index, @start_time, @end_time, @text = $1, $2, $3, $4
end

Instance Attribute Details

#end_timeObject (readonly)

Returns the value of attribute end_time.



7
8
9
# File 'lib/shift_subtitles/subtitle.rb', line 7

def end_time
  @end_time
end

#indexObject (readonly)

Returns the value of attribute index.



7
8
9
# File 'lib/shift_subtitles/subtitle.rb', line 7

def index
  @index
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



7
8
9
# File 'lib/shift_subtitles/subtitle.rb', line 7

def start_time
  @start_time
end

#textObject (readonly)

Returns the value of attribute text.



7
8
9
# File 'lib/shift_subtitles/subtitle.rb', line 7

def text
  @text
end

Instance Method Details

#formatObject



26
27
28
# File 'lib/shift_subtitles/subtitle.rb', line 26

def format
  "#{index}\n#{start_time} --> #{end_time}\n#{text}\n\n"
end

#subtitle_patternObject



30
31
32
# File 'lib/shift_subtitles/subtitle.rb', line 30

def subtitle_pattern
  /([0-9]+)\n([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}) --> ([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3})\n(.*)/      
end

#update_duration(seconds_difference) ⇒ Object



14
15
16
17
# File 'lib/shift_subtitles/subtitle.rb', line 14

def update_duration seconds_difference
  @start_time = update_time(start_time, seconds_difference)
  @end_time = update_time(end_time, seconds_difference)
end

#update_time(time_string, seconds_difference) ⇒ Object



19
20
21
22
23
24
# File 'lib/shift_subtitles/subtitle.rb', line 19

def update_time time_string, seconds_difference
  time = Time.parse(time_string)
  updated_time = Time.parse(time_string) + seconds_difference
  raise("Please specify a legal time to shift, this will push the subs into negative values") if updated_time.day < time.day
  updated_time.strftime("%H:%M:%S,%L")
end