Refactoring Artist model
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
module MusicBrainz
|
||||
module Parsers
|
||||
class Artist < Base
|
||||
class << self
|
||||
def model(xml)
|
||||
res = {
|
||||
:id => safe_get_attr(xml, "artist", "id"),
|
||||
:type => safe_get_attr(xml, "artist", "type"),
|
||||
:name => safe_get_value(xml, "artist > name").gsub(/[`’]/, "'"),
|
||||
:country => safe_get_value(xml, "artist > country"),
|
||||
:date_begin => safe_get_value(xml, "artist > life-span > begin"),
|
||||
:date_end => safe_get_value(xml, "artist > life-span > end"),
|
||||
:urls => {}
|
||||
}
|
||||
xml.css("relation-list[target-type='url'] > relation").each { |rel|
|
||||
res[:urls][rel.attr("type").downcase.split(" ").join("_").to_sym] = rel.css("target").text
|
||||
}
|
||||
res
|
||||
end
|
||||
|
||||
def search(xml)
|
||||
artists = []
|
||||
xml.css("artist-list > artist").each do |a|
|
||||
artists << {
|
||||
:name => a.first_element_child.text.gsub(/[`’]/, "'"),
|
||||
:sort_name => self.safe_get_value(a, "sort-name").gsub(/[`’]/, "'"),
|
||||
:weight => 0,
|
||||
:desc => self.safe_get_value(a, "disambiguation"),
|
||||
:type => self.safe_get_attr(a, nil, "type"),
|
||||
:mbid => self.safe_get_attr(a, nil, "id"),
|
||||
:aliases => a.css("alias-list > alias").map { |item| item.text }
|
||||
}
|
||||
end
|
||||
artists
|
||||
end
|
||||
|
||||
def release_groups(xml)
|
||||
release_groups = []
|
||||
xml.css("release-group").each do |rg|
|
||||
release_groups << MusicBrainz::Parsers::ReleaseGroup.model(rg)
|
||||
end
|
||||
release_groups
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,30 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
module MusicBrainz
|
||||
module Parsers
|
||||
class << self
|
||||
def get_by_name(name)
|
||||
case name
|
||||
when :artist_model
|
||||
return { :const => MusicBrainz::Parsers::Artist, :method => :model }
|
||||
when :artist_search
|
||||
return { :const => MusicBrainz::Parsers::Artist, :method => :search }
|
||||
when :artist_release_groups
|
||||
return { :const => MusicBrainz::Parsers::Artist, :method => :release_groups }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Base
|
||||
class << self
|
||||
def safe_get_attr(xml, path, name)
|
||||
node = path.nil? ? xml : (xml.css(path).first unless xml.css(path).empty?)
|
||||
node.attr(name) unless node.nil? or node.attr(name).nil?
|
||||
end
|
||||
|
||||
def safe_get_value(xml, path)
|
||||
xml.css(path).first.text unless xml.css(path).empty?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,19 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
module MusicBrainz
|
||||
module Parsers
|
||||
class ReleaseGroup < Base
|
||||
class << self
|
||||
def model(xml)
|
||||
{
|
||||
:id => safe_get_attr(xml, nil, "id"),
|
||||
:type => safe_get_attr(xml, nil, "type"),
|
||||
:title => safe_get_value(xml, "title"),
|
||||
:disambiguation => safe_get_value(xml, "disambiguation"),
|
||||
:first_release_date => safe_get_value(xml, "first-release-date")
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user