1
0
Fork 0

Models moved into module, safe loading, v0.2.0

This commit is contained in:
Gregory Eremin 2011-09-13 14:06:52 +04:00
parent dadf0de159
commit 6a2720e637
13 changed files with 177 additions and 146 deletions

View File

@ -1 +1 @@
0.1.0
0.2.0

View File

@ -0,0 +1,26 @@
module MusicBrainz
@@last_query_time = 0
def self.last_query_time
@@last_query_time
end
def self.last_query_time= time
@@last_query_time = time
end
def self.load url
sleep 1.1 - (Time.now.to_f - self.last_query_time) if Time.now.to_f - self.last_query_time < 1.1
response = nil
5.times do
begin
response = open(url, "User-Agent" => "gem musicbrainz (https://github.com/magnolia-fan/musicbrainz) @ " + Socket.gethostname)
rescue => e
p "MusicBrainz: 503"
end
break unless response.nil?
end
self.last_query_time = Time.now.to_f
response
end
end

View File

@ -0,0 +1,31 @@
module MusicBrainz
class Artist
attr_accessor :id, :type, :name, :country, :date_begin, :date_end
@release_groups
def release_groups
if @release_groups.nil? and not self.id.nil?
@release_groups = []
Nokogiri::XML(MusicBrainz.load('http://musicbrainz.org/ws/2/release-group/?artist=' + self.id)).css('release-group').each do |rg|
@release_groups << MusicBrainz::ReleaseGroup.parse_xml(rg)
end
end
@release_groups
end
def self.find mbid
@artist = self.parse_xml(Nokogiri::XML(MusicBrainz.load('http://musicbrainz.org/ws/2/artist/' + mbid)))
end
def self.parse_xml xml
@artist = MusicBrainz::Artist.new
@artist.id = xml.css('artist').attr('id').value
@artist.type = xml.css('artist').attr('type').value
@artist.name = xml.css('artist > name').text
@artist.country = xml.css('artist > country').text || nil
@artist.date_begin = xml.css('artist > life-span > begin').text || nil
@artist.date_end = xml.css('artist > life-span > end').text || nil
@artist
end
end
end

View File

@ -0,0 +1,40 @@
module MusicBrainz
class Release
attr_accessor :id, :title, :status, :date, :country
@tracks
def tracks
if @tracks.nil? and not self.id.nil?
@tracks = []
Nokogiri::XML(MusicBrainz.load('http://musicbrainz.org/ws/2/release/' + self.id + '?inc=recordings')).css('medium-list > medium > track-list > track').each do |r|
@tracks << MusicBrainz::Track.parse_xml(r)
end
end
@tracks
end
def self.find mbid
xml = Nokogiri::XML(MusicBrainz.load('http://musicbrainz.org/ws/2/release/' + mbid)).css('release').first
self.parse_xml(xml) unless xml.nil?
end
def self.parse_xml xml
@release = MusicBrainz::Release.new
@release.id = xml.attr('id')
@release.title = xml.css('title').text
@release.status = xml.css('status').text
date = xml.css('date').text
unless date.nil? or date.empty?
if date.length == 4
date += '-01-01'
elsif date.length == 7
date += '-01'
end
date = Time.parse(date)
end
@release.date = date
@release.country = xml.css('country').text
@release
end
end
end

View File

@ -0,0 +1,39 @@
module MusicBrainz
class ReleaseGroup
attr_accessor :id, :type, :title, :first_release_date
@releases
def releases
if @releases.nil? and not self.id.nil?
@releases = []
Nokogiri::XML(MusicBrainz.load('http://musicbrainz.org/ws/2/release/?release-group=' + self.id)).css('release').each do |r|
@releases << MusicBrainz::Release.parse_xml(r)
end
end
@releases
end
def self.find mbid
xml = Nokogiri::XML(MusicBrainz.load('http://musicbrainz.org/ws/2/release-group/' + mbid)).css('release-group').first
self.parse_xml(xml) unless xml.nil?
end
def self.parse_xml xml
@release_group = MusicBrainz::ReleaseGroup.new
@release_group.id = xml.attr('id')
@release_group.type = xml.attr('type')
@release_group.title = xml.css('title').text
date = xml.css('first-release-date').text
unless date.nil? or date.empty?
if date.length == 4
date += '-01-01'
elsif date.length == 7
date += '-01'
end
date = Time.parse(date)
end
@release_group.first_release_date = date
@release_group
end
end
end

View File

@ -0,0 +1,19 @@
module MusicBrainz
class Track
attr_accessor :position, :recording_id, :title, :length
def self.find mbid
xml = Nokogiri::XML(MusicBrainz.load('http://musicbrainz.org/ws/2/recording/' + mbid))
self.parse_xml(xml) unless xml.nil?
end
def self.parse_xml xml
@track = MusicBrainz::Track.new
@track.position = xml.css('position').text.to_i || nil
@track.recording_id = xml.css('recording').attr('id').value
@track.title = xml.css('recording > title').text
@track.length = xml.css('length').first.text.to_i || 0
@track
end
end
end

View File

@ -1,30 +0,0 @@
class MusicBrainzArtist
attr_accessor :id, :type, :name, :country, :date_begin, :date_end
@release_groups
def release_groups
if @release_groups.nil? and not self.id.nil?
@release_groups = []
Nokogiri::XML(open('http://musicbrainz.org/ws/2/release-group/?artist=' + self.id)).css('release-group').each do |rg|
@release_groups << MusicBrainzReleaseGroup.parse_xml(rg)
end
end
@release_groups
end
def self.find mbid
@artist = self.parse_xml(Nokogiri::XML(open('http://musicbrainz.org/ws/2/artist/' + mbid)))
end
def self.parse_xml xml
@artist = MusicBrainzArtist.new
@artist.id = xml.css('artist').attr('id').value
@artist.type = xml.css('artist').attr('type').value
@artist.name = xml.css('artist > name').text
@artist.country = xml.css('artist > country').text || nil
@artist.date_begin = xml.css('artist > life-span > begin').text || nil
@artist.date_end = xml.css('artist > life-span > end').text || nil
@artist
end
end

View File

@ -1,38 +0,0 @@
class MusicBrainzRelease
attr_accessor :id, :title, :status, :date, :country
@tracks
def tracks
if @tracks.nil? and not self.id.nil?
@tracks = []
Nokogiri::XML(open('http://musicbrainz.org/ws/2/release/' + self.id + '?inc=recordings')).css('medium-list > medium > track-list > track').each do |r|
@tracks << MusicBrainzTrack.parse_xml(r)
end
end
@tracks
end
def self.find mbid
xml = Nokogiri::XML(open('http://musicbrainz.org/ws/2/release/' + mbid)).css('release').first
self.parse_xml(xml) unless xml.nil?
end
def self.parse_xml xml
@release = MusicBrainzRelease.new
@release.id = xml.attr('id')
@release.title = xml.css('title').text
@release.status = xml.css('status').text
date = xml.css('date').text
unless date.nil? or date.empty?
if date.length == 4
date += '-01-01'
elsif date.length == 7
date += '-01'
end
date = Time.parse(date)
end
@release.date = date
@release.country = xml.css('country').text
@release
end
end

View File

@ -1,37 +0,0 @@
class MusicBrainzReleaseGroup
attr_accessor :id, :type, :title, :first_release_date
@releases
def releases
if @releases.nil? and not self.id.nil?
@releases = []
Nokogiri::XML(open('http://musicbrainz.org/ws/2/release/?release-group=' + self.id)).css('release').each do |r|
@releases << MusicBrainzRelease.parse_xml(r)
end
end
@releases
end
def self.find mbid
xml = Nokogiri::XML(open('http://musicbrainz.org/ws/2/release-group/' + mbid)).css('release-group').first
self.parse_xml(xml) unless xml.nil?
end
def self.parse_xml xml
@release_group = MusicBrainzReleaseGroup.new
@release_group.id = xml.attr('id')
@release_group.type = xml.attr('type')
@release_group.title = xml.css('title').text
date = xml.css('first-release-date').text
unless date.nil? or date.empty?
if date.length == 4
date += '-01-01'
elsif date.length == 7
date += '-01'
end
date = Time.parse(date)
end
@release_group.first_release_date = date
@release_group
end
end

View File

@ -1,17 +0,0 @@
class MusicBrainzTrack
attr_accessor :position, :recording_id, :title, :length
def self.find mbid
xml = Nokogiri::XML(open('http://musicbrainz.org/ws/2/recording/' + mbid))
self.parse_xml(xml) unless xml.nil?
end
def self.parse_xml xml
@track = MusicBrainzTrack.new
@track.position = xml.css('position').text.to_i || nil
@track.recording_id = xml.css('recording').attr('id').value
@track.title = xml.css('recording > title').text
@track.length = xml.css('length').first.text.to_i || 0
@track
end
end

View File

@ -1,7 +1,9 @@
require "open-uri"
require "socket"
require "nokogiri"
require "models/music_brainz_artist"
require "models/music_brainz_release_group"
require "models/music_brainz_release"
require "models/music_brainz_track"
require "models/music_brainz"
require "models/music_brainz/artist"
require "models/music_brainz/release_group"
require "models/music_brainz/release"
require "models/music_brainz/track"

View File

@ -5,11 +5,11 @@
Gem::Specification.new do |s|
s.name = %q{musicbrainz}
s.version = "0.1.0"
s.version = "0.2.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = [%q{Gregory Eremin}]
s.date = %q{2011-07-18}
s.date = %q{2011-09-13}
s.description = %q{MusicBrainz Web Service wrapper with ActiveRecord-style models}
s.email = %q{magnolia_fan@me.com}
s.extra_rdoc_files = [
@ -24,10 +24,6 @@ Gem::Specification.new do |s|
"README.rdoc",
"Rakefile",
"VERSION",
"lib/models/music_brainz_artist.rb",
"lib/models/music_brainz_release.rb",
"lib/models/music_brainz_release_group.rb",
"lib/models/music_brainz_track.rb",
"lib/musicbrainz.rb",
"musicbrainz.gemspec",
"test/helper.rb",
@ -36,7 +32,7 @@ Gem::Specification.new do |s|
s.homepage = %q{http://github.com/magnolia-fan/musicbrainz}
s.licenses = [%q{MIT}]
s.require_paths = [%q{lib}]
s.rubygems_version = %q{1.8.5}
s.rubygems_version = %q{1.8.8}
s.summary = %q{MusicBrainz Web Service wrapper}
if s.respond_to? :specification_version then

View File

@ -4,16 +4,16 @@ class TestMusicbrainz < Test::Unit::TestCase
context "artist" do
should "load artist xml" do
assert_nothing_raised(Exception) do
MusicBrainzArtist.find('69b39eab-6577-46a4-a9f5-817839092033')
MusicBrainz::Artist.find('69b39eab-6577-46a4-a9f5-817839092033')
end
end
setup do
@artist = MusicBrainzArtist.find('69b39eab-6577-46a4-a9f5-817839092033')
@artist = MusicBrainz::Artist.find('69b39eab-6577-46a4-a9f5-817839092033')
end
should "return valid instance" do
assert_instance_of(MusicBrainzArtist, @artist)
assert_instance_of(MusicBrainz::Artist, @artist)
end
should "contain correct data" do
@ -37,16 +37,16 @@ class TestMusicbrainz < Test::Unit::TestCase
context "release group" do
should "load release group xml" do
assert_nothing_raised(Exception) do
MusicBrainzReleaseGroup.find('6f33e0f0-cde2-38f9-9aee-2c60af8d1a61')
MusicBrainz::ReleaseGroup.find('6f33e0f0-cde2-38f9-9aee-2c60af8d1a61')
end
end
setup do
@release_group = MusicBrainzReleaseGroup.find('6f33e0f0-cde2-38f9-9aee-2c60af8d1a61')
@release_group = MusicBrainz::ReleaseGroup.find('6f33e0f0-cde2-38f9-9aee-2c60af8d1a61')
end
should "return valid instance" do
assert_instance_of(MusicBrainzReleaseGroup, @release_group)
assert_instance_of(MusicBrainz::ReleaseGroup, @release_group)
end
should "contain correct data" do
@ -70,16 +70,16 @@ class TestMusicbrainz < Test::Unit::TestCase
context "release" do
should "load release xml" do
assert_nothing_raised(Exception) do
MusicBrainzRelease.find('2225dd4c-ae9a-403b-8ea0-9e05014c778f')
MusicBrainz::Release.find('2225dd4c-ae9a-403b-8ea0-9e05014c778f')
end
end
setup do
@release = MusicBrainzRelease.find('2225dd4c-ae9a-403b-8ea0-9e05014c778f')
@release = MusicBrainz::Release.find('2225dd4c-ae9a-403b-8ea0-9e05014c778f')
end
should "return valid instance" do
assert_instance_of(MusicBrainzRelease, @release)
assert_instance_of(MusicBrainz::Release, @release)
end
should "contain correct data" do
@ -103,16 +103,16 @@ class TestMusicbrainz < Test::Unit::TestCase
context "track" do
should "load track xml" do
assert_nothing_raised(Exception) do
MusicBrainzTrack.find('b3015bab-1540-4d4e-9f30-14872a1525f7')
MusicBrainz::Track.find('b3015bab-1540-4d4e-9f30-14872a1525f7')
end
end
setup do
@track = MusicBrainzTrack.find('b3015bab-1540-4d4e-9f30-14872a1525f7')
@track = MusicBrainz::Track.find('b3015bab-1540-4d4e-9f30-14872a1525f7')
end
should "return valid instance" do
assert_instance_of(MusicBrainzTrack, @track)
assert_instance_of(MusicBrainz::Track, @track)
end
should "contain correct data" do