1
0
Fork 0

Gem is rewrapped with Bundler instead of Jeweler. All tests are using RSpec. Reorganized lib structure. Tests are fixed.

This commit is contained in:
Gregory Eremin 2012-07-04 17:10:47 +04:00
parent 2ed4c869cb
commit 9121e7927e
34 changed files with 486 additions and 649 deletions

View File

@ -1,5 +0,0 @@
lib/**/*.rb
bin/*
-
features/**/*.feature
LICENSE.txt

3
.gitignore vendored
View File

@ -4,6 +4,5 @@ doc
.yardoc
.bundle
pkg
.DS_Store
tmp
Gemfile.lock
Gemfile.lock

1
.rspec Normal file
View File

@ -0,0 +1 @@
--color

View File

@ -1,5 +1,2 @@
source "http://rubygems.org"
source :rubygems
gemspec
gem 'simplecov', :require => false, :group => :test

View File

@ -1,4 +1,6 @@
Copyright (c) 2011 magnolia-fan
Copyright (c) 2012 Gregory Eremin
MIT License
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@ -17,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

86
README.md Normal file
View File

@ -0,0 +1,86 @@
## MusicBrainz Web Service wrapper [![Travis CI](https://secure.travis-ci.org/magnolia-fan/musicbrainz.png)](http://travis-ci.org/magnolia-fan/musicbrainz)
### Installation
```bash
gem install musicbrainz
```
### Usage
```ruby
require 'musicbrainz'
# Search for artists
@suggestions = MusicBrainz::Artist.search('Jet')
# Find artist by name or mbid
@foo_fighters = MusicBrainz::Artist.find_by_name('Foo Fighters')
@kasabian = MusicBrainz::Artist.find('69b39eab-6577-46a4-a9f5-817839092033')
# Use them like ActiveRecord models
@empire_tracks = @kasabian.release_groups[8].releases.first.tracks
# Setting a cache path enables caching of requests
MusicBrainz::Tools::Cache.cache_path = "tmp/cache"
# Configuring request interval
MusicBrainz::Tools::Proxy.query_interval = 1.2 # seconds
```
### Api
MusicBrainz::Artist
```ruby
@artists = MusicBrainz::Artist.search(query)
@artist = MusicBrainz::Artist.find_by_name(name)
@artist = MusicBrainz::Artist.find(mbid)
@artist.id
@artist.type
@artist.name
@artist.country
@artist.date_begin
@artist.date_end
@artist.release_groups
```
MusicBrainz::ReleaseGroup
```ruby
@release_group = MusicBrainz::ReleaseGroup.find(mbid)
@release_group.id
@release_group.type
@release_group.title
@release_group.first_release_date
@release_group.releases
```
MusicBrainz::Release
```ruby
@release = MusicBrainz::Release.find(mbid)
@release.id
@release.title
@release.status
@release.date
@release.country
@release.tracks
```
MusicBrainz::Track
```ruby
@track = MusicBrainz::Track.find(mbid)
@track.position
@track.recording_id
@track.title
@track.length
```
### Contributing
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
* Fork the project
* Start a feature/bugfix branch
* Commit and push until you are happy with your contribution
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
### Copyright
Copyright (c) 2011 Gregory Eremin. See [LICENSE](https://raw.github.com/magnolia-fan/musicbrainz/master/LICENSE) for further details.

View File

@ -1,80 +0,0 @@
= MusicBrainz Web Service wrapper {<img src="https://secure.travis-ci.org/magnolia-fan/musicbrainz.png"/>}[http://travis-ci.org/magnolia-fan/musicbrainz]
=== Installation
gem install musicbrainz
=== Usage
require 'musicbrainz'
# Search for artists
@suggestions = MusicBrainz::Artist.search('Jet')
# Find artist by name or mbid
@foo_fighters = MusicBrainz::Artist.find_by_name('Foo Fighters')
@kasabian = MusicBrainz::Artist.find('69b39eab-6577-46a4-a9f5-817839092033')
# Use them like ActiveRecord models
@empire_tracks = @kasabian.release_groups[8].releases.first.tracks
# Setting a cache path enables caching of requests
MusicBrainz.cache_path = File.join(File.dirname(__FILE__), '../../tmp/test/musicbrainz')
=== Api
MusicBrainz::Artist
@artists = MusicBrainz::Artist.search(query)
@artist = MusicBrainz::Artist.find_by_name(name)
@artist = MusicBrainz::Artist.find(mbid)
@artist.id
@artist.type
@artist.name
@artist.country
@artist.date_begin
@artist.date_end
@artist.release_groups
MusicBrainz::ReleaseGroup
@release_group = MusicBrainz::ReleaseGroup.find(mbid)
@release_group.id
@release_group.type
@release_group.title
@release_group.first_release_date
@release_group.releases
MusicBrainz::Release
@release = MusicBrainz::Release.find(mbid)
@release.id
@release.title
@release.status
@release.date
@release.country
@release.tracks
MusicBrainz::Track
@track = MusicBrainz::Track.find(mbid)
@track.position
@track.recording_id
@track.title
@track.length
=== Contributing
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
* Fork the project
* Start a feature/bugfix branch
* Commit and push until you are happy with your contribution
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
* to get a coverage report for your Test Unit and RSpec tests use the rake tasks: test_coverage, rspec_coverage
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
=== Copyright
Copyright (c) 2011 Gregory Eremin. See LICENSE.txt for further details.

View File

@ -1,62 +1,8 @@
# encoding: utf-8
#!/usr/bin/env rake
require "bundler/gem_tasks"
require "rspec/core/rake_task"
require 'rubygems'
require 'bundler'
begin
Bundler.setup(:default, :development)
rescue Bundler::BundlerError => e
$stderr.puts e.message
$stderr.puts "Run `bundle install` to install missing gems"
exit e.status_code
end
require 'rake'
RSpec::Core::RakeTask.new("spec")
require 'jeweler'
Jeweler::Tasks.new do |gem|
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
gem.name = "musicbrainz"
gem.homepage = "http://github.com/magnolia-fan/musicbrainz"
gem.license = "MIT"
gem.summary = %Q{MusicBrainz Web Service wrapper}
gem.description = %Q{MusicBrainz Web Service wrapper with ActiveRecord-style models}
gem.email = "magnolia_fan@me.com"
gem.authors = ["Gregory Eremin"]
# dependencies defined in Gemfile
end
Jeweler::RubygemsDotOrgTasks.new
require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
test.pattern = 'test/**/test_*.rb'
test.verbose = true
end
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |test|
test.verbose = true
end
desc "Run Test Unit with code coverage"
task :test_coverage do
ENV['COVERAGE'] = 'true'
Rake::Task["test"].execute
end
desc "Run RSpec with code coverage"
task :rspec_coverage do
ENV['COVERAGE'] = 'true'
Rake::Task["spec"].execute
end
task :default => :test
require 'rdoc/task'
RDoc::Task.new do |rdoc|
version = File.exist?('VERSION') ? File.read('VERSION') : ""
rdoc.rdoc_dir = 'rdoc'
rdoc.title = "musicbrainz #{version}"
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
end
task :default => :spec
task :test => :spec

View File

@ -1 +0,0 @@
0.5.2

12
lib/deprecated.rb Normal file
View File

@ -0,0 +1,12 @@
# -*- encoding: utf-8 -*-
module MusicBrainz
def self.query_interval=(sec)
MusicBrainz::Tools::Proxy.query_interval = sec
puts "WARNING! MusicBrainz.query_interval is deprecated. Use MusicBrainz::Tools::Proxy.query_interval"
end
def self.cache_path=(path)
MusicBrainz::Tools::Cache.cache_path = path
puts "WARNING! MusicBrainz.cache_path is deprecated. Use MusicBrainz::Tools::Cache.cache_path"
end
end

View File

@ -1,78 +0,0 @@
module MusicBrainz
@@last_query_time = 0
@@query_interval = 1.5 # Min: 1.0 Safe: 1.5
@@cache_path = nil
WEB_SERVICE_URL = 'http://musicbrainz.org/ws/2/'
USER_AGENT = "gem musicbrainz (https://github.com/magnolia-fan/musicbrainz) @ " + Socket.gethostname
def self.query_interval= sec
@@query_interval = sec.to_f
end
def self.cache_path= path
@@cache_path = path
end
def self.load resourse, params = {}
url = WEB_SERVICE_URL + resourse.to_s.gsub('_', '-') + '/' + (params[:id].to_s || '')
params.delete(:id) unless params[:id].nil?
url << '?' + params.map{ |k, v|
k.to_s.gsub('_', '-') + '=' + (v.is_a?(Array) ? v.map{ |_| _.to_s.gsub('_', '-') }.join('+') : v.to_s)
}.join('&') unless params.empty?
self.cache_contents(url) do
self.get_contents url
end
end
def self.clear_cache
FileUtils.rm_r(@@cache_path) if @@cache_path && File.exist?(@@cache_path)
end
private
def self.cache_contents url
response = nil
url_parts = url.split('/')
file_name = url_parts.pop
directory = url_parts.pop
file_path = @@cache_path ? "#{@@cache_path}/#{directory}/#{file_name}" : nil
if file_path && File.exist?(file_path)
response = File.open(file_path).gets
else
response = yield
unless response.nil? || file_path.nil?
FileUtils.mkdir_p file_path.split('/')[0..-2].join('/')
file = File.new(file_path, 'w')
file.puts(response.gets.force_encoding('UTF-8'))
file.chmod(0755)
file.close
response.rewind
end
end
response
end
def self.get_contents url
response = nil
5.times do
time_passed = Time.now.to_f - @@last_query_time
sleep @@query_interval - time_passed if time_passed < @@query_interval
begin
response = open(url, "User-Agent" => USER_AGENT)
@@last_query_time = Time.now.to_f
rescue => e
response = nil if e.io.status[0].to_i == 404
end
break unless response.nil?
end
response
end
end

View File

@ -1,11 +1,20 @@
# -*- encoding: utf-8 -*-
require "open-uri"
require "socket"
require "nokogiri"
require "cgi"
require "models/music_brainz"
require "models/music_brainz/base"
require "models/music_brainz/artist"
require "models/music_brainz/release_group"
require "models/music_brainz/release"
require "models/music_brainz/track"
require "version"
module MusicBrainz
autoload :Base, "musicbrainz/base"
autoload :Artist, "musicbrainz/artist"
autoload :ReleaseGroup, "musicbrainz/release_group"
autoload :Release, "musicbrainz/release"
autoload :Track, "musicbrainz/track"
module Tools
autoload :Cache, "tools/cache"
autoload :Proxy, "tools/proxy"
end
end

View File

@ -1,26 +1,26 @@
# encoding: UTF-8
# -*- encoding: utf-8 -*-
module MusicBrainz
class Artist < MusicBrainz::Base
attr_accessor :id, :type, :name, :country, :date_begin, :date_end, :urls
@release_groups
def release_groups
if @release_groups.nil? and not self.id.nil?
@release_groups = []
Nokogiri::XML(MusicBrainz.load(:release_group, :artist => self.id)).css('release-group').each do |rg|
Nokogiri::XML(self.class.load(:release_group, :artist => self.id)).css('release-group').each do |rg|
@release_groups << MusicBrainz::ReleaseGroup.parse_xml(rg)
end
end
@release_groups.sort{ |a, b| a.first_release_date <=> b.first_release_date }
end
def self.find mbid
res = MusicBrainz.load :artist, :id => mbid, :inc => [:url_rels]
res = self.load :artist, :id => mbid, :inc => [:url_rels]
return nil if res.nil?
@artist = self.parse_xml(Nokogiri::XML(res))
end
def self.parse_xml xml
@artist = MusicBrainz::Artist.new
@artist.id = self.safe_get_attr(xml, 'artist', 'id')
@ -35,21 +35,21 @@ module MusicBrainz
end
@artist
end
def self.discography mbid
artist = self.find(mbid)
artist.release_groups.each {|rg| rg.releases.each {|r| r.tracks } }
artist
end
def self.find_by_name name
matches = self.search name
matches.length.zero? ? nil : self.find(matches.first[:mbid])
end
def self.search name
artists = []
xml = Nokogiri::XML(MusicBrainz.load(:artist, :query => CGI.escape(name).gsub(/\!/, '\!') + '~', :limit => 50))
xml = Nokogiri::XML(self.load(:artist, :query => CGI.escape(name).gsub(/\!/, '\!') + '~', :limit => 50))
xml.css('artist-list > artist').each do |a|
artist = {
:name => a.first_element_child.text.gsub(/[`]/, "'"),

View File

@ -1,12 +1,17 @@
# -*- encoding: utf-8 -*-
module MusicBrainz
class Base
def self.safe_get_attr xml, path, name
def self.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 self.safe_get_value xml, path
def self.safe_get_value(xml, path)
xml.css(path).first.text unless xml.css(path).empty?
end
def self.load(*args)
MusicBrainz::Tools::Proxy.load(*args)
end
end
end

View File

@ -1,23 +1,24 @@
# -*- encoding: utf-8 -*-
module MusicBrainz
class Release < MusicBrainz::Base
attr_accessor :id, :title, :status, :format, :date, :country
@tracks
def tracks
if @tracks.nil? and not self.id.nil?
@tracks = []
Nokogiri::XML(MusicBrainz.load(:release, :id => self.id, :inc => [:recordings, :media], :limit => 100)).css('medium-list > medium > track-list > track').each do |r|
Nokogiri::XML(self.class.load(:release, :id => self.id, :inc => [:recordings, :media], :limit => 100)).css('medium-list > medium > track-list > track').each do |r|
@tracks << MusicBrainz::Track.parse_xml(r)
end
end
@tracks.sort{ |a, b| a.position <=> b.position }
end
def self.find mbid
xml = Nokogiri::XML(MusicBrainz.load(:release, :id => mbid, :inc => [:media])).css('release').first
xml = Nokogiri::XML(self.load(:release, :id => mbid, :inc => [:media])).css('release').first
self.parse_xml(xml) unless xml.nil?
end
def self.parse_xml xml
@release = MusicBrainz::Release.new
@release.id = self.safe_get_attr(xml, nil, 'id')

View File

@ -1,23 +1,24 @@
# -*- encoding: utf-8 -*-
module MusicBrainz
class ReleaseGroup < MusicBrainz::Base
attr_accessor :id, :type, :title, :disambiguation, :first_release_date
@releases
def releases
if @releases.nil? and not self.id.nil?
@releases = []
Nokogiri::XML(MusicBrainz.load(:release, :release_group => self.id, :inc => [:media], :limit => 100)).css('release').each do |r|
Nokogiri::XML(self.class.load(:release, :release_group => self.id, :inc => [:media], :limit => 100)).css('release').each do |r|
@releases << MusicBrainz::Release.parse_xml(r)
end
end
@releases.sort{ |a, b| a.date <=> b.date }
end
def self.find mbid
xml = Nokogiri::XML(MusicBrainz.load(:release_group, :id => mbid)).css('release-group').first
xml = Nokogiri::XML(self.load(:release_group, :id => 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 = self.safe_get_attr(xml, nil, 'id')

View File

@ -1,12 +1,13 @@
# -*- encoding: utf-8 -*-
module MusicBrainz
class Track < MusicBrainz::Base
attr_accessor :position, :recording_id, :title, :length
def self.find mbid
xml = Nokogiri::XML(MusicBrainz.load(:recording, :id => mbid))
xml = Nokogiri::XML(self.load(:recording, :id => mbid))
self.parse_xml(xml) unless xml.nil?
end
def self.parse_xml xml
@track = MusicBrainz::Track.new
@track.position = self.safe_get_value(xml, 'position').to_i

45
lib/tools/cache.rb Normal file
View File

@ -0,0 +1,45 @@
# -*- encoding: utf-8 -*-
module MusicBrainz
module Tools
class Cache
@@cache_path = nil
def self.cache_path=(path)
@@cache_path = path
end
def self.cache_path
@@cache_path
end
def self.clear_cache
FileUtils.rm_r(@@cache_path) if @@cache_path && File.exist?(@@cache_path)
end
def self.cache_contents(url)
response = nil
url_parts = url.split('/')
file_name = url_parts.pop
directory = url_parts.pop
file_path = @@cache_path ? "#{@@cache_path}/#{directory}/#{file_name}" : nil
if file_path && File.exist?(file_path)
response = File.open(file_path).gets
else
response = yield
unless response.nil? or file_path.nil?
FileUtils.mkdir_p file_path.split('/')[0..-2].join('/')
file = File.new(file_path, 'w')
file.puts(response.gets) # .force_encoding('UTF-8')
file.chmod(0755)
file.close
response.rewind
end
end
response
end
end
end
end

54
lib/tools/proxy.rb Normal file
View File

@ -0,0 +1,54 @@
# -*- encoding: utf-8 -*-
module MusicBrainz
module Tools
class Proxy
@@last_query_time = 0
@@query_interval = 1.5 # Min: 1.0 Safe: 1.5
@@tries_limit = 5
WEB_SERVICE_URL = 'http://musicbrainz.org/ws/2/'
USER_AGENT = "gem musicbrainz (https://github.com/magnolia-fan/musicbrainz) @ " + Socket.gethostname
def self.query_interval=(sec)
@@query_interval = sec.to_f
end
def self.tries_limit=(num)
@@tries_limit = num.to_i
end
def self.load(resourse, params = {})
url = WEB_SERVICE_URL + resourse.to_s.gsub('_', '-') + '/' + (params[:id].to_s || '')
params.delete(:id) unless params[:id].nil?
url << '?' + params.map{ |k, v|
k = k.to_s.gsub('_', '-')
v = (v.is_a?(Array) ? v.map{ |_| _.to_s.gsub('_', '-') }.join('+') : v.to_s)
k + '=' + v
}.join('&') unless params.empty?
MusicBrainz::Tools::Cache.cache_contents(url) {
self.get_contents url
}
end
def self.get_contents(url)
response = nil
@@tries_limit.times {
time_passed = Time.now.to_f - @@last_query_time
sleep(@@query_interval - time_passed) if time_passed < @@query_interval
begin
response = open(url, "User-Agent" => USER_AGENT)
@@last_query_time = Time.now.to_f
rescue => e
response = nil if e.io.status[0].to_i == 404
end
break unless response.nil?
}
response
end
end
end
end

4
lib/version.rb Normal file
View File

@ -0,0 +1,4 @@
# -*- encoding: utf-8 -*-
module MusicBrainz
VERSION = "0.6.0-rc1"
end

View File

@ -1,83 +1,22 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/version', __FILE__)
Gem::Specification.new do |s|
s.name = %q{musicbrainz}
s.version = "0.5.2"
Gem::Specification.new do |gem|
gem.authors = ["Gregory Eremin"]
gem.email = ["magnolia_fan@me.com"]
gem.summary = %q{MusicBrainz Web Service wrapper with ActiveRecord-style models}
gem.homepage = "http://github.com/magnolia-fan/musicbrainz"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Gregory Eremin"]
s.date = %q{2011-11-15}
s.description = %q{MusicBrainz Web Service wrapper with ActiveRecord-style models}
s.email = %q{magnolia_fan@me.com}
s.extra_rdoc_files = [
"LICENSE.txt",
"README.rdoc"
]
s.files = [
".document",
".travis.yml",
"Gemfile",
"LICENSE.txt",
"README.rdoc",
"Rakefile",
"VERSION",
"lib/models/music_brainz.rb",
"lib/models/music_brainz/artist.rb",
"lib/models/music_brainz/base.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",
"spec/requests/artist_spec.rb",
"spec/spec_helper.rb",
"spec/support/vcr.rb",
"test/helper.rb",
"test/test_musicbrainz_artist.rb",
"test/test_musicbrainz_release.rb",
"test/test_musicbrainz_release_group.rb",
"test/test_musicbrainz_track.rb"
]
s.homepage = %q{http://github.com/magnolia-fan/musicbrainz}
s.licenses = ["MIT"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.6.2}
s.summary = %q{MusicBrainz Web Service wrapper}
gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "musicbrainz"
gem.require_paths = ["lib"]
gem.version = MusicBrainz::VERSION
gem.license = "MIT"
if s.respond_to? :specification_version then
s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<nokogiri>, [">= 0"])
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
s.add_development_dependency(%q<simplecov>, [">= 0"])
s.add_development_dependency(%q<rdoc>, [">= 0"])
s.add_development_dependency(%q<shoulda>, [">= 0"])
s.add_development_dependency(%q<rspec>, [">= 0"])
s.add_development_dependency(%q<vcr>, [">= 0"])
s.add_development_dependency(%q<webmock>, [">= 0"])
else
s.add_dependency(%q<nokogiri>, [">= 0"])
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
s.add_dependency(%q<simplecov>, [">= 0"])
s.add_dependency(%q<rdoc>, [">= 0"])
s.add_dependency(%q<shoulda>, [">= 0"])
s.add_dependency(%q<rspec>, [">= 0"])
s.add_dependency(%q<vcr>, [">= 0"])
s.add_dependency(%q<webmock>, [">= 0"])
end
else
s.add_dependency(%q<nokogiri>, [">= 0"])
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
s.add_dependency(%q<simplecov>, [">= 0"])
s.add_dependency(%q<rdoc>, [">= 0"])
s.add_dependency(%q<shoulda>, [">= 0"])
s.add_dependency(%q<rspec>, [">= 0"])
s.add_dependency(%q<vcr>, [">= 0"])
s.add_dependency(%q<webmock>, [">= 0"])
end
gem.add_dependency("nokogiri")
gem.add_development_dependency("rake")
gem.add_development_dependency("awesome_print")
gem.add_development_dependency("rspec")
end

View File

@ -1,51 +0,0 @@
require 'spec_helper'
describe MusicBrainz do
describe '.cache_contents', :vcr do
let(:cache_path) { File.join(File.dirname(__FILE__), '../../tmp/rspec_test/musicbrainz') }
let(:response) { ::StringIO.new('<?xml version="1.0" encoding="UTF-8"?><metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#"><artist type="Group" id="69b39eab-6577-46a4-a9f5-817839092033"><name>Kasabian</name><sort-name>Kasabian</sort-name><country>GB</country><life-span><begin>1999</begin></life-span><relation-list target-type="url"><relation type="allmusic"><target>http://allmusic.com/artist/p678134</target></relation><relation type="wikipedia"><target>http://en.wikipedia.org/wiki/Kasabian</target></relation><relation type="microblog"><target>http://twitter.com/kasabianhq</target></relation><relation type="BBC Music page"><target>http://www.bbc.co.uk/music/artists/69b39eab-6577-46a4-a9f5-817839092033</target></relation><relation type="discogs"><target>http://www.discogs.com/artist/Kasabian</target></relation><relation type="social network"><target>http://www.facebook.com/kasabian</target></relation><relation type="IMDb"><target>http://www.imdb.com/name/nm1868442/</target></relation><relation type="official homepage"><target>http://www.kasabian.co.uk/</target></relation><relation type="myspace"><target>http://www.myspace.com/kasabian</target></relation><relation type="youtube"><target>http://www.youtube.com/kasabianvevo</target></relation><relation type="youtube"><target>http://www.youtube.com/user/KasabianTour</target></relation></relation-list></artist></metadata>') }
before(:each) do
MusicBrainz.clear_cache
end
after(:each) do
MusicBrainz.clear_cache
end
context 'with cache enabled' do
it 'calls get contents only once when requesting the resource twice' do
MusicBrainz.cache_path = cache_path
MusicBrainz.stub(:get_contents).and_return(response)
MusicBrainz.should_receive(:get_contents).once
mbid = '69b39eab-6577-46a4-a9f5-817839092033'
File.exist?("#{cache_path}/artist/#{mbid}?inc=url-rels").should be_false
artist = MusicBrainz::Artist.find(mbid)
artist.should be_a_kind_of(MusicBrainz::Artist)
File.exist?("#{cache_path}/artist/#{mbid}?inc=url-rels").should be_true
artist = MusicBrainz::Artist.find(mbid)
artist.should be_a_kind_of(MusicBrainz::Artist)
end
end
context 'with cache disabled' do
it 'calls get contents twice when requesting the resource twice' do
MusicBrainz.cache_path = nil
MusicBrainz.stub(:get_contents).and_return(response)
MusicBrainz.should_receive(:get_contents).twice
mbid = '69b39eab-6577-46a4-a9f5-817839092033'
File.exist?("#{cache_path}/artist/#{mbid}?inc=url-rels").should be_false
artist = MusicBrainz::Artist.find(mbid)
artist.should be_a_kind_of(MusicBrainz::Artist)
File.exist?("#{cache_path}/artist/#{mbid}?inc=url-rels").should be_false
response.rewind
MusicBrainz.stub(:get_contents).and_return(response)
artist = MusicBrainz::Artist.find(mbid)
artist.should be_a_kind_of(MusicBrainz::Artist)
end
end
end
end

View File

@ -1,44 +1,49 @@
require 'spec_helper'
require "spec_helper"
describe "artist" do
it "return valid instance", :vcr do
artist = MusicBrainz::Artist.find('69b39eab-6577-46a4-a9f5-817839092033')
artist.should be_a_kind_of(MusicBrainz::Artist)
describe MusicBrainz::Artist do
it "gets no exception while loading artist info" do
lambda {
MusicBrainz::Artist.find('69b39eab-6577-46a4-a9f5-817839092033')
}.should_not raise_error(Exception)
end
it "search by name", :vcr do
it "gets correct instance" do
artist = MusicBrainz::Artist.find_by_name('Kasabian')
artist.should be_an_instance_of(MusicBrainz::Artist)
end
it "searches artist by name" do
matches = MusicBrainz::Artist.search('Kasabian')
matches.should have_at_least(1).item
matches.first[:name].should eql('Kasabian')
matches.length.should be > 0
matches.first[:name].should == "Kasabian"
end
it "finds name first than alias" do
matches = MusicBrainz::Artist.search('Chris Martin')
matches.length.should be > 0
matches.first[:name].should == "Chris Martin"
end
it "gets correct result by name" do
artist = MusicBrainz::Artist.find_by_name('Kasabian')
artist.id.should == "69b39eab-6577-46a4-a9f5-817839092033"
end
it "gets correct artist data" do
artist = MusicBrainz::Artist.find_by_name('Kasabian')
artist.id.should == "69b39eab-6577-46a4-a9f5-817839092033"
artist.type.should == "Group"
artist.name.should == "Kasabian"
artist.country.should == "GB"
artist.date_begin.should == "1999"
end
it "gets correct artist's release groups" do
release_groups = MusicBrainz::Artist.find_by_name('Kasabian').release_groups
release_groups.length.should be >= 16
release_groups.first.id.should == "533cbc5f-ec7e-32ab-95f3-8d1f804a5176"
release_groups.first.type.should == "Single"
release_groups.first.title.should == "Club Foot"
release_groups.first.first_release_date.should == Time.utc(2004, 5, 10)
end
#
# should "get correct result by name" do
# @artist = MusicBrainz::Artist.find_by_name('Kasabian')
# assert_equal("69b39eab-6577-46a4-a9f5-817839092033", @artist.id)
# end
#
# setup do
# @artist = MusicBrainz::Artist.find('69b39eab-6577-46a4-a9f5-817839092033')
# end
#
# should "return valid instance" do
# assert_instance_of(MusicBrainz::Artist, @artist)
# end
#
# should "contain correct data" do
# assert_equal("69b39eab-6577-46a4-a9f5-817839092033", @artist.id)
# assert_equal("Group", @artist.type)
# assert_equal("Kasabian", @artist.name)
# assert_equal("GB", @artist.country)
# assert_equal("1999", @artist.date_begin)
# end
#
# should "load release groups" do
# release_groups = @artist.release_groups
# assert_operator(16, :<=, release_groups.length)
# assert_equal('533cbc5f-ec7e-32ab-95f3-8d1f804a5176', release_groups.first.id)
# assert_equal('Single', release_groups.first.type)
# assert_equal('Club Foot', release_groups.first.title)
# assert_equal(Time.utc(2004, 5, 10), release_groups.first.first_release_date)
# end
end

View File

@ -0,0 +1,32 @@
require "spec_helper"
describe MusicBrainz::ReleaseGroup do
it "gets no exception while loading release group info" do
lambda {
MusicBrainz::ReleaseGroup.find("6f33e0f0-cde2-38f9-9aee-2c60af8d1a61")
}.should_not raise_error(Exception)
end
it "gets correct instance" do
release_group = MusicBrainz::ReleaseGroup.find("6f33e0f0-cde2-38f9-9aee-2c60af8d1a61")
release_group.should be_an_instance_of(MusicBrainz::ReleaseGroup)
end
it "gets correct release group data" do
release_group = MusicBrainz::ReleaseGroup.find("6f33e0f0-cde2-38f9-9aee-2c60af8d1a61")
release_group.id.should == "6f33e0f0-cde2-38f9-9aee-2c60af8d1a61"
release_group.type.should == "Album"
release_group.title.should == "Empire"
release_group.first_release_date.should == Time.utc(2006, 8, 28)
end
it "gets correct release group's releases" do
releases = MusicBrainz::ReleaseGroup.find("6f33e0f0-cde2-38f9-9aee-2c60af8d1a61").releases
releases.length.should be >= 5
releases.first.id.should == "2225dd4c-ae9a-403b-8ea0-9e05014c778f"
releases.first.status.should == "Official"
releases.first.title.should == "Empire"
releases.first.date.should == Time.utc(2006, 8, 28)
releases.first.country.should == "GB"
end
end

View File

@ -0,0 +1,32 @@
require "spec_helper"
describe MusicBrainz::Release do
it "gets no exception while loading release info" do
lambda {
MusicBrainz::Release.find("2225dd4c-ae9a-403b-8ea0-9e05014c778f")
}.should_not raise_error(Exception)
end
it "gets correct instance" do
release = MusicBrainz::Release.find("2225dd4c-ae9a-403b-8ea0-9e05014c778f")
release.should be_an_instance_of(MusicBrainz::Release)
end
it "gets correct release data" do
release = MusicBrainz::Release.find("2225dd4c-ae9a-403b-8ea0-9e05014c778f")
release.id.should == "2225dd4c-ae9a-403b-8ea0-9e05014c778f"
release.title.should == "Empire"
release.status.should == "Official"
release.date.should == Time.utc(2006, 8, 28)
release.country.should == "GB"
end
it "gets correct release tracks" do
tracks = MusicBrainz::Release.find("2225dd4c-ae9a-403b-8ea0-9e05014c778f").tracks
tracks.length.should == 11
tracks.first.position.should == 1
tracks.first.recording_id.should == "b3015bab-1540-4d4e-9f30-14872a1525f7"
tracks.first.title.should == "Empire"
tracks.first.length.should == 233013
end
end

View File

@ -0,0 +1,21 @@
require "spec_helper"
describe MusicBrainz::Track do
it "gets no exception while loading release info" do
lambda {
MusicBrainz::Track.find("b3015bab-1540-4d4e-9f30-14872a1525f7")
}.should_not raise_error(Exception)
end
it "gets correct instance" do
track = MusicBrainz::Track.find("b3015bab-1540-4d4e-9f30-14872a1525f7")
track.should be_an_instance_of(MusicBrainz::Track)
end
it "gets correct track data" do
track = MusicBrainz::Track.find("b3015bab-1540-4d4e-9f30-14872a1525f7")
track.recording_id.should == "b3015bab-1540-4d4e-9f30-14872a1525f7"
track.title.should == "Empire"
track.length.should == 233013
end
end

View File

@ -1,29 +1,11 @@
require 'simplecov'
require "rubygems"
require "bundler/setup"
require "ap"
if ENV["COVERAGE"]
SimpleCov.start do
add_filter '/gems/'
add_filter '/test/'
add_filter '/spec/'
end
end
require 'rubygems'
require 'bundler'
begin
Bundler.setup(:default, :development)
rescue Bundler::BundlerError => e
$stderr.puts e.message
$stderr.puts "Run `bundle install` to install missing gems"
exit e.status_code
end
require "vcr"
require "musicbrainz"
# HTTPI.log = false
Dir[File.dirname(__FILE__)+"/../lib/*.rb"].each{ |f| require f }
Dir[File.dirname(__FILE__)+"/../spec/support/*.rb"].each{ |f| require f }
MusicBrainz::Tools::Cache.cache_path = "tmp/cache"
RSpec.configure do |config|
config.mock_with :rspec
end
# Configuration is not currently necessary
end

View File

@ -1,13 +0,0 @@
VCR.config do |c|
c.cassette_library_dir = File.join(File.dirname(__FILE__), '..', '..', 'tmp', 'vcr')
c.stub_with :webmock
c.default_cassette_options = { :record => :new_episodes }
end
RSpec.configure do |c|
c.treat_symbols_as_metadata_keys_with_true_values = true
c.around(:each, :vcr) do |example|
name = example.metadata[:full_description].gsub(/\s/, '_')
VCR.use_cassette(name) { example.call }
end
end

71
spec/tools/cache_spec.rb Normal file
View File

@ -0,0 +1,71 @@
require "spec_helper"
describe MusicBrainz::Tools::Cache do
before(:all) do
@old_cache_path = MusicBrainz::Tools::Cache.cache_path
@tmp_cache_path = File.join(File.dirname(__FILE__), '../../tmp/cache/tools')
end
after(:all) do
MusicBrainz::Tools::Cache.cache_path = @old_cache_path
end
before(:each) do
@test_response = ::StringIO.new('<?xml version="1.0" encoding="UTF-8"?>'+
'<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#">'+
'<artist type="Group" id="69b39eab-6577-46a4-a9f5-817839092033">'+
'<name>Kasabian</name><sort-name>Kasabian</sort-name><country>GB</country>'+
'<life-span><begin>1999</begin></life-span><relation-list target-type="url">'+
'<relation type="allmusic"><target>http://allmusic.com/artist/p678134</target>'+
'</relation><relation type="wikipedia"><target>http://en.wikipedia.org/wiki/Kasabian</target>'+
'</relation><relation type="microblog"><target>http://twitter.com/kasabianhq</target>'+
'</relation><relation type="BBC Music page"><target>'+
'http://www.bbc.co.uk/music/artists/69b39eab-6577-46a4-a9f5-817839092033</target></relation>'+
'<relation type="discogs"><target>http://www.discogs.com/artist/Kasabian</target></relation>'+
'<relation type="social network"><target>http://www.facebook.com/kasabian</target></relation>'+
'<relation type="IMDb"><target>http://www.imdb.com/name/nm1868442/</target></relation>'+
'<relation type="official homepage"><target>http://www.kasabian.co.uk/</target></relation>'+
'<relation type="myspace"><target>http://www.myspace.com/kasabian</target></relation>'+
'<relation type="youtube"><target>http://www.youtube.com/kasabianvevo</target></relation>'+
'<relation type="youtube"><target>http://www.youtube.com/user/KasabianTour</target></relation>'+
'</relation-list></artist></metadata>')
end
context "with cache enabled" do
it "calls get contents only once when requesting the resource twice" do
MusicBrainz::Tools::Cache.cache_path = @tmp_cache_path
MusicBrainz::Tools::Proxy.stub(:get_contents).and_return(@test_response)
MusicBrainz::Tools::Proxy.should_receive(:get_contents).once
mbid = "69b39eab-6577-46a4-a9f5-817839092033"
File.exist?("#{@tmp_cache_path}/artist/#{mbid}?inc=url-rels").should be_false
artist = MusicBrainz::Artist.find(mbid)
artist.should be_a_kind_of(MusicBrainz::Artist)
File.exist?("#{@tmp_cache_path}/artist/#{mbid}?inc=url-rels").should be_true
artist = MusicBrainz::Artist.find(mbid)
artist.should be_a_kind_of(MusicBrainz::Artist)
MusicBrainz::Tools::Cache.clear_cache
end
end
context "with cache disabled" do
it "calls get contents twice when requesting the resource twice" do
MusicBrainz::Tools::Cache.cache_path = nil
MusicBrainz::Tools::Proxy.stub(:get_contents).and_return(@test_response)
MusicBrainz::Tools::Proxy.should_receive(:get_contents).twice
mbid = "69b39eab-6577-46a4-a9f5-817839092033"
File.exist?("#{@tmp_cache_path}/artist/#{mbid}?inc=url-rels").should be_false
artist = MusicBrainz::Artist.find(mbid)
artist.should be_a_kind_of(MusicBrainz::Artist)
File.exist?("#{@tmp_cache_path}/artist/#{mbid}?inc=url-rels").should be_false
@test_response.rewind
MusicBrainz.stub(:get_contents).and_return(@test_response)
artist = MusicBrainz::Artist.find(mbid)
artist.should be_a_kind_of(MusicBrainz::Artist)
end
end
end

View File

@ -1,30 +0,0 @@
require 'simplecov'
if ENV["COVERAGE"]
SimpleCov.start do
add_filter '/gems/'
add_filter '/test/'
add_filter '/spec/'
end
end
require 'rubygems'
require 'bundler'
begin
Bundler.setup(:default, :test)
rescue Bundler::BundlerError => e
$stderr.puts e.message
$stderr.puts "Run `bundle install` to install missing gems"
exit e.status_code
end
require 'test/unit'
require 'shoulda'
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'musicbrainz'
MusicBrainz.cache_path = File.join(File.dirname(__FILE__), '../tmp/test/musicbrainz')
class Test::Unit::TestCase
end

View File

@ -1,53 +0,0 @@
require 'helper'
class TestMusicbrainzArtist < Test::Unit::TestCase
context "artist" do
should "load artist xml" do
assert_nothing_raised(Exception) do
MusicBrainz::Artist.find('69b39eab-6577-46a4-a9f5-817839092033')
end
end
should "search by name" do
matches = MusicBrainz::Artist.search('Kasabian')
assert_operator(0, :<, matches.length)
assert_equal("Kasabian", matches.first[:name])
end
should "find name first than alias" do
matches = MusicBrainz::Artist.search('Chris Martin')
assert_operator(0, :<, matches.length)
assert_equal("Chris Martin", matches.first[:name])
end
should "get correct result by name" do
@artist = MusicBrainz::Artist.find_by_name('Kasabian')
assert_equal("69b39eab-6577-46a4-a9f5-817839092033", @artist.id)
end
setup do
@artist = MusicBrainz::Artist.find('69b39eab-6577-46a4-a9f5-817839092033')
end
should "return valid instance" do
assert_instance_of(MusicBrainz::Artist, @artist)
end
should "contain correct data" do
assert_equal("69b39eab-6577-46a4-a9f5-817839092033", @artist.id)
assert_equal("Group", @artist.type)
assert_equal("Kasabian", @artist.name)
assert_equal("GB", @artist.country)
assert_equal("1999", @artist.date_begin)
end
should "load release groups" do
release_groups = @artist.release_groups
assert_operator(16, :<=, release_groups.length)
assert_equal('533cbc5f-ec7e-32ab-95f3-8d1f804a5176', release_groups.first.id)
assert_equal('Single', release_groups.first.type)
assert_equal('Club Foot', release_groups.first.title)
assert_equal(Time.utc(2004, 5, 10), release_groups.first.first_release_date)
end
end
end

View File

@ -1,36 +0,0 @@
require 'helper'
class TestMusicbrainzRelease < Test::Unit::TestCase
context "release" do
should "load xml" do
assert_nothing_raised(Exception) do
MusicBrainz::Release.find('2225dd4c-ae9a-403b-8ea0-9e05014c778f')
end
end
setup do
@release = MusicBrainz::Release.find('2225dd4c-ae9a-403b-8ea0-9e05014c778f')
end
should "return valid instance" do
assert_instance_of(MusicBrainz::Release, @release)
end
should "contain correct data" do
assert_equal("2225dd4c-ae9a-403b-8ea0-9e05014c778f", @release.id)
assert_equal("Empire", @release.title)
assert_equal("Official", @release.status)
assert_equal(Time.utc(2006, 8, 28), @release.date)
assert_equal("GB", @release.country)
end
should "load tracks" do
tracks = @release.tracks
assert_equal(11, tracks.length)
assert_equal(1, tracks.first.position)
assert_equal('b3015bab-1540-4d4e-9f30-14872a1525f7', tracks.first.recording_id)
assert_equal('Empire', tracks.first.title)
assert_equal(233013, tracks.first.length)
end
end
end

View File

@ -1,36 +0,0 @@
require 'helper'
class TestMusicbrainzReleaseGroup < Test::Unit::TestCase
context "release group" do
should "load xml" do
assert_nothing_raised(Exception) do
MusicBrainz::ReleaseGroup.find('6f33e0f0-cde2-38f9-9aee-2c60af8d1a61')
end
end
setup do
@release_group = MusicBrainz::ReleaseGroup.find('6f33e0f0-cde2-38f9-9aee-2c60af8d1a61')
end
should "return valid instance" do
assert_instance_of(MusicBrainz::ReleaseGroup, @release_group)
end
should "contain correct data" do
assert_equal("6f33e0f0-cde2-38f9-9aee-2c60af8d1a61", @release_group.id)
assert_equal("Album", @release_group.type)
assert_equal("Empire", @release_group.title)
assert_equal(Time.utc(2006, 8, 28), @release_group.first_release_date)
end
should "load releases" do
releases = @release_group.releases
assert_operator(5, :<=, releases.length)
assert_equal('2225dd4c-ae9a-403b-8ea0-9e05014c778f', releases.first.id)
assert_equal('Official', releases.first.status)
assert_equal('Empire', releases.first.title)
assert_equal(Time.utc(2006, 8, 28), releases.first.date)
assert_equal('GB', releases.first.country)
end
end
end

View File

@ -1,25 +0,0 @@
require 'helper'
class TestMusicbrainzTrack < Test::Unit::TestCase
context "track" do
should "load xml" do
assert_nothing_raised(Exception) do
MusicBrainz::Track.find('b3015bab-1540-4d4e-9f30-14872a1525f7')
end
end
setup do
@track = MusicBrainz::Track.find('b3015bab-1540-4d4e-9f30-14872a1525f7')
end
should "return valid instance" do
assert_instance_of(MusicBrainz::Track, @track)
end
should "contain correct data" do
assert_equal("b3015bab-1540-4d4e-9f30-14872a1525f7", @track.recording_id)
assert_equal("Empire", @track.title)
assert_equal(233013, @track.length)
end
end
end