Class: Cased::CLI::Asciinema::File

Inherits:
Object
  • Object
show all
Defined in:
lib/cased/cli/asciinema/file.rb

Constant Summary collapse

OUT =
'o'
IN =
'i'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header, stream) ⇒ File

Returns a new instance of File.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cased/cli/asciinema/file.rb', line 37

def initialize(header, stream)
  @header = header
  @version = header.fetch('version')
  @width = header.fetch('width')
  @height = header.fetch('height')
  self.timestamp = header['timestamp']
  self.duration = header['duration']
  self.idle_time_limit = header['idle_time_limit']
  @command = header['command']
  @title = header['title']
  @env = header.fetch('env', {})
  @theme = header['theme']
  @stream = stream
end

Instance Attribute Details

#commandObject (readonly)

Optional



35
36
37
# File 'lib/cased/cli/asciinema/file.rb', line 35

def command
  @command
end

#durationObject

Optional



35
36
37
# File 'lib/cased/cli/asciinema/file.rb', line 35

def duration
  @duration
end

#envObject (readonly)

Optional



35
36
37
# File 'lib/cased/cli/asciinema/file.rb', line 35

def env
  @env
end

#headerObject (readonly)

Required



32
33
34
# File 'lib/cased/cli/asciinema/file.rb', line 32

def header
  @header
end

#heightObject (readonly)

Required



32
33
34
# File 'lib/cased/cli/asciinema/file.rb', line 32

def height
  @height
end

#idle_time_limitObject

Optional



35
36
37
# File 'lib/cased/cli/asciinema/file.rb', line 35

def idle_time_limit
  @idle_time_limit
end

#streamObject (readonly)

Required



32
33
34
# File 'lib/cased/cli/asciinema/file.rb', line 32

def stream
  @stream
end

#themeObject (readonly)

Optional



35
36
37
# File 'lib/cased/cli/asciinema/file.rb', line 35

def theme
  @theme
end

#timestampObject

Optional



35
36
37
# File 'lib/cased/cli/asciinema/file.rb', line 35

def timestamp
  @timestamp
end

#titleObject (readonly)

Optional



35
36
37
# File 'lib/cased/cli/asciinema/file.rb', line 35

def title
  @title
end

#versionObject (readonly)

Required



32
33
34
# File 'lib/cased/cli/asciinema/file.rb', line 32

def version
  @version
end

#widthObject (readonly)

Required



32
33
34
# File 'lib/cased/cli/asciinema/file.rb', line 32

def width
  @width
end

Class Method Details

.from_cast(cast) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cased/cli/asciinema/file.rb', line 17

def self.from_cast(cast)
  return if cast.blank?

  stream = cast.split("\n").collect do |data|
    JSON.parse(data)
  end
  header = stream.shift
  return unless header.is_a?(Hash)

  new(header, stream)
rescue JSON::ParserError
  nil
end

.from_writer(writer) ⇒ Object



13
14
15
# File 'lib/cased/cli/asciinema/file.rb', line 13

def self.from_writer(writer)
  new(writer.header, writer.stream)
end

Instance Method Details

#to_castObject



81
82
83
84
85
86
87
88
# File 'lib/cased/cli/asciinema/file.rb', line 81

def to_cast
  builder = []
  builder << JSON.dump(header)
  stream.each do |duration, type, data|
    builder << JSON.dump([duration, type, data])
  end
  builder.join("\n")
end

#to_sObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/cased/cli/asciinema/file.rb', line 90

def to_s
  str = []
  stream.each do |_timestamp, type, data|
    next unless type == OUT

    str << data
  end

  str.join
end