Class: Somadic::BaseChannel

Inherits:
Object
  • Object
show all
Defined in:
lib/somadic/base_channel.rb

Direct Known Subclasses

Channel::DI, Channel::SlayRadio, Channel::Soma

Constant Summary collapse

API_TIMEOUT =
60
ONE_DAY =
86400

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ BaseChannel

Returns a new instance of BaseChannel.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/somadic/base_channel.rb', line 8

def initialize(options)
  @url = options[:url]
  playlist = @url.split('/').last
  if playlist.end_with?('.pls')
    name = playlist[0..playlist.index('.pls') - 1]
  elsif playlist.end_with?('.m3u')
    name = playlist[0..playlist.index('.m3u') - 1]
  else
    Somadic::Logger.error("BaseChannel#initialize: bad playlist #{playlist}")
    return
  end
  @channel = find_channel(name)

  @mp = Mplayer.new(options)
  @mp.add_observer(self)
  @listeners = options[:listeners]
end

Instance Attribute Details

#channelsObject (readonly)

Returns the value of attribute channels.



3
4
5
# File 'lib/somadic/base_channel.rb', line 3

def channels
  @channels
end

#songObject (readonly)

Returns the value of attribute song.



3
4
5
# File 'lib/somadic/base_channel.rb', line 3

def song
  @song
end

Instance Method Details

#channel_listObject



60
61
62
# File 'lib/somadic/base_channel.rb', line 60

def channel_list
  @channels
end

#find_channel(name) ⇒ Object

Raises:

  • (NotImplementedError)


56
57
58
# File 'lib/somadic/base_channel.rb', line 56

def find_channel(name)
  raise NotImplementedError
end

#startObject

Let’s go already.



27
28
29
30
31
32
# File 'lib/somadic/base_channel.rb', line 27

def start
  Somadic::Logger.debug('BaseChannel#start')
  @mp.start
rescue => e
  Somadic::Logger.error("BaseChannel#start error: #{e}")
end

#stopObject

Enough already.



35
36
37
38
# File 'lib/somadic/base_channel.rb', line 35

def stop
  Somadic::Logger.debug('BaseChannel#stop')
  @mp.stop
end

#stopped?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/somadic/base_channel.rb', line 40

def stopped?
  @mp.stopped?
end

#update(time, song) ⇒ Object

Observer callback, and also one of the simplest displays possible.



45
46
47
48
49
50
51
52
53
54
# File 'lib/somadic/base_channel.rb', line 45

def update(time, song)
  Somadic::Logger.debug("BaseChannel#update: #{time} - #{song}")
  songs = [{ 'started' => Time.now.to_i - 1,
             'duration' => 1,
             'track' => song,
             'votes' => { 'up' => 0, 'down' => 0 } }]
  @listeners.each do |l|
    l.update(@channel, songs) if l.respond_to?(:update)
  end
end