diff --git a/lib/musicbrainz/models/base_model.rb b/lib/musicbrainz/models/base_model.rb index d11841e..f7af924 100644 --- a/lib/musicbrainz/models/base_model.rb +++ b/lib/musicbrainz/models/base_model.rb @@ -24,11 +24,15 @@ module MusicBrainz MusicBrainz.client end - def search(hash) + def search(hash, resource=nil) hash = escape_strings(hash) query_val = build_query(hash) underscore_name = self.name[13..-1].underscore - client.load(underscore_name.to_sym, { query: query_val, limit: 10 }, { binding: underscore_name.insert(-1,"_search").to_sym }) + if resource # only needed since "track" is really a "recording", ugly + client.load(resource, { query: query_val, limit: 10 }, { binding: underscore_name.insert(-1,"_search").to_sym }) + else + client.load(underscore_name.to_sym, { query: query_val, limit: 10 }, { binding: underscore_name.insert(-1,"_search").to_sym }) + end end class ::String diff --git a/lib/musicbrainz/models/track.rb b/lib/musicbrainz/models/track.rb index 4e9203c..8390abf 100644 --- a/lib/musicbrainz/models/track.rb +++ b/lib/musicbrainz/models/track.rb @@ -14,7 +14,8 @@ module MusicBrainz end def search(artist_name, track_name) - super({artist: artist_name, recording: track_name}) + # this model really should be named "recording" I'd rename, but I don't want to break anything + super({recording: track_name, artist: artist_name}, "recording") end end end diff --git a/spec/models/release_group_spec.rb b/spec/models/release_group_spec.rb index 8e292ab..8d7c3d9 100644 --- a/spec/models/release_group_spec.rb +++ b/spec/models/release_group_spec.rb @@ -61,7 +61,6 @@ describe MusicBrainz::ReleaseGroup do allow_any_instance_of(MusicBrainz::Client).to receive(:get_contents) .with('http://musicbrainz.org/ws/2/release-group/6f33e0f0-cde2-38f9-9aee-2c60af8d1a61?inc=url-rels') .and_return({ status: 200, body: response}) - release_group = MusicBrainz::ReleaseGroup.find_by_artist_and_title('Kasabian', 'Empire') expect(release_group.id).to eq '6f33e0f0-cde2-38f9-9aee-2c60af8d1a61' end diff --git a/spec/models/track_spec.rb b/spec/models/track_spec.rb index 764be74..acefb8e 100644 --- a/spec/models/track_spec.rb +++ b/spec/models/track_spec.rb @@ -3,21 +3,32 @@ require "spec_helper" describe MusicBrainz::Track do - it "gets no exception while loading release info" do - expect { - MusicBrainz::Track.find("b3015bab-1540-4d4e-9f30-14872a1525f7") - }.to_not raise_error(Exception) - end + describe '.find' do + it "gets no exception while loading release info" do + lambda { + MusicBrainz::Track.find("b3015bab-1540-4d4e-9f30-14872a1525f7") + }.should_not raise_error(Exception) + end - it "gets correct instance" do - track = MusicBrainz::Track.find("b3015bab-1540-4d4e-9f30-14872a1525f7") - expect(track).to be_an_instance_of(MusicBrainz::Track) - end + it "gets correct instance" do + track = MusicBrainz::Track.find("b3015bab-1540-4d4e-9f30-14872a1525f7") + track.should be_an_instance_of(MusicBrainz::Track) + end - it "gets correct track data" do - track = MusicBrainz::Track.find("b3015bab-1540-4d4e-9f30-14872a1525f7") - expect(track.recording_id).to eq "b3015bab-1540-4d4e-9f30-14872a1525f7" - expect(track.title).to eq "Empire" - expect(track.length).to eq 233013 - end + it "gets correct track data" do + track = MusicBrainz::Track.find("b3015bab-1540-4d4e-9f30-14872a1525f7") + track.recording_id.should == "b3015bab-1540-4d4e-9f30-14872a1525f7" + track.title.should == "Empire" + track.length.should == 233013 + end + end + + describe '.search' do + it "searches tracks (aka recordings) by artist name and title" do + matches = MusicBrainz::Track.search('Local H', 'Bound for the floor') + matches.length.should be > 0 + matches.first[:title].should == 'Empire' + matches.first[:type].should == 'Album' + end + end end