From 83dd697fb924fe48563dd1815f10c0cbd2e30988 Mon Sep 17 00:00:00 2001 From: magnolia-fan Date: Thu, 24 Nov 2011 01:45:45 +0400 Subject: [PATCH] Moving musicbrainz gem inside app --- Gemfile | 3 +- Gemfile.lock | 18 ++-- vendor/plugins/gems/musicbrainz/Gemfile | 15 ++++ vendor/plugins/gems/musicbrainz/Gemfile.lock | 44 ++++++++++ vendor/plugins/gems/musicbrainz/LICENSE.txt | 20 +++++ vendor/plugins/gems/musicbrainz/README.rdoc | 76 ++++++++++++++++ vendor/plugins/gems/musicbrainz/Rakefile | 53 ++++++++++++ vendor/plugins/gems/musicbrainz/VERSION | 1 + .../musicbrainz/lib/models/music_brainz.rb | 38 ++++++++ .../lib/models/music_brainz/artist.rb | 75 ++++++++++++++++ .../lib/models/music_brainz/base.rb | 12 +++ .../lib/models/music_brainz/release.rb | 41 +++++++++ .../lib/models/music_brainz/release_group.rb | 40 +++++++++ .../lib/models/music_brainz/track.rb | 20 +++++ .../gems/musicbrainz/lib/musicbrainz.rb | 11 +++ .../gems/musicbrainz/musicbrainz.gemspec | 86 +++++++++++++++++++ .../musicbrainz/spec/requests/artist_spec.rb | 44 ++++++++++ .../gems/musicbrainz/spec/spec_helper.rb | 19 ++++ .../gems/musicbrainz/spec/support/vcr.rb | 13 +++ .../plugins/gems/musicbrainz/test/helper.rb | 18 ++++ .../test/test_musicbrainz_artist.rb | 47 ++++++++++ .../test/test_musicbrainz_release.rb | 36 ++++++++ .../test/test_musicbrainz_release_group.rb | 36 ++++++++ .../test/test_musicbrainz_track.rb | 25 ++++++ 24 files changed, 783 insertions(+), 8 deletions(-) create mode 100644 vendor/plugins/gems/musicbrainz/Gemfile create mode 100644 vendor/plugins/gems/musicbrainz/Gemfile.lock create mode 100644 vendor/plugins/gems/musicbrainz/LICENSE.txt create mode 100644 vendor/plugins/gems/musicbrainz/README.rdoc create mode 100644 vendor/plugins/gems/musicbrainz/Rakefile create mode 100644 vendor/plugins/gems/musicbrainz/VERSION create mode 100644 vendor/plugins/gems/musicbrainz/lib/models/music_brainz.rb create mode 100644 vendor/plugins/gems/musicbrainz/lib/models/music_brainz/artist.rb create mode 100644 vendor/plugins/gems/musicbrainz/lib/models/music_brainz/base.rb create mode 100644 vendor/plugins/gems/musicbrainz/lib/models/music_brainz/release.rb create mode 100644 vendor/plugins/gems/musicbrainz/lib/models/music_brainz/release_group.rb create mode 100644 vendor/plugins/gems/musicbrainz/lib/models/music_brainz/track.rb create mode 100644 vendor/plugins/gems/musicbrainz/lib/musicbrainz.rb create mode 100644 vendor/plugins/gems/musicbrainz/musicbrainz.gemspec create mode 100644 vendor/plugins/gems/musicbrainz/spec/requests/artist_spec.rb create mode 100644 vendor/plugins/gems/musicbrainz/spec/spec_helper.rb create mode 100644 vendor/plugins/gems/musicbrainz/spec/support/vcr.rb create mode 100644 vendor/plugins/gems/musicbrainz/test/helper.rb create mode 100644 vendor/plugins/gems/musicbrainz/test/test_musicbrainz_artist.rb create mode 100644 vendor/plugins/gems/musicbrainz/test/test_musicbrainz_release.rb create mode 100644 vendor/plugins/gems/musicbrainz/test/test_musicbrainz_release_group.rb create mode 100644 vendor/plugins/gems/musicbrainz/test/test_musicbrainz_track.rb diff --git a/Gemfile b/Gemfile index 2efbce0..69de778 100644 --- a/Gemfile +++ b/Gemfile @@ -20,7 +20,8 @@ gem 'jquery-rails' # Music related stuff gem 'lastfm-client', :git => 'http://github.com/pch/lastfm-client.git' -gem 'musicbrainz', :require => 'musicbrainz' +# gem 'musicbrainz', :require => 'musicbrainz' +gem 'musicbrainz', path: "vendor/plugins/gems/musicbrainz", :require => 'musicbrainz' # Misc utils gem 'bitmask_attributes' diff --git a/Gemfile.lock b/Gemfile.lock index 4e92a67..99ba64d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -5,6 +5,12 @@ GIT lastfm-client (0.0.3) json (>= 1.4.6) +PATH + remote: vendor/plugins/gems/musicbrainz + specs: + musicbrainz (0.5.2) + nokogiri + GEM remote: http://rubygems.org/ specs: @@ -40,7 +46,7 @@ GEM arel (2.2.1) awesome_print (0.4.0) bcrypt-ruby (3.0.1) - bitmask_attributes (0.2.3) + bitmask_attributes (0.2.4) activerecord (~> 3.0) builder (3.0.0) coffee-script (2.2.0) @@ -57,20 +63,18 @@ GEM haml (3.1.3) hike (1.2.1) i18n (0.6.0) - jquery-rails (1.0.17) + jquery-rails (1.0.18) railties (~> 3.0) thor (~> 0.14) json (1.6.1) kgio (2.6.0) - libv8 (3.3.10.2) + libv8 (3.3.10.4) mail (2.3.0) i18n (>= 0.4.0) mime-types (~> 1.16) treetop (~> 1.4.8) mime-types (1.17.2) multi_json (1.0.3) - musicbrainz (0.5.2) - nokogiri mysql2 (0.3.7) nokogiri (1.5.0) pg (0.11.0) @@ -116,7 +120,7 @@ GEM polyglot polyglot (>= 0.3.1) tzinfo (0.3.31) - uglifier (1.0.4) + uglifier (1.1.0) execjs (>= 0.3.0) multi_json (>= 1.0.2) unicorn (4.1.1) @@ -136,7 +140,7 @@ DEPENDENCIES jquery-rails json lastfm-client! - musicbrainz + musicbrainz! mysql2 (= 0.3.7) nokogiri pg diff --git a/vendor/plugins/gems/musicbrainz/Gemfile b/vendor/plugins/gems/musicbrainz/Gemfile new file mode 100644 index 0000000..48728dd --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/Gemfile @@ -0,0 +1,15 @@ +source "http://rubygems.org" + +group :development do + gem "bundler", "~> 1.0.0" + gem "jeweler", "~> 1.6.4" + gem "rcov", ">= 0" + gem "rdoc", ">= 0" + gem "shoulda" + + gem "rspec" + gem "vcr" + gem "webmock" +end + +gem "nokogiri", ">= 0" diff --git a/vendor/plugins/gems/musicbrainz/Gemfile.lock b/vendor/plugins/gems/musicbrainz/Gemfile.lock new file mode 100644 index 0000000..8c25450 --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/Gemfile.lock @@ -0,0 +1,44 @@ +GEM + remote: http://rubygems.org/ + specs: + addressable (2.2.6) + crack (0.3.1) + diff-lcs (1.1.3) + git (1.2.5) + jeweler (1.6.4) + bundler (~> 1.0) + git (>= 1.2.5) + rake + json (1.6.1) + nokogiri (1.5.0) + rake (0.9.2.2) + rcov (0.9.11) + rdoc (3.11) + json (~> 1.4) + rspec (2.7.0) + rspec-core (~> 2.7.0) + rspec-expectations (~> 2.7.0) + rspec-mocks (~> 2.7.0) + rspec-core (2.7.1) + rspec-expectations (2.7.0) + diff-lcs (~> 1.1.2) + rspec-mocks (2.7.0) + shoulda (2.11.3) + vcr (1.11.3) + webmock (1.7.7) + addressable (~> 2.2, > 2.2.5) + crack (>= 0.1.7) + +PLATFORMS + ruby + +DEPENDENCIES + bundler (~> 1.0.0) + jeweler (~> 1.6.4) + nokogiri + rcov + rdoc + rspec + shoulda + vcr + webmock diff --git a/vendor/plugins/gems/musicbrainz/LICENSE.txt b/vendor/plugins/gems/musicbrainz/LICENSE.txt new file mode 100644 index 0000000..dc2693f --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/LICENSE.txt @@ -0,0 +1,20 @@ +Copyright (c) 2011 magnolia-fan + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +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. diff --git a/vendor/plugins/gems/musicbrainz/README.rdoc b/vendor/plugins/gems/musicbrainz/README.rdoc new file mode 100644 index 0000000..c3e2c04 --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/README.rdoc @@ -0,0 +1,76 @@ += MusicBrainz Web Service wrapper {}[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 + +=== 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. +* 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. diff --git a/vendor/plugins/gems/musicbrainz/Rakefile b/vendor/plugins/gems/musicbrainz/Rakefile new file mode 100644 index 0000000..5e96f3a --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/Rakefile @@ -0,0 +1,53 @@ +# encoding: utf-8 + +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' + +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 'rcov/rcovtask' +Rcov::RcovTask.new do |test| + test.libs << 'test' + test.pattern = 'test/**/test_*.rb' + test.verbose = true + test.rcov_opts << '--exclude "gems/*"' +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 diff --git a/vendor/plugins/gems/musicbrainz/VERSION b/vendor/plugins/gems/musicbrainz/VERSION new file mode 100644 index 0000000..2411653 --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/VERSION @@ -0,0 +1 @@ +0.5.2 \ No newline at end of file diff --git a/vendor/plugins/gems/musicbrainz/lib/models/music_brainz.rb b/vendor/plugins/gems/musicbrainz/lib/models/music_brainz.rb new file mode 100644 index 0000000..3264704 --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/lib/models/music_brainz.rb @@ -0,0 +1,38 @@ +module MusicBrainz + @@last_query_time = 0 + @@query_interval = 1.5 # Min: 1.0 Safe: 1.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.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.get_contents url + end + +private + + def self.get_contents url + time_passed = Time.now.to_f - @@last_query_time + sleep @@query_interval - time_passed if time_passed < @@query_interval + response = nil + 5.times do + begin + response = open(url, "User-Agent" => USER_AGENT) + @@last_query_time = Time.now.to_f + rescue => e + return nil if e.io.status[0].to_i == 404 + end + break unless response.nil? + end + response + end +end diff --git a/vendor/plugins/gems/musicbrainz/lib/models/music_brainz/artist.rb b/vendor/plugins/gems/musicbrainz/lib/models/music_brainz/artist.rb new file mode 100644 index 0000000..54c89ad --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/lib/models/music_brainz/artist.rb @@ -0,0 +1,75 @@ +# 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| + @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] + 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') + @artist.type = self.safe_get_attr(xml, 'artist', 'type') + @artist.name = self.safe_get_value(xml, 'artist > name').gsub(/[`’]/, "'") + @artist.country = self.safe_get_value(xml, 'artist > country') + @artist.date_begin = self.safe_get_value(xml, 'artist > life-span > begin') + @artist.date_end = self.safe_get_value(xml, 'artist > life-span > end') + @artist.urls = {} + xml.css('relation-list[target-type="url"] > relation').each do |rel| + @artist.urls[rel.attr('type').downcase.split(' ').join('_').to_sym] = rel.css('target').text + 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 => name.gsub(/\!/, '\!').gsub(/\s/, '+') + '~', :limit => 50)) + xml.css('artist-list > artist').each do |a| + artist = { + :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 } + if aliases.include? name + artist[:weight] += 20 + elsif aliases.map{ |item| item.downcase }.include? name.downcase + artist[:weight] += 10 + elsif aliases.map{ |item| item.downcase.gsub(/\s/, '') }.include? name.downcase.gsub(/\s/, '') + artist[:weight] += 5 + end + artists << artist + end + artists.sort{ |a, b| b[:weight] <=> a[:weight] }.take(10) + end + end +end diff --git a/vendor/plugins/gems/musicbrainz/lib/models/music_brainz/base.rb b/vendor/plugins/gems/musicbrainz/lib/models/music_brainz/base.rb new file mode 100644 index 0000000..b7e10bf --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/lib/models/music_brainz/base.rb @@ -0,0 +1,12 @@ +module MusicBrainz + class Base + 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 + xml.css(path).first.text unless xml.css(path).empty? + end + end +end diff --git a/vendor/plugins/gems/musicbrainz/lib/models/music_brainz/release.rb b/vendor/plugins/gems/musicbrainz/lib/models/music_brainz/release.rb new file mode 100644 index 0000000..0cc9ba4 --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/lib/models/music_brainz/release.rb @@ -0,0 +1,41 @@ +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| + @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 + 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') + @release.title = self.safe_get_value(xml, 'title') + @release.status = self.safe_get_value(xml, 'status') + @release.format = self.safe_get_value(xml, 'medium-list > medium > format') + date = xml.css('date').empty? ? '2030-12-31' : xml.css('date').text + if date.length == 0 + date = '2030-12-31' + elsif date.length == 4 + date += '-12-31' + elsif date.length == 7 + date += '-31' + end + date = date.split('-') + @release.date = Time.utc(date[0], date[1], date[2]) + @release.country = self.safe_get_value(xml, 'country') + @release + end + end +end diff --git a/vendor/plugins/gems/musicbrainz/lib/models/music_brainz/release_group.rb b/vendor/plugins/gems/musicbrainz/lib/models/music_brainz/release_group.rb new file mode 100644 index 0000000..5e1cff4 --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/lib/models/music_brainz/release_group.rb @@ -0,0 +1,40 @@ +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| + @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 + 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') + @release_group.type = self.safe_get_attr(xml, nil, 'type') + @release_group.title = self.safe_get_value(xml, 'title') + @release_group.disambiguation = self.safe_get_value(xml, 'disambiguation') + date = xml.css('first-release-date').empty? ? '2030-12-31' : xml.css('first-release-date').text + if date.length == 0 + date = '2030-12-31' + elsif date.length == 4 + date += '-12-31' + elsif date.length == 7 + date += '-31' + end + date = date.split('-') + @release_group.first_release_date = Time.utc(date[0], date[1], date[2]) + @release_group + end + end +end diff --git a/vendor/plugins/gems/musicbrainz/lib/models/music_brainz/track.rb b/vendor/plugins/gems/musicbrainz/lib/models/music_brainz/track.rb new file mode 100644 index 0000000..44957d6 --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/lib/models/music_brainz/track.rb @@ -0,0 +1,20 @@ +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)) + 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 + @track.recording_id = self.safe_get_attr(xml, 'recording', 'id') + @track.title = self.safe_get_value(xml, 'recording > title') + @track.length = self.safe_get_value(xml, 'length').to_i + @track.length = self.safe_get_value(xml, 'recording > length').to_i + @track + end + end +end diff --git a/vendor/plugins/gems/musicbrainz/lib/musicbrainz.rb b/vendor/plugins/gems/musicbrainz/lib/musicbrainz.rb new file mode 100644 index 0000000..f6ce6a9 --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/lib/musicbrainz.rb @@ -0,0 +1,11 @@ +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" diff --git a/vendor/plugins/gems/musicbrainz/musicbrainz.gemspec b/vendor/plugins/gems/musicbrainz/musicbrainz.gemspec new file mode 100644 index 0000000..1bf5e83 --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/musicbrainz.gemspec @@ -0,0 +1,86 @@ +# Generated by jeweler +# DO NOT EDIT THIS FILE DIRECTLY +# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' +# -*- encoding: utf-8 -*- + +Gem::Specification.new do |s| + s.name = %q{musicbrainz} + s.version = "0.5.2" + + 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} + + 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, [">= 0"]) + s.add_development_dependency(%q, ["~> 1.0.0"]) + s.add_development_dependency(%q, ["~> 1.6.4"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + else + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, ["~> 1.0.0"]) + s.add_dependency(%q, ["~> 1.6.4"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + end + else + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, ["~> 1.0.0"]) + s.add_dependency(%q, ["~> 1.6.4"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + end +end + diff --git a/vendor/plugins/gems/musicbrainz/spec/requests/artist_spec.rb b/vendor/plugins/gems/musicbrainz/spec/requests/artist_spec.rb new file mode 100644 index 0000000..55dcbfb --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/spec/requests/artist_spec.rb @@ -0,0 +1,44 @@ +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) + end + + it "search by name", :vcr do + matches = MusicBrainz::Artist.search('Kasabian') + matches.should have_at_least(1).item + matches.first[:name].should eql('Kasabian') + 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 diff --git a/vendor/plugins/gems/musicbrainz/spec/spec_helper.rb b/vendor/plugins/gems/musicbrainz/spec/spec_helper.rb new file mode 100644 index 0000000..d83f4ad --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/spec/spec_helper.rb @@ -0,0 +1,19 @@ +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 } + +RSpec.configure do |config| + config.mock_with :rspec +end \ No newline at end of file diff --git a/vendor/plugins/gems/musicbrainz/spec/support/vcr.rb b/vendor/plugins/gems/musicbrainz/spec/support/vcr.rb new file mode 100644 index 0000000..06f5fbd --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/spec/support/vcr.rb @@ -0,0 +1,13 @@ +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 \ No newline at end of file diff --git a/vendor/plugins/gems/musicbrainz/test/helper.rb b/vendor/plugins/gems/musicbrainz/test/helper.rb new file mode 100644 index 0000000..50661f8 --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/test/helper.rb @@ -0,0 +1,18 @@ +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' + +class Test::Unit::TestCase +end diff --git a/vendor/plugins/gems/musicbrainz/test/test_musicbrainz_artist.rb b/vendor/plugins/gems/musicbrainz/test/test_musicbrainz_artist.rb new file mode 100644 index 0000000..df21a4f --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/test/test_musicbrainz_artist.rb @@ -0,0 +1,47 @@ +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 "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 diff --git a/vendor/plugins/gems/musicbrainz/test/test_musicbrainz_release.rb b/vendor/plugins/gems/musicbrainz/test/test_musicbrainz_release.rb new file mode 100644 index 0000000..5e5495a --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/test/test_musicbrainz_release.rb @@ -0,0 +1,36 @@ +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 diff --git a/vendor/plugins/gems/musicbrainz/test/test_musicbrainz_release_group.rb b/vendor/plugins/gems/musicbrainz/test/test_musicbrainz_release_group.rb new file mode 100644 index 0000000..c592595 --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/test/test_musicbrainz_release_group.rb @@ -0,0 +1,36 @@ +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 diff --git a/vendor/plugins/gems/musicbrainz/test/test_musicbrainz_track.rb b/vendor/plugins/gems/musicbrainz/test/test_musicbrainz_track.rb new file mode 100644 index 0000000..d1ecd50 --- /dev/null +++ b/vendor/plugins/gems/musicbrainz/test/test_musicbrainz_track.rb @@ -0,0 +1,25 @@ +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