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



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

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



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

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
52
# File 'lib/candelabra/remote.rb', line 44

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

.flushObject



143
144
145
146
# File 'lib/candelabra/remote.rb', line 143

def flush
  output { |io| loop { io.read_nonblock(1) } }
rescue # TODO put the correct exception
end

.flush_allObject



138
139
140
141
# File 'lib/candelabra/remote.rb', line 138

def flush_all
  flush
  flush_input
end

.flush_inputObject



148
149
150
151
# File 'lib/candelabra/remote.rb', line 148

def flush_input
  input { |io| loop { io.read_nonblock(1) } }
rescue # TODO put the correct exception
end

.infoObject



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

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

.inputObject

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

Yields and IO reader for pianobar’s output



167
168
169
170
171
# File 'lib/candelabra/remote.rb', line 167

def input
  File.open( Candelabra::Installer.input_path, 'r+' ) do |io|
    yield( io )
  end
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



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

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



157
158
159
160
161
# File 'lib/candelabra/remote.rb', line 157

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

.station_idObject



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

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

.station_idsObject



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

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



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

def stations
  list = []
  execute_command( :stations )
  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