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

Fix up API for artists/albums/songs.

This commit is contained in:
Dan Barber 2013-12-10 17:59:29 +00:00
parent 97d262b6d2
commit 637054ad44
5 changed files with 44 additions and 20 deletions

View File

@ -7,7 +7,7 @@ gem 'sinatra-contrib'
gem 'sinatra-asset-pipeline'
gem 'foreman'
gem 'ruby-mpd'
gem 'ruby-mpd', git: 'git@github.com:archSeer/ruby-mpd.git'
group :development, :test do
gem 'pry'

View File

@ -1,3 +1,9 @@
GIT
remote: git@github.com:archSeer/ruby-mpd.git
revision: 14ad532729529f22a852f747d379c2fa90eacda2
specs:
ruby-mpd (0.3.0)
GEM
remote: https://rubygems.org/
specs:
@ -36,7 +42,6 @@ GEM
rspec-expectations (2.14.3)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.4)
ruby-mpd (0.3.0)
sass (3.2.12)
shotgun (0.9)
rack (>= 1.0)
@ -85,7 +90,7 @@ DEPENDENCIES
pry
rspec
rspec-mocks
ruby-mpd
ruby-mpd!
shotgun
sinatra
sinatra-asset-pipeline

View File

@ -3,7 +3,7 @@ require './models/mpd_connection'
class Album < Struct.new(:title, :artist, :genre, :year)
def initialize(album)
first_song = MPDConnection.mpd.search(:album, album).first
first_song = MPDConnection.mpd.where(album: album).first
self.title = first_song.album
self.artist = first_song.artist
self.genre = first_song.genre
@ -15,7 +15,7 @@ class Album < Struct.new(:title, :artist, :genre, :year)
end
def self.all
MPDConnection.mpd.albums.sort.map { |artist| self.new(album) }
MPDConnection.mpd.albums.sort.map { |album| self.new(album) }
end
def self.by_artist(artist)

View File

@ -1,9 +1,10 @@
require './models/mpd_connection'
class Song < Struct.new(:id, :artist, :album, :title, :length, :pos, :playing)
class Song < Struct.new(:id, :track, :artist, :album, :title, :length, :pos, :playing)
def initialize(song, pos: nil, playing: false)
self.id = song.id
self.track = song.track
self.artist = song.artist
self.album = song.album
self.title = song.title
@ -12,8 +13,24 @@ class Song < Struct.new(:id, :artist, :album, :title, :length, :pos, :playing)
self.playing = playing
end
def <=>(song)
title <=> song.title
end
def self.queue
current_song = MPDConnection.mpd.status[:songid]
MPDConnection.mpd.queue.map { |song| self.new(song, playing: (song.id == current_song), pos: song.pos) }
end
def self.all
MPDConnection.mpd.songs.map { |album| self.new(album) }
end
def self.by_artist(artist)
MPDConnection.mpd.where(artist: artist).map { |song| self.new(song) }
end
def self.by_album(artist, album)
MPDConnection.mpd.where(artist: artist, album: album).map { |song| self.new(song) }
end
end

View File

@ -63,27 +63,29 @@ class MPDClient < Sinatra::Base
get '/albums' do
content_type 'application/json'
JSON Album.all.map(&:to_h)
if params[:artist]
JSON Album.by_artist(CGI.unescape(params[:artist])).sort.map(&:to_h)
else
JSON Album.all.map(&:to_h)
end
end
get '/artists/:artist' do
content_type 'application/json'
JSON Album.by_artist(CGI.unescape(params[:artist])).sort.map(&:to_h)
end
#get '/albums/:album' do
#JSON get_songs_by_album(CGI.unescape(params[:album]))
#end
#get '/artists/:artist/:album' do
#JSON get_songs_by_album(CGI.unescape(params[:album]))
#end
get '/artists' do
content_type 'application/json'
JSON Artist.all.map(&:to_h)
end
get '/songs' do
content_type 'application/json'
if params[:artist] && params[:album]
JSON Song.by_album(CGI.unescape(params[:artist]), CGI.unescape(params[:album])).map(&:to_h)
elsif params[:artist]
JSON Song.by_artist(CGI.unescape(params[:artist])).map(&:to_h)
else
JSON Song.all.sort.map(&:to_h)
end
end
get '/queue' do
content_type 'application/json'
JSON({ data: Song.queue.map(&:to_h) })