Module: Candelabra::Remote

Defined in:
lib/candelabra/remote.rb

Class Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)

Wrapper for all the commands. This will give you the ability to call the following methods

Example:

Candelabra::Remote.pause
  # => Candelabra.Remote.execute_command :pause

Candelabra::Remote.love
  # => Candelabra.Remote.execute_command :love

Returns result of execute_command or method missing



63
64
65
66
67
68
69
# File 'lib/candelabra/remote.rb', line 63

def method_missing(method,*args)
  if commands.keys.include? method.to_sym
    execute_command method.to_sym
  else
    super method, args
  end
end

Class Method Details

.change_station(station_number = nil) ⇒ Object

When changing stations the user needs to get the list of stations.

Example:

Candelabra::Remote.change_station
  # => [ list of stations ]
Candelabra::Remote.change_station 6
  # => go to the 6th station

Returns list of stations or changes the station to the requested station



80
81
82
83
84
85
86
87
# File 'lib/candelabra/remote.rb', line 80

def change_station( station_number = nil )
  if station_number.nil?
    stations
  else
    execute_command( :change_station )
    execute_command( "s" + station_number.to_s ) unless station_number.nil?
  end
end

.commandsObject

This is a list of avaiable commands for pianobar The list here is the list that we are string with. There are a few more commands but thoses commands might have multple input request and we have to figure out how to do that for the user

Returns hash of commands and key



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/candelabra/remote.rb', line 12

def commands
  {
    :pause          => 'p',
    :continue       => 'p',
    :love           => '+',
    :ban            => '-',
    :bookmark       => 'b',
    :info           => 'i',
    :skip           => 'n',
    :next           => 'n',
    :quit           => 'q',
    :change_station => 's',
    :stations       => 's',
    :tired          => 't',
    :upcomming      => 'u',
    :quick_mix      => 'x',
  }
end

.execute_command(cmd) ⇒ Object

Send the command to pianobar through the fifo file. If Pianobar is not running it will start pianobar, but not send the command.

Example:

If pianobar is NOT running
Candelabra::Remote.execute_command :pause
  # => just starts pianobar.  It doesn't pause it

If pianobar IS running
Candelabra::Remote.execute_command :pause
  # => pauses pianobar

Returns command you passed in



44
45
46
47
48
49
50
51
# File 'lib/candelabra/remote.rb', line 44

def execute_command cmd
  if commands.include? cmd
    %x[ echo #{commands[cmd]} > #{Candelabra::Installer.ctl_path} ]
  else
    %x[ echo #{cmd} > #{Candelabra::Installer.ctl_path} ]
  end
  nil
end

.flushObject



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/candelabra/remote.rb', line 137

def flush
  output do |io| 
    loop do
      begin
        io.read_nonblock(1); 
      rescue 
        break
      end 
    end
  end
end

.infoObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/candelabra/remote.rb', line 105

def info
  execute_command( :info )
  song_info = ''
  output do |io|
    io.lines.each do |line|
      /(Station .+ \(\d+\))/ =~ line
      if $1
        song_info = $1
        break
      end
    end
  end
  song_info
end

.method_missing(method, *args) ⇒ Object

Wrapper for all the commands. This will give you the ability to call the following methods

Example:

Candelabra::Remote.pause
  # => Candelabra.Remote.execute_command :pause

Candelabra::Remote.love
  # => Candelabra.Remote.execute_command :love

Returns result of execute_command or method missing



63
64
65
66
67
68
69
# File 'lib/candelabra/remote.rb', line 63

def method_missing(method,*args)
  if commands.keys.include? method.to_sym
    execute_command method.to_sym
  else
    super method, args
  end
end

.outputObject

The out put file for the commands This contains all the output from pianobar

Yields and IO reader for pianobar’s output



153
154
155
156
157
# File 'lib/candelabra/remote.rb', line 153

def output
  File.open( Candelabra::Installer.output_path, 'r+' ) do |io|
    yield( io )
  end
end

.station_idObject



100
101
102
103
# File 'lib/candelabra/remote.rb', line 100

def station_id
  /(\d+)/ =~ info
  $1
end

.station_idsObject



89
90
91
92
93
94
95
96
97
98
# File 'lib/candelabra/remote.rb', line 89

def station_ids
  return @ids unless @ids.nil?
  @ids = []
  stations.each_with_index do |station,index|
    change_station(index)
    @ids << station_id
  end
  pause # this is because the it will play the last station other wise
  @ids
end

.stationsObject

Get a list of stations from the system read the station list from the command line

Returns an array of stations



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/candelabra/remote.rb', line 124

def stations
  list = []
  execute_command( :change_station )
  output do |io|
    io.lines.each do |line|
      /(\[\?\])/ =~ line
      break if $1 == '[?]' # this denotes the use input for which station to change to
      list << $1 if /(#{list.size}\).+)/ =~ line
    end
  end
  list
end