1
0
Fork 0

Merge pull request #3 from Applicat/caching

Caching of http requests
This commit is contained in:
Gregory Eremin 2012-07-04 01:36:18 -07:00
commit ccbc869250
4 changed files with 103 additions and 7 deletions

View File

@ -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

View File

@ -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.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
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
response = nil if e.io.status[0].to_i == 404
end
break unless response.nil?
end
response
end
end

View File

@ -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('<?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

@ -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