Skip to content
Browse files

started writing tests, added mpd library and sample music, moved arou…

…nd configs a bit
  • Loading branch information...
1 parent d4888b1 commit d377be9a364e1422c3f8bcc7693866458dc228ea Jonathan Hoyt committed Jan 19, 2010
View
45 app/models/player.rb
@@ -0,0 +1,45 @@
+require 'librmpd'
+
+class Player
+
+ def self.play
+ with_mpd do |mpd|
+ mpd.play
+ end
+ end
+
+ def self.next_song
+ with_mpd do |mpd|
+ mpd.next
+ end
+ end
+
+ def self.previous_song
+ with_mpd do |mpd|
+ mpd.previous
+ end
+ end
+
+ def self.stop
+ with_mpd do |mpd|
+ mpd.stop
+ end
+ end
+
+ def self.clear
+ with_mpd do |mpd|
+ mpd.clear
+ end
+ end
+
+ def self.with_mpd(&block)
+ mpd = MPD.new 'localhost', APP_CONFIG[:mpd_port]
+ mpd.connect
+ if mpd.connected?
+ yield(mpd)
+ end
+ ensure
+ mpd.disconnect
+ end
+
+end
View
21 app/models/playlist.rb
@@ -1,5 +1,24 @@
+require 'librmpd'
+
class Playlist
include MongoMapper::Document
key :name, String, :require => true, :unique => true
- key :songs, Array
+ key :songs, Array
+
+ def load
+ with_mpd do |mpd|
+ songs.each { |id| mpd.add "http://localhost:3333/gridfs/#{Song.find(id).mp3_path}" }
+ end
+ end
+
+ def with_mpd(&block)
+ mpd = MPD.new 'localhost', APP_CONFIG[:mpd_port]
+ mpd.connect
+ if mpd.connected?
+ yield(mpd)
+ end
+ ensure
+ mpd.disconnect
+ end
+
end
View
16 app/models/song.rb
@@ -40,16 +40,16 @@ def self.read_id3_tags(file_path)
tags = {}
begin
Mp3Info.open(file_path) do |song|
- tags["title"] = song.tag.title.blank? ? song_file_name : song.tag.title
- tags["artist"] = song.tag.artist
- tags["duration"] = song.length
- tags["album"] = song.tag.album
+ tags["title"] = song.tag.title.blank? ? file_path.split('/')[-1] : song.tag.title
+ tags["artist"] = song.tag.artist.blank? ? nil : song.tag.artist
+ tags["duration"] = song.length.blank? ? nil : song.length
+ tags["album"] = song.tag.album.blank? ? nil : song.tag.album
if song.tag.genre && (1..125).include?(song.tag.genre.to_i)
tags["genre"] = GENRES[song.tag.genre.to_i]
elsif song.tag.genre_s && (1..125).include?(song.tag.genre_s.gsub(/\D/,'').to_i)
tags["genre"] = GENRES[song.tag.genre_s.gsub(/\D/,'').to_i]
else
- self.genre = ""
+ tags["genre"] = nil
end
end
rescue
@@ -84,9 +84,9 @@ def self.read_id3_tags(file_path)
'Punk Rock', 'Drum Solo', 'Acapella', 'Euro-House', 'Dance Hall']
def duration_converted
- minutes = (duration/60).to_i
- seconds = (duration - minutes*60).to_i
- return "#{minutes}:#{seconds}"
+ total_minutes = duration / 1.minutes
+ seconds_in_last_minute = duration - total_minutes.minutes.seconds
+ return "#{total_minutes}:#{seconds_in_last_minute}"
end
end
View
9 config/environment.rb
@@ -21,6 +21,7 @@
# config.gem "aws-s3", :lib => "aws/s3"
config.gem 'mongo_mapper', :version => '>= 0.6.1'
config.gem 'ruby-mp3info', :lib => 'mp3info'
+ config.gem 'librmpd', :lib => 'librmpd'
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named
@@ -40,10 +41,4 @@
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
# config.i18n.default_locale = :de
-end
-
-# mongomapper connection
-require 'mongo/gridfs'
-MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, :logger => Rails.logger, :slave_ok => true)
-MongoMapper.database = "jukeman-#{RAILS_ENV}"
-MongoMapper.ensure_indexes!
+end
View
4 config/environments/test.rb
@@ -25,4 +25,6 @@
# Use SQL instead of Active Record's schema dumper when creating the test database.
# This is necessary if your schema can't be completely dumped by the schema dumper,
# like if you have constraints or database-specific column types
-# config.active_record.schema_format = :sql
+# config.active_record.schema_format = :sql
+config.gem "thoughtbot-shoulda", :lib => "shoulda"
+config.gem "thoughtbot-factory_girl", :lib => "factory_girl"
View
8 config/initializers/mongomapper.rb
@@ -0,0 +1,8 @@
+# mongomapper connection
+require 'mongo/gridfs'
+MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, :logger => Rails.logger, :slave_ok => true)
+MongoMapper.database = "jukeman-#{RAILS_ENV}"
+Dir[Rails.root + 'app/models/**/*.rb'].each do |model_path|
+ File.basename(model_path, '.rb').classify.constantize
+end
+MongoMapper.ensure_indexes!
View
BIN music/Caught Up.mp3
Binary file not shown.
View
BIN music/song.mp3
Binary file not shown.
View
46 test/test_helper.rb
@@ -1,38 +1,20 @@
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'test_help'
+require 'librmpd'
+require 'mpdserver'
class ActiveSupport::TestCase
- # Transactional fixtures accelerate your tests by wrapping each test method
- # in a transaction that's rolled back on completion. This ensures that the
- # test database remains unchanged so your fixtures don't have to be reloaded
- # between every test method. Fewer database queries means faster tests.
- #
- # Read Mike Clark's excellent walkthrough at
- # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
- #
- # Every Active Record database supports transactions except MyISAM tables
- # in MySQL. Turn off transactional fixtures in this case; however, if you
- # don't care one way or the other, switching from MyISAM to InnoDB tables
- # is recommended.
- #
- # The only drawback to using transactional fixtures is when you actually
- # need to test transactions. Since your test is bracketed by a transaction,
- # any transactions started in your code will be automatically rolled back.
- self.use_transactional_fixtures = true
-
- # Instantiated fixtures are slow, but give you @david where otherwise you
- # would need people(:david). If you don't want to migrate your existing
- # test cases which use the @david style and don't mind the speed hit (each
- # instantiated fixtures translates to a database query per test method),
- # then set this back to true.
- self.use_instantiated_fixtures = false
-
- # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
- #
- # Note: You'll currently still have to declare fixtures explicitly in integration tests
- # -- they do not yet inherit this setting
- fixtures :all
-
- # Add more helper methods to be used by all tests here...
+ def setup
+ # clear database
+ MongoMapper.database.collections.map(&:remove)
+ # setup test mpd server
+ @server = MPDTestServer.new APP_CONFIG[:mpd_port]
+ @server.start
+ @server.audit = true
+ end
+
+ def teardown
+ @server.stop
+ end
end
View
29 test/unit/player_test.rb
@@ -0,0 +1,29 @@
+require File.dirname(__FILE__) + '/../test_helper'
+require 'librmpd'
+require 'mpdserver'
+
+class PlayerTest < ActiveSupport::TestCase
+ context "when controlling mpd it" do
+
+ should "play" do
+ assert Player.play
+ end
+
+ should "next_song" do
+ assert Player.next_song
+ end
+
+ should "previous_song" do
+ assert Player.previous_song
+ end
+
+ should "stop" do
+ assert Player.stop
+ end
+
+ should "clear" do
+ assert Player.clear
+ end
+ end
+
+end
View
33 test/unit/playlist_test.rb
@@ -0,0 +1,33 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+
+class PlaylistTest < ActiveSupport::TestCase
+ context "playlists" do
+ setup do
+ Song.import_from_folder
+ end
+
+ should "store song ids" do
+ playlist = Playlist.new(:name => "Test Playlist")
+ playlist.songs = Song.all.collect { |s| s.id }
+ playlist.save
+ assert_equal 2, playlist.songs.length
+ end
+ end
+
+ # context "when loading a playlist it" do
+ # def setup
+ # Song.import_from_folder
+ # @playlist = Playlist.new(:name => "Test Playlist")
+ # @playlist.songs = Song.all.collect { |s| s.id }
+ # @playlist.save
+ # end
+ #
+ # should "load the playlist to mpd" do
+ # @playlist.load
+ # assert @mpd.playlist
+ # end
+ # end
+
+
+end
View
40 test/unit/song_test.rb
@@ -0,0 +1,40 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class SongTest < ActiveSupport::TestCase
+ context "during song creation it" do
+ should "read the id3 tags" do
+ tags = Song.read_id3_tags("#{RAILS_ROOT}/music/song.mp3")
+ assert_equal 15, tags["duration"].to_i
+ assert_equal "Idol, Billy", tags["artist"]
+ assert_equal "White Wedding", tags["title"]
+ assert_nil tags["album"]
+ assert_equal "Rock", tags["genre"]
+ end
+
+ should "fail gracefully if id3 tags aren't present" do
+ tags = Song.read_id3_tags("#{RAILS_ROOT}/music/Caught Up.mp3")
+ assert_equal 15, tags["duration"].to_i
+ assert_nil tags["artist"]
+ assert_equal "Caught Up.mp3", tags["title"]
+ assert_nil tags["album"]
+ assert_nil tags["genre"]
+ end
+
+ should "create new song in database" do
+ assert Song.import_song("#{RAILS_ROOT}/music/song.mp3")
+ assert_equal Song.count, 1
+ end
+ end
+
+ context "when importing from a folder" do
+ setup do
+ Song.import_from_folder
+ end
+
+ should "import all songs in the folder" do
+ assert_equal Song.count, 2
+ end
+ end
+
+
+end

0 comments on commit d377be9

Please sign in to comment.
Something went wrong with that request. Please try again.