1
0
mirror of https://github.com/danbee/mpd-client synced 2025-03-04 08:39:09 +00:00

Extract the queue into its own class

This commit is contained in:
Lee Machin 2013-12-11 13:51:32 +00:00
parent c3f6aadd0e
commit 7cd737d1ce
6 changed files with 48 additions and 13 deletions

View File

@ -50,7 +50,7 @@ module MPDClient
on :playlist do
each_conn do |conn|
json = { type: 'queue', data: Song.queue }.to_json
json = { type: 'queue', data: Queue.new }.to_json
conn << "data: #{json}\n\n"
end
end
@ -105,7 +105,7 @@ module MPDClient
get '/queue' do
content_type 'application/json'
{ data: Song.queue }.to_json
{ data: Queue.new }.to_json
end
put '/control/play' do

View File

@ -12,6 +12,7 @@ module MPDClient
autoload :Album, File.expand_path('mpd_client/album.rb', __dir__)
autoload :Artist, File.expand_path('mpd_client/artist.rb', __dir__)
autoload :Control, File.expand_path('mpd_client/control.rb', __dir__)
autoload :Queue, File.expand_path('mpd_client/queue.rb', __dir__)
MPD_HOST = ENV.fetch('MPD_HOST', 'localhost')
MPD_PORT = ENV.fetch('MPD_PORT', 6600)

28
lib/mpd_client/queue.rb Normal file
View File

@ -0,0 +1,28 @@
module MPDClient
class Queue
include Enumerable
include Jsonable
attr :songs
def initialize
@songs = fetch_songs
end
def each(&block)
songs.each(&block)
end
def to_h
map(&:to_h)
end
private
def fetch_songs
MPDClient.conn.queue.map(&Song)
end
end
end

View File

@ -50,10 +50,6 @@ module MPDClient
MPDClient.conn.songs.map(&self)
end
def queue
MPDClient.conn.queue.map(&self)
end
def current_song
if song = MPDClient.conn.current_song
new(song)

View File

@ -0,0 +1,17 @@
require 'spec_helper'
describe MPDClient::Queue do
subject { MPDClient::Queue.new }
let(:song1) { MPD::Song.new({ title: 'Back in Black', album: 'Back in Black', genre: 'Rock', date: '1980' }) }
let(:song2) { MPD::Song.new({ title: 'Highway to Hell', album: 'Highway to Hell', genre: 'Rock', date: '1979' }) }
before do
MPDClient.conn.stub(:queue).and_return([song1, song2])
end
its(:songs) { should have(2).items }
end

View File

@ -11,13 +11,6 @@ describe MPDClient::Song do
MPDClient.conn.stub(:queue).and_return([song1, song2])
end
describe "#queue" do
it "returns the list of songs" do
queue = subject.queue
expect(queue).to have(2).items
end
end
describe "#playing?" do
let(:playing_song) { subject.new(song1) }
let(:not_playing_song) { subject.new(song2) }