1
0
Fork 0

Improves urls attribute to return an array if there are multiple urls for a relation type. #19

This commit is contained in:
Mathias Gawlista 2013-06-14 12:33:35 +02:00
parent 6c2d47544a
commit b5599710cd
5 changed files with 79 additions and 11 deletions

View File

@ -24,6 +24,7 @@ require "musicbrainz/models/track"
require "musicbrainz/bindings/artist" require "musicbrainz/bindings/artist"
require "musicbrainz/bindings/artist_search" require "musicbrainz/bindings/artist_search"
require "musicbrainz/bindings/artist_release_groups" require "musicbrainz/bindings/artist_release_groups"
require "musicbrainz/bindings/relations"
require "musicbrainz/bindings/release_group" require "musicbrainz/bindings/release_group"
require "musicbrainz/bindings/release_group_search" require "musicbrainz/bindings/release_group_search"
require "musicbrainz/bindings/release_group_releases" require "musicbrainz/bindings/release_group_releases"

View File

@ -3,18 +3,18 @@ module MusicBrainz
module Bindings module Bindings
module Artist module Artist
def parse(xml) 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), id: (xml.attribute('id').value rescue nil),
type: (xml.attribute('type').value rescue nil), type: (xml.attribute('type').value rescue nil),
name: (xml.xpath('./name').text.gsub(/[`]/, "'") rescue nil), name: (xml.xpath('./name').text.gsub(/[`]/, "'") rescue nil),
country: (xml.xpath('./country').text rescue nil), country: (xml.xpath('./country').text rescue nil),
date_begin: (xml.xpath('./life-span/begin').text rescue nil), date_begin: (xml.xpath('./life-span/begin').text rescue nil),
date_end: (xml.xpath('./life-span/end').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| }.merge(Relations.parse(xml))
[xml.attribute('type').value.downcase.split(" ").join("_").to_sym, xml.xpath('./target').text]
}] rescue {})
}
end end
extend self extend self

View File

@ -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

View File

@ -8,11 +8,8 @@ module MusicBrainz
type: (xml.attribute('type').value rescue nil), type: (xml.attribute('type').value rescue nil),
title: (xml.xpath('./title').text rescue nil), title: (xml.xpath('./title').text rescue nil),
desc: (xml.xpath('./disambiguation').text rescue nil), desc: (xml.xpath('./disambiguation').text rescue nil),
first_release_date: (xml.xpath('./first-release-date').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| }.merge(Relations.parse(xml))
[xml.attribute('type').value.downcase.split(" ").join("_").to_sym, xml.xpath('./target').text]
}] rescue {})
}
end end
extend self extend self

View File

@ -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