diff --git a/bin/mpd_client b/bin/mpd_client index d0d1ffd..864007b 100755 --- a/bin/mpd_client +++ b/bin/mpd_client @@ -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 diff --git a/lib/mpd_client.rb b/lib/mpd_client.rb index 3a07830..7d9bfd4 100644 --- a/lib/mpd_client.rb +++ b/lib/mpd_client.rb @@ -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) diff --git a/lib/mpd_client/queue.rb b/lib/mpd_client/queue.rb new file mode 100644 index 0000000..326ee0d --- /dev/null +++ b/lib/mpd_client/queue.rb @@ -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 diff --git a/lib/mpd_client/song.rb b/lib/mpd_client/song.rb index 41079bf..d50c8df 100644 --- a/lib/mpd_client/song.rb +++ b/lib/mpd_client/song.rb @@ -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) diff --git a/spec/lib/mpd_client/queue_spec.rb b/spec/lib/mpd_client/queue_spec.rb new file mode 100644 index 0000000..1e6a9d0 --- /dev/null +++ b/spec/lib/mpd_client/queue_spec.rb @@ -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 diff --git a/spec/lib/mpd_client/song_spec.rb b/spec/lib/mpd_client/song_spec.rb index 569d5d1..5bc6dd7 100644 --- a/spec/lib/mpd_client/song_spec.rb +++ b/spec/lib/mpd_client/song_spec.rb @@ -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) }