mirror of
https://github.com/danbee/mpd-client
synced 2025-03-04 08:39:09 +00:00
Add rspec and split out models.
This commit is contained in:
parent
abe7ad7626
commit
68042e18e0
9
Gemfile
9
Gemfile
@ -6,6 +6,15 @@ gem 'foreman'
|
|||||||
|
|
||||||
gem 'ruby-mpd'
|
gem 'ruby-mpd'
|
||||||
|
|
||||||
|
group :development, :test do
|
||||||
|
gem 'pry'
|
||||||
|
end
|
||||||
|
|
||||||
|
group :test do
|
||||||
|
gem 'rspec'
|
||||||
|
gem 'rspec-mocks'
|
||||||
|
end
|
||||||
|
|
||||||
group :development do
|
group :development do
|
||||||
gem 'shotgun'
|
gem 'shotgun'
|
||||||
end
|
end
|
||||||
|
|||||||
19
Gemfile.lock
19
Gemfile.lock
@ -2,18 +2,33 @@ GEM
|
|||||||
remote: https://rubygems.org/
|
remote: https://rubygems.org/
|
||||||
specs:
|
specs:
|
||||||
backports (3.3.3)
|
backports (3.3.3)
|
||||||
|
coderay (1.0.9)
|
||||||
daemons (1.1.9)
|
daemons (1.1.9)
|
||||||
|
diff-lcs (1.2.4)
|
||||||
dotenv (0.8.0)
|
dotenv (0.8.0)
|
||||||
eventmachine (1.0.3)
|
eventmachine (1.0.3)
|
||||||
foreman (0.63.0)
|
foreman (0.63.0)
|
||||||
dotenv (>= 0.7)
|
dotenv (>= 0.7)
|
||||||
thor (>= 0.13.6)
|
thor (>= 0.13.6)
|
||||||
|
method_source (0.8.2)
|
||||||
multi_json (1.7.9)
|
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 (1.5.2)
|
||||||
rack-protection (1.5.0)
|
rack-protection (1.5.0)
|
||||||
rack
|
rack
|
||||||
rack-test (0.6.2)
|
rack-test (0.6.2)
|
||||||
rack (>= 1.0)
|
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)
|
ruby-mpd (0.2.4)
|
||||||
shotgun (0.9)
|
shotgun (0.9)
|
||||||
rack (>= 1.0)
|
rack (>= 1.0)
|
||||||
@ -28,6 +43,7 @@ GEM
|
|||||||
rack-test
|
rack-test
|
||||||
sinatra (~> 1.4.0)
|
sinatra (~> 1.4.0)
|
||||||
tilt (~> 1.3)
|
tilt (~> 1.3)
|
||||||
|
slop (3.4.6)
|
||||||
thin (1.5.1)
|
thin (1.5.1)
|
||||||
daemons (>= 1.0.9)
|
daemons (>= 1.0.9)
|
||||||
eventmachine (>= 0.12.6)
|
eventmachine (>= 0.12.6)
|
||||||
@ -40,6 +56,9 @@ PLATFORMS
|
|||||||
|
|
||||||
DEPENDENCIES
|
DEPENDENCIES
|
||||||
foreman
|
foreman
|
||||||
|
pry
|
||||||
|
rspec
|
||||||
|
rspec-mocks
|
||||||
ruby-mpd
|
ruby-mpd
|
||||||
shotgun
|
shotgun
|
||||||
sinatra
|
sinatra
|
||||||
|
|||||||
26
models/album.rb
Normal file
26
models/album.rb
Normal file
@ -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
|
||||||
7
models/mpd_connection.rb
Normal file
7
models/mpd_connection.rb
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
class MPDConnection
|
||||||
|
def self.mpd
|
||||||
|
@@mpd ||= MPD.new
|
||||||
|
@@mpd.connect unless @@mpd.connected?
|
||||||
|
@@mpd
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -1,9 +1,12 @@
|
|||||||
require 'sinatra'
|
require 'bundler'
|
||||||
require "sinatra/namespace"
|
ENV['RACK_ENV'] ||= 'development'
|
||||||
|
Bundler.require(:default, ENV['RACK_ENV'])
|
||||||
|
|
||||||
require 'json'
|
require 'json'
|
||||||
require 'ruby-mpd'
|
|
||||||
require 'cgi'
|
require 'cgi'
|
||||||
|
|
||||||
|
require './models/album'
|
||||||
|
|
||||||
class MPDClient < Sinatra::Base
|
class MPDClient < Sinatra::Base
|
||||||
register Sinatra::Namespace
|
register Sinatra::Namespace
|
||||||
|
|
||||||
@ -55,9 +58,7 @@ class MPDClient < Sinatra::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def mpd
|
def mpd
|
||||||
@mpd ||= MPD.new
|
MPDConnection.mpd
|
||||||
@mpd.connect unless @mpd.connected?
|
|
||||||
@mpd
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
31
spec/models/album_spec.rb
Normal file
31
spec/models/album_spec.rb
Normal file
@ -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
|
||||||
8
spec/mpd_client_spec.rb
Normal file
8
spec/mpd_client_spec.rb
Normal file
@ -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
|
||||||
20
spec/spec_helper.rb
Normal file
20
spec/spec_helper.rb
Normal file
@ -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
|
||||||
Loading…
Reference in New Issue
Block a user