Class: LastFM::Client
- Inherits:
-
Object
show all
- Defined in:
- lib/lastfm.rb
Defined Under Namespace
Classes: CommandLineClient, TelnetClient
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(lastfm, audio_output) ⇒ Client
Returns a new instance of Client.
135
136
137
138
139
|
# File 'lib/lastfm.rb', line 135
def initialize(lastfm, audio_output)
@lastfm = lastfm
@audio_output = audio_output
@output_buffer = []
end
|
Instance Attribute Details
#current_song ⇒ Object
Returns the value of attribute current_song.
134
135
136
|
# File 'lib/lastfm.rb', line 134
def current_song
@current_song
end
|
Instance Method Details
#display_current_song ⇒ Object
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
# File 'lib/lastfm.rb', line 218
def display_current_song
song = @lastfm.current_song
if song.nil?
return "\nNo current song\n" unless @current_song
else
@current_song = song
end
@output_buffer << "Currently Playing:"
@output_buffer << divider
%W{ artist track album station }.each do |x|
unless song.respond_to?(x)
@output_buffer = "something is wrong with the last.fm connection. no song data"
break
end
@output_buffer << x + ": " + song.send(x)
end
@output_buffer << divider
@output_buffer
end
|
#divider ⇒ Object
214
215
216
|
# File 'lib/lastfm.rb', line 214
def divider
"-" * 80
end
|
#flush_output_buffer ⇒ Object
239
240
241
242
243
244
245
246
247
|
# File 'lib/lastfm.rb', line 239
def flush_output_buffer
temp = @output_buffer.dup
@output_buffer = []
if temp.class == String
temp
else
temp.join("\n")
end
end
|
#help ⇒ Object
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
# File 'lib/lastfm.rb', line 253
def help
%Q[Commands: skip
love
ban
recommended
artist [artist]
tag [tag]
tags [tag+tag]
volume
volume [1-100]
stop
play
quit
[return]
You can truncate the commands to the fewest possible
unambiguous letters.
LastFM is sometimes unresponsive. In that case,
try "skip" every few seconds until it works again.
lastfm> ]
end
|
#parse_command(command, *args) ⇒ Object
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/lastfm.rb', line 143
def parse_command(command, *args)
if command.nil?
command = "current song info"
end
args = args.join(' ').strip
puts "request: #{command} #{args}"
case command
when /^h/
@output_buffer << help
when /^sk/ skip
@output_buffer << "skipping..."
when /^st/ stop
@output_buffer << "stopping..."
when /^pl/ play
@output_buffer << "playing..."
when /^v/
if args != ''
@audio_output.volume = args.to_i
@output_buffer << "setting volume to #{args}"
else
@output_buffer << "current volume: #{@audio_output.volume}"
end
when /^l/ @lastfm.action("love")
when /^b/ @lastfm.action("ban")
when /^q/ @audio_output.stop
exit
when /^a/ tune("artist/#{args}/similarartists")
when /^t/ tune("globaltags/#{args}")
when /^r/ tune("user/#{@lastfm.username}/recommended/100")
when /^n/ tune("user/#{@lastfm.username}/neighbours")
else
display_current_song
end
end
|
#play ⇒ Object
206
207
208
|
# File 'lib/lastfm.rb', line 206
def play
skip
end
|
#prompt ⇒ Object
249
250
251
|
# File 'lib/lastfm.rb', line 249
def prompt
"Press h for help\nlastfm> "
end
|
#skip ⇒ Object
198
199
200
201
202
203
204
|
# File 'lib/lastfm.rb', line 198
def skip
@audio_output.skip
sleep 3 display_current_song
end
|
#start ⇒ Object
140
141
142
|
# File 'lib/lastfm.rb', line 140
def start
display_current_song
end
|
#stop ⇒ Object
210
211
212
|
# File 'lib/lastfm.rb', line 210
def stop
@audio_output.stop
end
|
#tune(path) ⇒ Object
191
192
193
194
195
196
|
# File 'lib/lastfm.rb', line 191
def tune(path)
@lastfm.tune(path=URI.escape(path))
@output_buffer << "tuning to #{path}"
sleep 1
skip
end
|