1
0
Fork 0

Merge pull request #12 from Applicat/release_group_search2

Implementation of release group search
This commit is contained in:
Gregory Eremin 2013-01-22 21:55:35 -08:00
commit abd67818d3
6 changed files with 113 additions and 24 deletions

View File

@ -80,6 +80,9 @@ MusicBrainz::ReleaseGroup
```ruby
# Class Methods
MusicBrainz::ReleaseGroup.find(id)
MusicBrainz::ReleaseGroup.find_by_artist_and_title(artist_name, title, type: 'Album')
MusicBrainz::ReleaseGroup.search(artist_name, title)
MusicBrainz::ReleaseGroup.search(artist_name, title, type: 'Album')
# Instance Methods
@release_group.releases

View File

@ -24,6 +24,7 @@ require "musicbrainz/bindings/artist"
require "musicbrainz/bindings/artist_search"
require "musicbrainz/bindings/artist_release_groups"
require "musicbrainz/bindings/release_group"
require "musicbrainz/bindings/release_group_search"
require "musicbrainz/bindings/release_group_releases"
require "musicbrainz/bindings/release"
require "musicbrainz/bindings/release_tracks"

View File

@ -0,0 +1,20 @@
# encoding: UTF-8
module MusicBrainz
module Bindings
module ReleaseGroupSearch
def parse(xml)
xml.xpath('./release-group-list/release-group').map do |xml|
{
id: (xml.attribute('id').value rescue nil),
mbid: (xml.attribute('id').value rescue nil), # Old shit
title: (xml.xpath('./title').text.gsub(/[`]/, "'") rescue nil),
type: (xml.attribute('type').value rescue nil),
score: (xml.attribute('score').value.to_i rescue nil)
} rescue nil
end.delete_if{ |item| item.nil? }
end
extend self
end
end
end

View File

@ -23,6 +23,23 @@ module MusicBrainz
create_model: :release_group
})
end
def search(artist_name, title, options = {})
artist_name = CGI.escape(artist_name).gsub(/\!/, '\!')
title = CGI.escape(title).gsub(/\!/, '\!')
query = ["artist:\"#{artist_name}\"", "releasegroup:\"#{title}\""]
query << "type: #{options[:type]}" if options[:type]
client.load(
:release_group, { query: query.join(' AND '), limit: 10 },
{ binding: :release_group_search }
)
end
def find_by_artist_and_title(artist_name, title, options = {})
matches = search(artist_name, title, options)
matches.empty? ? nil : find(matches.first[:id])
end
end
end
end

View File

@ -0,0 +1,17 @@
# -*- encoding: utf-8 -*-
require "spec_helper"
describe MusicBrainz::Bindings::ReleaseGroupSearch do
describe '.parse' do
it "gets correct release group data" do
response = '<release-group-list><release-group id="246bc928-2dc8-35ba-80ee-7a0079de1632" type="Single" ext:score="100"><title>Empire</title></release-group>'
described_class.parse(Nokogiri::XML.parse(response)).should == [
{
id: '246bc928-2dc8-35ba-80ee-7a0079de1632', mbid: '246bc928-2dc8-35ba-80ee-7a0079de1632',
title: 'Empire', type: 'Single', score: 100
}
]
end
end
end

View File

@ -3,6 +3,7 @@
require "spec_helper"
describe MusicBrainz::ReleaseGroup do
describe '.find' do
it "gets no exception while loading release group info" do
lambda {
MusicBrainz::ReleaseGroup.find("6f33e0f0-cde2-38f9-9aee-2c60af8d1a61")
@ -21,7 +22,36 @@ describe MusicBrainz::ReleaseGroup do
release_group.title.should == "Empire"
release_group.first_release_date.should == Date.new(2006, 8, 28)
end
end
describe '.search' do
context 'without type filter' do
it "searches release group by artist name and title" do
matches = MusicBrainz::ReleaseGroup.search('Kasabian', 'Empire')
matches.length.should be > 0
matches.first[:title].should == 'Empire'
matches.first[:type].should == 'Single'
end
end
context 'with type filter' do
it "searches release group by artist name and title" do
matches = MusicBrainz::ReleaseGroup.search('Kasabian', 'Empire', type: 'Album')
matches.length.should be > 0
matches.first[:title].should == 'Empire'
matches.first[:type].should == 'Album'
end
end
end
describe '.find_by_artist_and_title' do
it "gets first release group by artist name and title" do
release_group = MusicBrainz::ReleaseGroup.find_by_artist_and_title('Kasabian', 'Empire')
release_group.id.should == '246bc928-2dc8-35ba-80ee-7a0079de1632'
end
end
describe '#releases' do
it "gets correct release group's releases" do
releases = MusicBrainz::ReleaseGroup.find("6f33e0f0-cde2-38f9-9aee-2c60af8d1a61").releases
releases.length.should be >= 5
@ -31,4 +61,5 @@ describe MusicBrainz::ReleaseGroup do
releases.first.date.should == Date.new(2006, 8, 28)
releases.first.country.should == "GB"
end
end
end