Improves urls attribute to return an array if there are multiple urls for a relation type. #19
This commit is contained in:
parent
6c2d47544a
commit
b5599710cd
|
@ -24,6 +24,7 @@ require "musicbrainz/models/track"
|
|||
require "musicbrainz/bindings/artist"
|
||||
require "musicbrainz/bindings/artist_search"
|
||||
require "musicbrainz/bindings/artist_release_groups"
|
||||
require "musicbrainz/bindings/relations"
|
||||
require "musicbrainz/bindings/release_group"
|
||||
require "musicbrainz/bindings/release_group_search"
|
||||
require "musicbrainz/bindings/release_group_releases"
|
||||
|
|
|
@ -3,18 +3,18 @@ module MusicBrainz
|
|||
module Bindings
|
||||
module Artist
|
||||
def parse(xml)
|
||||
xml = xml.xpath('./artist') unless xml.xpath('./artist').empty?
|
||||
xml = xml.xpath('./artist')
|
||||
|
||||
return {} if xml.empty?
|
||||
|
||||
{
|
||||
id: (xml.attribute('id').value rescue nil),
|
||||
type: (xml.attribute('type').value rescue nil),
|
||||
name: (xml.xpath('./name').text.gsub(/[`’]/, "'") rescue nil),
|
||||
country: (xml.xpath('./country').text rescue nil),
|
||||
date_begin: (xml.xpath('./life-span/begin').text rescue nil),
|
||||
date_end: (xml.xpath('./life-span/end').text rescue nil),
|
||||
urls: (Hash[xml.xpath('./relation-list[@target-type="url"]/relation').map{ |xml|
|
||||
[xml.attribute('type').value.downcase.split(" ").join("_").to_sym, xml.xpath('./target').text]
|
||||
}] rescue {})
|
||||
}
|
||||
date_end: (xml.xpath('./life-span/end').text rescue nil)
|
||||
}.merge(Relations.parse(xml))
|
||||
end
|
||||
|
||||
extend self
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
# encoding: UTF-8
|
||||
module MusicBrainz
|
||||
module Bindings
|
||||
module Relations
|
||||
def parse(xml)
|
||||
hash = { urls: {} }
|
||||
|
||||
xml.xpath('./relation-list[@target-type="url"]/relation').map do |xml|
|
||||
next unless type = xml.attribute('type')
|
||||
|
||||
type = type.value.downcase.split(" ").join("_").to_sym
|
||||
target = xml.xpath('./target').text
|
||||
|
||||
if hash[:urls][type].nil? then hash[:urls][type] = target
|
||||
elsif hash[:urls][type].is_a?(Array) then hash[:urls][type] << target
|
||||
else hash[:urls][type] = [hash[:urls][type]]; hash[:urls][type] << target
|
||||
end
|
||||
end
|
||||
|
||||
hash
|
||||
end
|
||||
|
||||
extend self
|
||||
end
|
||||
end
|
||||
end
|
|
@ -8,11 +8,8 @@ module MusicBrainz
|
|||
type: (xml.attribute('type').value rescue nil),
|
||||
title: (xml.xpath('./title').text rescue nil),
|
||||
desc: (xml.xpath('./disambiguation').text rescue nil),
|
||||
first_release_date: (xml.xpath('./first-release-date').text rescue nil),
|
||||
urls: (Hash[xml.xpath('./relation-list[@target-type="url"]/relation').map{ |xml|
|
||||
[xml.attribute('type').value.downcase.split(" ").join("_").to_sym, xml.xpath('./target').text]
|
||||
}] rescue {})
|
||||
}
|
||||
first_release_date: (xml.xpath('./first-release-date').text rescue nil)
|
||||
}.merge(Relations.parse(xml))
|
||||
end
|
||||
|
||||
extend self
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
# -*- encoding: utf-8 -*-
|
||||
|
||||
require "spec_helper"
|
||||
|
||||
describe MusicBrainz::Bindings::Relations do
|
||||
describe '.parse' do
|
||||
describe 'attributes' do
|
||||
describe 'urls' do
|
||||
context '1 url for relation type' do
|
||||
it 'returns a string' do
|
||||
xml = Nokogiri::XML.parse(
|
||||
%Q{<artist><relation-list target-type="url">
|
||||
<relation type-id="99429741-f3f6-484b-84f8-23af51991770" type="social network">
|
||||
<target id="4f4068cb-7001-47a3-a2fe-9146eb6b5d16">https://plus.google.com/+Madonna</target>
|
||||
</relation>
|
||||
</relation-list></artist>}
|
||||
)
|
||||
|
||||
described_class.parse(xml.xpath('./artist'))[:urls][:social_network].should == 'https://plus.google.com/+Madonna'
|
||||
end
|
||||
end
|
||||
|
||||
context 'multiple urls for relation types' do
|
||||
it 'returns an array' do
|
||||
xml = Nokogiri::XML.parse(
|
||||
%Q{<artist><relation-list target-type="url">
|
||||
<relation type-id="99429741-f3f6-484b-84f8-23af51991770" type="social network">
|
||||
<target id="4f4068cb-7001-47a3-a2fe-9146eb6b5d16">https://plus.google.com/+Madonna</target>
|
||||
</relation>
|
||||
<relation type-id="99429741-f3f6-484b-84f8-23af51991770" type="social network">
|
||||
<target id="1dc9e14d-ebfb-448c-a005-e3481d320595">https://www.facebook.com/madonna</target>
|
||||
</relation>
|
||||
</relation-list></artist>}
|
||||
)
|
||||
|
||||
described_class.parse(xml.xpath('./artist'))[:urls][:social_network].should == [
|
||||
'https://plus.google.com/+Madonna', 'https://www.facebook.com/madonna'
|
||||
]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue