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:
parent
c3f6aadd0e
commit
7cd737d1ce
@ -50,7 +50,7 @@ module MPDClient
|
|||||||
|
|
||||||
on :playlist do
|
on :playlist do
|
||||||
each_conn do |conn|
|
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"
|
conn << "data: #{json}\n\n"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -105,7 +105,7 @@ module MPDClient
|
|||||||
|
|
||||||
get '/queue' do
|
get '/queue' do
|
||||||
content_type 'application/json'
|
content_type 'application/json'
|
||||||
{ data: Song.queue }.to_json
|
{ data: Queue.new }.to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
put '/control/play' do
|
put '/control/play' do
|
||||||
|
|||||||
@ -12,6 +12,7 @@ module MPDClient
|
|||||||
autoload :Album, File.expand_path('mpd_client/album.rb', __dir__)
|
autoload :Album, File.expand_path('mpd_client/album.rb', __dir__)
|
||||||
autoload :Artist, File.expand_path('mpd_client/artist.rb', __dir__)
|
autoload :Artist, File.expand_path('mpd_client/artist.rb', __dir__)
|
||||||
autoload :Control, File.expand_path('mpd_client/control.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_HOST = ENV.fetch('MPD_HOST', 'localhost')
|
||||||
MPD_PORT = ENV.fetch('MPD_PORT', 6600)
|
MPD_PORT = ENV.fetch('MPD_PORT', 6600)
|
||||||
|
|||||||
28
lib/mpd_client/queue.rb
Normal file
28
lib/mpd_client/queue.rb
Normal 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
|
||||||
@ -50,10 +50,6 @@ module MPDClient
|
|||||||
MPDClient.conn.songs.map(&self)
|
MPDClient.conn.songs.map(&self)
|
||||||
end
|
end
|
||||||
|
|
||||||
def queue
|
|
||||||
MPDClient.conn.queue.map(&self)
|
|
||||||
end
|
|
||||||
|
|
||||||
def current_song
|
def current_song
|
||||||
if song = MPDClient.conn.current_song
|
if song = MPDClient.conn.current_song
|
||||||
new(song)
|
new(song)
|
||||||
|
|||||||
17
spec/lib/mpd_client/queue_spec.rb
Normal file
17
spec/lib/mpd_client/queue_spec.rb
Normal 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
|
||||||
@ -11,13 +11,6 @@ describe MPDClient::Song do
|
|||||||
MPDClient.conn.stub(:queue).and_return([song1, song2])
|
MPDClient.conn.stub(:queue).and_return([song1, song2])
|
||||||
end
|
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
|
describe "#playing?" do
|
||||||
let(:playing_song) { subject.new(song1) }
|
let(:playing_song) { subject.new(song1) }
|
||||||
let(:not_playing_song) { subject.new(song2) }
|
let(:not_playing_song) { subject.new(song2) }
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user