Class: CastCaster::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/castcaster/channel.rb

Constant Summary collapse

STATUSES =
%w[stopped live error].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attrs = {}) ⇒ Channel

Returns a new instance of Channel.



8
9
10
11
12
13
14
15
16
17
# File 'lib/castcaster/channel.rb', line 8

def initialize(name, attrs = {})
  @name = name
  @engine = attrs['engine'] || 'default'
  @source = attrs['source'] || {}
  @hls = attrs['hls'] || { 'fragment' => 6, 'playlist_length' => 60 }
  @transcode = attrs['transcode']
  @snapshot = attrs['snapshot']
  @status = attrs['status'] || 'stopped'
  @path = File.join(self.class.channels_dir, "#{name}.yml")
end

Instance Attribute Details

#engineObject

Returns the value of attribute engine.



6
7
8
# File 'lib/castcaster/channel.rb', line 6

def engine
  @engine
end

#hlsObject

Returns the value of attribute hls.



6
7
8
# File 'lib/castcaster/channel.rb', line 6

def hls
  @hls
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/castcaster/channel.rb', line 5

def name
  @name
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/castcaster/channel.rb', line 5

def path
  @path
end

#snapshotObject

Returns the value of attribute snapshot.



6
7
8
# File 'lib/castcaster/channel.rb', line 6

def snapshot
  @snapshot
end

#sourceObject

Returns the value of attribute source.



6
7
8
# File 'lib/castcaster/channel.rb', line 6

def source
  @source
end

#statusObject

Returns the value of attribute status.



6
7
8
# File 'lib/castcaster/channel.rb', line 6

def status
  @status
end

#transcodeObject

Returns the value of attribute transcode.



6
7
8
# File 'lib/castcaster/channel.rb', line 6

def transcode
  @transcode
end

Class Method Details

.allObject



53
54
55
56
57
# File 'lib/castcaster/channel.rb', line 53

def all
  Dir[File.join(channels_dir, '*.yml')].map do |f|
    from_file(f)
  end.compact.sort_by(&:name)
end

.channels_dirObject



49
50
51
# File 'lib/castcaster/channel.rb', line 49

def channels_dir
  File.join(Dir.pwd, 'channels')
end

.create(name, attrs = {}) ⇒ Object

Raises:



65
66
67
68
69
70
71
# File 'lib/castcaster/channel.rb', line 65

def create(name, attrs = {})
  ch = new(name, attrs)
  ch.validate!
  raise Error, "Channel '#{name}' already exists" if File.exist?(ch.path)
  ch.save
  ch
end

.find(name) ⇒ Object



59
60
61
62
63
# File 'lib/castcaster/channel.rb', line 59

def find(name)
  path = File.join(channels_dir, "#{name}.yml")
  return nil unless File.exist?(path)
  from_file(path)
end

Instance Method Details

#destroyObject



35
36
37
# File 'lib/castcaster/channel.rb', line 35

def destroy
  File.delete(@path) if File.exist?(@path)
end

#live?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/castcaster/channel.rb', line 39

def live?
  @status == 'live'
end

#saveObject



31
32
33
# File 'lib/castcaster/channel.rb', line 31

def save
  File.write(@path, YAML.dump(to_h))
end

#to_hObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/castcaster/channel.rb', line 19

def to_h
  {
    'name' => @name,
    'engine' => @engine,
    'source' => @source,
    'hls' => @hls,
    'transcode' => @transcode,
    'snapshot' => @snapshot,
    'status' => @status
  }.compact
end

#validate!Object

Raises:



43
44
45
46
# File 'lib/castcaster/channel.rb', line 43

def validate!
  raise Error, "Channel name required" if @name.nil? || @name.empty?
  raise Error, "Invalid channel name: #{@name}" unless @name.match?(/\A[a-zA-Z0-9_-]+\z/)
end