Merge pull request #9 from Applicat/parse_multiple_cd_format_correctly
Consider different cases of release's format attribute.
This commit is contained in:
		
						commit
						7d5bde8616
					
				@ -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
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										43
									
								
								spec/bindings/release_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								spec/bindings/release_spec.rb
									
									
									
									
									
										Normal file
									
								
							@ -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 = '<release><medium-list count="1"><medium><format>CD</format></medium></medium-list></release>'
 | 
			
		||||
            xml = Nokogiri::XML.parse(response)
 | 
			
		||||
            described_class.parse(xml)[:format].should == 'CD'
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
        
 | 
			
		||||
        context 'multiple cds' do
 | 
			
		||||
          it 'returns 2xCD' do
 | 
			
		||||
            response = '<release><medium-list count="2"><medium><format>CD</format><track-list count="11" /></medium><medium><title>bonus disc</title><format>CD</format></medium></medium-list></release>'
 | 
			
		||||
            xml = Nokogiri::XML.parse(response)
 | 
			
		||||
            described_class.parse(xml)[:format].should == '2xCD'
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
        
 | 
			
		||||
        context 'different formats' do
 | 
			
		||||
          it 'returns DVD + CD' do
 | 
			
		||||
            response = '<release><medium-list count="2"><medium><format>DVD</format></medium><medium><format>CD</format></medium></medium-list></release>'
 | 
			
		||||
            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 = '<release><medium-list count="2"><medium><format>CD</format></medium><medium><format>CD</format></medium><medium><format>DVD</format></medium></medium-list></release>'
 | 
			
		||||
            xml = Nokogiri::XML.parse(response)
 | 
			
		||||
            described_class.parse(xml)[:format].should == '2xCD + DVD'
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										32
									
								
								spec/deprecated/cache_config_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								spec/deprecated/cache_config_spec.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,32 @@
 | 
			
		||||
# -*- encoding: utf-8 -*-
 | 
			
		||||
 | 
			
		||||
require "spec_helper"
 | 
			
		||||
 | 
			
		||||
describe MusicBrainz::Deprecated::CacheConfig do
 | 
			
		||||
  before(:all) {
 | 
			
		||||
    @old_cache_path = MusicBrainz.config.cache_path
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  before(:each) {
 | 
			
		||||
    MusicBrainz.config.cache_path = nil
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  after(:all) {
 | 
			
		||||
    MusicBrainz.config.cache_path = @old_cache_path
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  it "allows deprecated use of cache_path" do
 | 
			
		||||
    MusicBrainz.config.cache_path = "test1"
 | 
			
		||||
 | 
			
		||||
    MusicBrainz::Tools::Cache.cache_path.should == "test1"
 | 
			
		||||
    MusicBrainz.cache_path.should == "test1"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  it "allows deprecated use of cache_path=" do
 | 
			
		||||
    MusicBrainz::Tools::Cache.cache_path = "test2"
 | 
			
		||||
    MusicBrainz.config.cache_path.should == "test2"
 | 
			
		||||
 | 
			
		||||
    MusicBrainz.cache_path = "test3"
 | 
			
		||||
    MusicBrainz.config.cache_path.should == "test3"
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
@ -2,37 +2,19 @@
 | 
			
		||||
 | 
			
		||||
require "spec_helper"
 | 
			
		||||
 | 
			
		||||
describe MusicBrainz do
 | 
			
		||||
describe MusicBrainz::Deprecated::ProxyConfig do
 | 
			
		||||
  before(:all) {
 | 
			
		||||
    @old_cache_path = MusicBrainz.config.cache_path
 | 
			
		||||
    @old_query_interval = MusicBrainz.config.query_interval
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  before(:each) {
 | 
			
		||||
    MusicBrainz.config.cache_path = nil
 | 
			
		||||
    MusicBrainz.config.query_interval = nil
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  after(:all) {
 | 
			
		||||
    MusicBrainz.config.cache_path = @old_cache_path
 | 
			
		||||
    MusicBrainz.config.query_interval = @old_query_interval
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  it "allows deprecated use of cache_path" do
 | 
			
		||||
    MusicBrainz.config.cache_path = "test1"
 | 
			
		||||
 | 
			
		||||
    MusicBrainz::Tools::Cache.cache_path.should == "test1"
 | 
			
		||||
    MusicBrainz.cache_path.should == "test1"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  it "allows deprecated use of cache_path=" do
 | 
			
		||||
    MusicBrainz::Tools::Cache.cache_path = "test2"
 | 
			
		||||
    MusicBrainz.config.cache_path.should == "test2"
 | 
			
		||||
 | 
			
		||||
    MusicBrainz.cache_path = "test3"
 | 
			
		||||
    MusicBrainz.config.cache_path.should == "test3"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  it "allows deprecated use of query_interval" do
 | 
			
		||||
    MusicBrainz.config.query_interval = 2
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user