Skip to content

Commit

Permalink
track_search now works, will be moved to it's own recording model.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwolfe authored and diegodurs committed May 12, 2015
1 parent 426fed8 commit 813b6f0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
8 changes: 6 additions & 2 deletions lib/musicbrainz/models/base_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ def client
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
Expand Down
3 changes: 2 additions & 1 deletion lib/musicbrainz/models/track.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def find(id)
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
Expand Down
1 change: 0 additions & 1 deletion spec/models/release_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
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
Expand Down
41 changes: 26 additions & 15 deletions spec/models/track_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 813b6f0

Please sign in to comment.