diff --git a/lib/musicbrainz/bindings/release.rb b/lib/musicbrainz/bindings/release.rb index c83911f..681860a 100644 --- a/lib/musicbrainz/bindings/release.rb +++ b/lib/musicbrainz/bindings/release.rb @@ -3,14 +3,23 @@ module MusicBrainz module Release def parse(xml) xml = xml.xpath('./release') unless xml.xpath('./release').empty? - { + + hash = { id: (xml.attribute('id').value rescue nil), title: (xml.xpath('./title').text rescue nil), status: (xml.xpath('./status').text rescue nil), country: (xml.xpath('./country').text rescue nil), - format: (xml.xpath('./medium-list/medium/format').text rescue nil), date: (xml.xpath('./date').text rescue nil) } + + formats = (xml.xpath('./medium-list/medium/format') rescue []).map(&:text) + + hash[:format] = formats.uniq.map do |format| + format_count = formats.select{|f| f == format}.length + format_count == 1 ? format : "#{format_count}x#{format}" + end.join(' + ') + + hash end extend self diff --git a/spec/bindings/release_spec.rb b/spec/bindings/release_spec.rb new file mode 100644 index 0000000..65941fc --- /dev/null +++ b/spec/bindings/release_spec.rb @@ -0,0 +1,43 @@ +# -*- encoding: utf-8 -*- + +require "spec_helper" + +describe MusicBrainz::Bindings::Release do + describe '.parse' do + describe 'attributes' do + describe 'format' do + context 'single cd' do + it 'returns CD' do + response = 'CD' + xml = Nokogiri::XML.parse(response) + described_class.parse(xml)[:format].should == 'CD' + end + end + + context 'multiple cds' do + it 'returns 2xCD' do + response = 'CDbonus discCD' + xml = Nokogiri::XML.parse(response) + described_class.parse(xml)[:format].should == '2xCD' + end + end + + context 'different formats' do + it 'returns DVD + CD' do + response = 'DVDCD' + xml = Nokogiri::XML.parse(response) + described_class.parse(xml)[:format].should == 'DVD + CD' + end + end + + context 'different formats plus multiple mediums with same format' do + it 'returns 2xCD + DVD' do + response = 'CDCDDVD' + xml = Nokogiri::XML.parse(response) + described_class.parse(xml)[:format].should == '2xCD + DVD' + end + end + end + end + end +end \ No newline at end of file