Class: Captive::Cue

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/captive/cue.rb

Constant Summary collapse

ALIGNMENT =

Text Properties supported

'alignment'
COLOR =
'color'
POSITION =
'position'
TEXT_PROPERTIES =

List of Text Properties

[ALIGNMENT, COLOR, POSITION].freeze

Constants included from Util

Util::TIMECODE_REGEX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#hours_to_seconds, #milliseconds_to_timecode, #minutes_to_seconds, #seconds_to_milliseconds, #timecode_to_milliseconds

Constructor Details

#initialize(text: nil, start_time: nil, end_time: nil, properties: {}) ⇒ Cue

Creates a new Cue class denoting a subtitle.



19
20
21
22
23
24
# File 'lib/captive/cue.rb', line 19

def initialize(text: nil, start_time: nil, end_time: nil, properties: {})
  self.text = text
  self.start_time = start_time
  self.end_time = end_time
  self.properties = properties || {}
end

Instance Attribute Details

#end_timeObject

Returns the value of attribute end_time.



16
17
18
# File 'lib/captive/cue.rb', line 16

def end_time
  @end_time
end

#propertiesObject

Returns the value of attribute properties.



15
16
17
# File 'lib/captive/cue.rb', line 15

def properties
  @properties
end

#start_timeObject

Returns the value of attribute start_time.



16
17
18
# File 'lib/captive/cue.rb', line 16

def start_time
  @start_time
end

#textObject

Returns the value of attribute text.



15
16
17
# File 'lib/captive/cue.rb', line 15

def text
  @text
end

Class Method Details

.from_json(json:, mapping: {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/captive/cue.rb', line 26

def self.from_json(json:, mapping: {})
  schema = {}
  %i[text! start_time! end_time! properties].each do |field|
    field_name = field.to_s.delete('!')
    schema[field] = mapping[field_name] || mapping[field_name.to_sym] || field_name.to_sym
  end
  data = {}
  schema.each do |mask, mapper|
    key = mask[-1] == '!' ? mask.to_s[0...-1].to_sym : mask
    if key.to_s != mask.to_s && !(json.key?(mapper.to_s) || json.key?(mapper.to_sym))
      raise InvalidJsonInput, "Cue missing field: #{mapper}"
    end

    data[key] = json[mapper.to_s] || json[mapper.to_sym]
  end
  new(**data)
end

Instance Method Details

#<=>(other) ⇒ Object



83
84
85
# File 'lib/captive/cue.rb', line 83

def <=>(other)
  start_time <=> other.start_time
end

#add_text(text) ⇒ Object

Adds text. If text is already present, new-line is added before text.



75
76
77
78
79
80
81
# File 'lib/captive/cue.rb', line 75

def add_text(text)
  if self.text.nil?
    self.text = text
  else
    self.text += "\n" + text
  end
end

#as_json(**args) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/captive/cue.rb', line 87

def as_json(**args)
  options = args.delete(:options) || {}
  format = options['format'] || options[:format] || {}
  mapping = (options['mapping'] || options[:mapping] || {}).map { |k, v| [k.to_s, v.to_s] }.to_h
  obj = {
    mapping['start_time'] || 'start_time' => format[:time] == :timecode ? milliseconds_to_timecode(start_time) : start_time,
    mapping['end_time'] || 'end_time' => format[:time] == :timecode ? milliseconds_to_timecode(end_time) : end_time,
    mapping['text'] || 'text' => text,
    mapping['properties'] || 'properties' => properties,
  }
  obj.respond_to?(:as_json) ? obj.as_json(**args) : obj
end

#durationObject



70
71
72
# File 'lib/captive/cue.rb', line 70

def duration
  end_time - start_time
end

#set_times(start_time:, end_time:) ⇒ Object



52
53
54
55
# File 'lib/captive/cue.rb', line 52

def set_times(start_time:, end_time:)
  self.start_time = start_time
  self.end_time = end_time
end