From 637054ad44b56b8e0e0e0abd375aa3f82b961414 Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Tue, 10 Dec 2013 17:59:29 +0000 Subject: [PATCH] Fix up API for artists/albums/songs. --- Gemfile | 2 +- Gemfile.lock | 9 +++++++-- models/album.rb | 4 ++-- models/song.rb | 19 ++++++++++++++++++- mpd_client.rb | 30 ++++++++++++++++-------------- 5 files changed, 44 insertions(+), 20 deletions(-) diff --git a/Gemfile b/Gemfile index b813c01..3740c46 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index c1933cc..2fc0d2f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/models/album.rb b/models/album.rb index 681073a..1897c23 100644 --- a/models/album.rb +++ b/models/album.rb @@ -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) diff --git a/models/song.rb b/models/song.rb index 331a8c3..508334d 100644 --- a/models/song.rb +++ b/models/song.rb @@ -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 diff --git a/mpd_client.rb b/mpd_client.rb index bc16042..7d5a46e 100644 --- a/mpd_client.rb +++ b/mpd_client.rb @@ -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) })