diff --git a/lib/musicbrainz.rb b/lib/musicbrainz.rb index bb6cde0..9a01624 100644 --- a/lib/musicbrainz.rb +++ b/lib/musicbrainz.rb @@ -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" diff --git a/lib/musicbrainz/bindings/artist.rb b/lib/musicbrainz/bindings/artist.rb index df82b75..aeb97f0 100644 --- a/lib/musicbrainz/bindings/artist.rb +++ b/lib/musicbrainz/bindings/artist.rb @@ -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 diff --git a/lib/musicbrainz/bindings/relations.rb b/lib/musicbrainz/bindings/relations.rb new file mode 100644 index 0000000..9f45576 --- /dev/null +++ b/lib/musicbrainz/bindings/relations.rb @@ -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 \ No newline at end of file diff --git a/lib/musicbrainz/bindings/release_group.rb b/lib/musicbrainz/bindings/release_group.rb index 7181d9b..7ac95d4 100644 --- a/lib/musicbrainz/bindings/release_group.rb +++ b/lib/musicbrainz/bindings/release_group.rb @@ -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 diff --git a/spec/bindings/relations_spec.rb b/spec/bindings/relations_spec.rb new file mode 100644 index 0000000..d187aa5 --- /dev/null +++ b/spec/bindings/relations_spec.rb @@ -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{ + + https://plus.google.com/+Madonna + + } + ) + + 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{ + + https://plus.google.com/+Madonna + + + https://www.facebook.com/madonna + + } + ) + + 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 \ No newline at end of file