Class: Somadic::BaseChannel

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

Direct Known Subclasses

Channel::DI, 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
# File 'lib/somadic/base_channel.rb', line 8

def initialize(options)
  @url = options[:url]
  playlist = @url.split('/').last
  name = playlist[0..playlist.index('.pls') - 1]
  @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



53
54
55
# File 'lib/somadic/base_channel.rb', line 53

def channel_list
  @channels
end

#find_channel(name) ⇒ Object

Raises:

  • (NotImplementedError)


49
50
51
# File 'lib/somadic/base_channel.rb', line 49

def find_channel(name)
  raise NotImplementedError
end

#startObject

Let’s go already.



20
21
22
23
24
25
# File 'lib/somadic/base_channel.rb', line 20

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

#stopObject

Enough already.



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

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

#stopped?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/somadic/base_channel.rb', line 33

def stopped?
  @mp.stopped?
end

#update(time, song) ⇒ Object

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



38
39
40
41
42
43
44
45
46
47
# File 'lib/somadic/base_channel.rb', line 38

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