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
+1
View File
@@ -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"
+6 -6
View File
@@ -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
+26
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
+2 -5
View File
@@ -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