diff --git a/lib/musicbrainz.rb b/lib/musicbrainz.rb index 782d5f2..e0a7e98 100644 --- a/lib/musicbrainz.rb +++ b/lib/musicbrainz.rb @@ -6,10 +6,8 @@ require "nokogiri" require "cgi" module MusicBrainz - module Tools - end - module Parsers - end + module Tools; end + module Parsers; end end require "version" diff --git a/lib/musicbrainz/base.rb b/lib/musicbrainz/base.rb index 34a6485..838fbeb 100644 --- a/lib/musicbrainz/base.rb +++ b/lib/musicbrainz/base.rb @@ -17,7 +17,7 @@ module MusicBrainz def load(params, query) parser = MusicBrainz::Parsers.get_by_name(params[:parser]) - xml = MusicBrainz::Tools::Proxy.load(query) + xml = MusicBrainz::Tools::Proxy.query(query) result = parser[:const].send(parser[:method], Nokogiri::XML(xml)) if params[:create_model] result_model = params[:create_model].new diff --git a/lib/parsers/base.rb b/lib/parsers/base.rb index 010a61a..54ab85a 100644 --- a/lib/parsers/base.rb +++ b/lib/parsers/base.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- + module MusicBrainz module Parsers class << self diff --git a/lib/tools/cache.rb b/lib/tools/cache.rb index ddb051c..bdffb1d 100644 --- a/lib/tools/cache.rb +++ b/lib/tools/cache.rb @@ -1,44 +1,47 @@ # -*- encoding: utf-8 -*- + module MusicBrainz module Tools class Cache - @@cache_path = nil + class << self + @@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 + def cache_path=(path) + @@cache_path = path end - response + def cache_path + @@cache_path + end + + def clear_cache + FileUtils.rm_r(@@cache_path) if @@cache_path && File.exist?(@@cache_path) + end + + def 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 diff --git a/lib/tools/proxy.rb b/lib/tools/proxy.rb index 8321305..4841123 100644 --- a/lib/tools/proxy.rb +++ b/lib/tools/proxy.rb @@ -1,58 +1,61 @@ # -*- 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 + class << self + @@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 + 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 query_interval=(sec) + @@query_interval = sec.to_f + end - def self.query_interval - @@query_interval - end + def query_interval + @@query_interval + end - def self.tries_limit=(num) - @@tries_limit = num.to_i - end + def tries_limit=(num) + @@tries_limit = num.to_i + end - def self.load(params = {}) - url = WEB_SERVICE_URL + params[:resource].to_s.gsub('_', '-') + '/' + (params[:id].to_s || '') - params.delete(:resource) - 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 query(params = {}) + url = WEB_SERVICE_URL + params[:resource].to_s.gsub('_', '-') + '/' + (params[:id].to_s || '') + params.delete(:resource) + 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) { + get_contents url + } + end - def self.get_contents(url) - response = nil + def 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 + @@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 + 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? - } + break unless response.nil? + } - response + response + end end end end diff --git a/lib/version.rb b/lib/version.rb index dc638d3..0212c5d 100644 --- a/lib/version.rb +++ b/lib/version.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- + module MusicBrainz VERSION = "0.6.0" end diff --git a/spec/misc/deprecated_spec.rb b/spec/misc/deprecated_spec.rb index 50f5fee..5d9f5a6 100644 --- a/spec/misc/deprecated_spec.rb +++ b/spec/misc/deprecated_spec.rb @@ -1,3 +1,5 @@ +# -*- encoding: utf-8 -*- + require "spec_helper" describe MusicBrainz do diff --git a/spec/requests/artist_spec.rb b/spec/requests/artist_spec.rb index 4a74817..127c9ba 100644 --- a/spec/requests/artist_spec.rb +++ b/spec/requests/artist_spec.rb @@ -1,3 +1,5 @@ +# -*- encoding: utf-8 -*- + require "spec_helper" describe MusicBrainz::Artist do diff --git a/spec/requests/release_group_spec.rb b/spec/requests/release_group_spec.rb index f9e3839..07cd2a2 100644 --- a/spec/requests/release_group_spec.rb +++ b/spec/requests/release_group_spec.rb @@ -1,3 +1,5 @@ +# -*- encoding: utf-8 -*- + require "spec_helper" describe MusicBrainz::ReleaseGroup do diff --git a/spec/requests/release_spec.rb b/spec/requests/release_spec.rb index c1d92bf..584e827 100644 --- a/spec/requests/release_spec.rb +++ b/spec/requests/release_spec.rb @@ -1,3 +1,5 @@ +# -*- encoding: utf-8 -*- + require "spec_helper" describe MusicBrainz::Release do diff --git a/spec/requests/track_spec.rb b/spec/requests/track_spec.rb index 4eb758f..3fb92dd 100644 --- a/spec/requests/track_spec.rb +++ b/spec/requests/track_spec.rb @@ -1,3 +1,5 @@ +# -*- encoding: utf-8 -*- + require "spec_helper" describe MusicBrainz::Track do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 16fa79a..078289b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ +# -*- encoding: utf-8 -*- + require "rubygems" require "bundler/setup" require "ap" diff --git a/spec/tools/cache_spec.rb b/spec/tools/cache_spec.rb index 73e810e..7d1348b 100644 --- a/spec/tools/cache_spec.rb +++ b/spec/tools/cache_spec.rb @@ -1,3 +1,5 @@ +# -*- encoding: utf-8 -*- + require "spec_helper" describe MusicBrainz::Tools::Cache do