Module: Captive::Base::ClassMethods

Defined in:
lib/captive/base.rb

Instance Method Summary collapse

Instance Method Details

#from_blob(blob:) ⇒ Object



14
15
16
# File 'lib/captive/base.rb', line 14

def from_blob(blob:)
  new(cue_list: parse(blob: blob))
end

#from_file(filename:) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/captive/base.rb', line 6

def from_file(filename:)
  file = File.new(filename, 'r:bom|utf-8')
  blob = file.read
  from_blob(blob: blob)
ensure
  file&.close
end

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

Raises:



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

def from_json(json:, mapping: {})
  json = JSON.parse(json) if json.is_a?(String)
  cues = json['cues'] || json[:cues]
  raise InvalidJsonInput unless cues.is_a?(Array)

  cues.map! { |cue_json| Cue.from_json(json: cue_json, mapping: mapping) }
  new(cue_list: cues)
end