From 68042e18e0033a7a84619a29415151a67de50193 Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Mon, 2 Sep 2013 14:40:39 +0100 Subject: [PATCH] Add rspec and split out models. --- Gemfile | 9 +++++++++ Gemfile.lock | 19 +++++++++++++++++++ models/album.rb | 26 ++++++++++++++++++++++++++ models/mpd_connection.rb | 7 +++++++ mpd_client.rb | 13 +++++++------ spec/models/album_spec.rb | 31 +++++++++++++++++++++++++++++++ spec/mpd_client_spec.rb | 8 ++++++++ spec/spec_helper.rb | 20 ++++++++++++++++++++ 8 files changed, 127 insertions(+), 6 deletions(-) create mode 100644 models/album.rb create mode 100644 models/mpd_connection.rb create mode 100644 spec/models/album_spec.rb create mode 100644 spec/mpd_client_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/Gemfile b/Gemfile index 1f6e65b..086c0f1 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,15 @@ gem 'foreman' gem 'ruby-mpd' +group :development, :test do + gem 'pry' +end + +group :test do + gem 'rspec' + gem 'rspec-mocks' +end + group :development do gem 'shotgun' end diff --git a/Gemfile.lock b/Gemfile.lock index 521bced..52ccde7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,18 +2,33 @@ GEM remote: https://rubygems.org/ specs: backports (3.3.3) + coderay (1.0.9) daemons (1.1.9) + diff-lcs (1.2.4) dotenv (0.8.0) eventmachine (1.0.3) foreman (0.63.0) dotenv (>= 0.7) thor (>= 0.13.6) + method_source (0.8.2) multi_json (1.7.9) + pry (0.9.12.2) + coderay (~> 1.0.5) + method_source (~> 0.8) + slop (~> 3.4) rack (1.5.2) rack-protection (1.5.0) rack rack-test (0.6.2) rack (>= 1.0) + rspec (2.14.1) + rspec-core (~> 2.14.0) + rspec-expectations (~> 2.14.0) + rspec-mocks (~> 2.14.0) + rspec-core (2.14.5) + rspec-expectations (2.14.2) + diff-lcs (>= 1.1.3, < 2.0) + rspec-mocks (2.14.3) ruby-mpd (0.2.4) shotgun (0.9) rack (>= 1.0) @@ -28,6 +43,7 @@ GEM rack-test sinatra (~> 1.4.0) tilt (~> 1.3) + slop (3.4.6) thin (1.5.1) daemons (>= 1.0.9) eventmachine (>= 0.12.6) @@ -40,6 +56,9 @@ PLATFORMS DEPENDENCIES foreman + pry + rspec + rspec-mocks ruby-mpd shotgun sinatra diff --git a/models/album.rb b/models/album.rb new file mode 100644 index 0000000..20a86a2 --- /dev/null +++ b/models/album.rb @@ -0,0 +1,26 @@ +require './models/mpd_connection' + +class Album + attr_accessor :title, :genre, :year + + def initialize(album) + first_song = MPDConnection.mpd.search(:album, album).first + @title = first_song.album + @genre = first_song.genre + @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 attributes + { title: @title, + genre: @genre, + year: @year } + end +end diff --git a/models/mpd_connection.rb b/models/mpd_connection.rb new file mode 100644 index 0000000..b460b02 --- /dev/null +++ b/models/mpd_connection.rb @@ -0,0 +1,7 @@ +class MPDConnection + def self.mpd + @@mpd ||= MPD.new + @@mpd.connect unless @@mpd.connected? + @@mpd + end +end diff --git a/mpd_client.rb b/mpd_client.rb index 0484169..fec84a0 100644 --- a/mpd_client.rb +++ b/mpd_client.rb @@ -1,9 +1,12 @@ -require 'sinatra' -require "sinatra/namespace" +require 'bundler' +ENV['RACK_ENV'] ||= 'development' +Bundler.require(:default, ENV['RACK_ENV']) + require 'json' -require 'ruby-mpd' require 'cgi' +require './models/album' + class MPDClient < Sinatra::Base register Sinatra::Namespace @@ -55,9 +58,7 @@ class MPDClient < Sinatra::Base end def mpd - @mpd ||= MPD.new - @mpd.connect unless @mpd.connected? - @mpd + MPDConnection.mpd end end diff --git a/spec/models/album_spec.rb b/spec/models/album_spec.rb new file mode 100644 index 0000000..1bd4669 --- /dev/null +++ b/spec/models/album_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe Album do + + let(:song1) { MPD::Song.new({ album: 'Back in Black', genre: 'Rock', date: '1980' }) } + let(:song2) { MPD::Song.new({ album: 'Highway to Hell', genre: 'Rock', date: '1979' }) } + + before do + MPDConnection.mpd.stub(:albums).and_return([song1.album, song2.album]) + MPDConnection.mpd.stub(:search).and_return([song1, song2]) + MPDConnection.mpd.stub(:search).with(:album, song1.album).and_return([song1]) + MPDConnection.mpd.stub(:search).with(:album, song2.album).and_return([song2]) + end + + it 'has attributes based on first song' do + album = Album.new(song1.album) + expect(album.title).to eq(song1.album) + expect(album.genre).to eq(song1.genre) + expect(album.year).to eq(song1.date) + end + + it 'should return a list of albums' do + expect(Album.by_artist('AC/DC')).to have(2).items + end + + it 'should sort the albums by year' do + albums = Album.by_artist('AC/DC') + expect(albums.sort.map(&:year)).to eq(['1979', '1980']) + end + +end diff --git a/spec/mpd_client_spec.rb b/spec/mpd_client_spec.rb new file mode 100644 index 0000000..7c600f1 --- /dev/null +++ b/spec/mpd_client_spec.rb @@ -0,0 +1,8 @@ +require 'spec_helper' + +describe MPDClient do + it 'should respond to GET' do + get '/' + expect(last_response.status).to eq(404) + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..ea68e56 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,20 @@ +ENV['RACK_ENV'] = 'test' + +require File.join(File.dirname(__FILE__), '..', 'mpd_client.rb') + +require 'sinatra' +require 'rack/test' + +# setup test environment +set :environment, :test +set :run, false +set :raise_errors, true +set :logging, false + +def app + MPDClient +end + +RSpec.configure do |config| + config.include Rack::Test::Methods +end