diff --git a/README.rdoc b/README.rdoc index 79afb39..7e33ce5 100644 --- a/README.rdoc +++ b/README.rdoc @@ -18,6 +18,9 @@ # 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 diff --git a/lib/models/music_brainz.rb b/lib/models/music_brainz.rb index 3264704..a7a1ad1 100644 --- a/lib/models/music_brainz.rb +++ b/lib/models/music_brainz.rb @@ -1,6 +1,7 @@ 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 @@ -9,30 +10,69 @@ module MusicBrainz @@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.get_contents url + 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.get_contents url - time_passed = Time.now.to_f - @@last_query_time - sleep @@query_interval - time_passed if time_passed < @@query_interval + 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 - return nil if e.io.status[0].to_i == 404 + response = nil if e.io.status[0].to_i == 404 end - break unless response.nil? + + break unless response.nil? end + response end -end +end \ No newline at end of file diff --git a/spec/models/music_brainz_spec.rb b/spec/models/music_brainz_spec.rb new file mode 100644 index 0000000..931f170 --- /dev/null +++ b/spec/models/music_brainz_spec.rb @@ -0,0 +1,51 @@ +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('KasabianKasabianGB1999http://allmusic.com/artist/p678134http://en.wikipedia.org/wiki/Kasabianhttp://twitter.com/kasabianhqhttp://www.bbc.co.uk/music/artists/69b39eab-6577-46a4-a9f5-817839092033http://www.discogs.com/artist/Kasabianhttp://www.facebook.com/kasabianhttp://www.imdb.com/name/nm1868442/http://www.kasabian.co.uk/http://www.myspace.com/kasabianhttp://www.youtube.com/kasabianvevohttp://www.youtube.com/user/KasabianTour') } + + 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 diff --git a/test/helper.rb b/test/helper.rb index 1fba407..77ccbeb 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -24,5 +24,7 @@ $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