diff --git a/models/album.rb b/models/album.rb index 20a86a2..b1d80f4 100644 --- a/models/album.rb +++ b/models/album.rb @@ -10,14 +10,18 @@ class Album @year = first_song.date end - def self.by_artist(artist) - MPDConnection.mpd.albums(artist).map { |album| Album.new(album) } - end - def <=>(album) year <=> album.year end + def self.all + MPDConnection.mpd.albums.sort.map { |artist| Album.new(album) } + end + + def self.by_artist(artist) + MPDConnection.mpd.albums(artist).map { |album| Album.new(album) } + end + def attributes { title: @title, genre: @genre, diff --git a/models/artist.rb b/models/artist.rb new file mode 100644 index 0000000..db1e556 --- /dev/null +++ b/models/artist.rb @@ -0,0 +1,21 @@ +require './models/mpd_connection' + +class Artist + attr_accessor :name + + def initialize(name) + @name = name + end + + def <=>(artist) + name <=> artist.name + end + + def self.all + MPDConnection.mpd.artists.sort.map { |artist| Artist.new(artist) } + end + + def attributes + { name: @name } + end +end diff --git a/mpd_client.rb b/mpd_client.rb index fec84a0..bced13f 100644 --- a/mpd_client.rb +++ b/mpd_client.rb @@ -6,6 +6,7 @@ require 'json' require 'cgi' require './models/album' +require './models/artist' class MPDClient < Sinatra::Base register Sinatra::Namespace @@ -13,52 +14,25 @@ class MPDClient < Sinatra::Base namespace '/api' do get '/albums' do - JSON mpd.albums + JSON Album.all.map(&:attributes) end get '/artists/:artist' do - JSON get_albums_by_artist(CGI.unescape(params[:artist])) + JSON Album.by_artist(CGI.unescape(params[:artist])).sort.map(&:attributes) end - get '/albums/:album' do - JSON get_songs_by_album(CGI.unescape(params[:album])) - 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/:artist/:album' do + #JSON get_songs_by_album(CGI.unescape(params[:album])) + #end get '/artists' do - JSON mpd.artists + JSON Artist.all.map(&:attributes) end end - private - - def get_albums_by_artist(artist) - mpd.albums(artist).map { |album| album_info(album) }.sort { |a, b| a[:year] <=> b[:year] } - end - - def get_songs_by_album(album) - mpd.search(:album, album).map { |song| song_info(song) } - end - - def song_info(song) - { disc: song.disc, - track: song.track, - title: song.title } - end - - def album_info(album) - first_song = mpd.search(:album, album).first - { title: first_song.album, - genre: first_song.genre, - year: first_song.date } - end - - def mpd - MPDConnection.mpd - end - end diff --git a/spec/models/artist_spec.rb b/spec/models/artist_spec.rb new file mode 100644 index 0000000..655e610 --- /dev/null +++ b/spec/models/artist_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe Artist do + + let(:artist) { Artist.new('Alice Cooper') } + + it 'has attributes' do + expect(artist.name).to eq('Alice Cooper') + end + +end