1
0
Fork 0

Encoding headers, wrapping eigenclasses

This commit is contained in:
Gregory Eremin 2012-07-09 02:37:46 +04:00
parent b97625597e
commit e3cd3341b7
13 changed files with 100 additions and 80 deletions

View File

@ -6,10 +6,8 @@ require "nokogiri"
require "cgi" require "cgi"
module MusicBrainz module MusicBrainz
module Tools module Tools; end
end module Parsers; end
module Parsers
end
end end
require "version" require "version"

View File

@ -17,7 +17,7 @@ module MusicBrainz
def load(params, query) def load(params, query)
parser = MusicBrainz::Parsers.get_by_name(params[:parser]) 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)) result = parser[:const].send(parser[:method], Nokogiri::XML(xml))
if params[:create_model] if params[:create_model]
result_model = params[:create_model].new result_model = params[:create_model].new

View File

@ -1,4 +1,5 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
module MusicBrainz module MusicBrainz
module Parsers module Parsers
class << self class << self

View File

@ -1,22 +1,24 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
module MusicBrainz module MusicBrainz
module Tools module Tools
class Cache class Cache
class << self
@@cache_path = nil @@cache_path = nil
def self.cache_path=(path) def cache_path=(path)
@@cache_path = path @@cache_path = path
end end
def self.cache_path def cache_path
@@cache_path @@cache_path
end end
def self.clear_cache def clear_cache
FileUtils.rm_r(@@cache_path) if @@cache_path && File.exist?(@@cache_path) FileUtils.rm_r(@@cache_path) if @@cache_path && File.exist?(@@cache_path)
end end
def self.cache_contents(url) def cache_contents(url)
response = nil response = nil
url_parts = url.split('/') url_parts = url.split('/')
file_name = url_parts.pop file_name = url_parts.pop
@ -42,4 +44,5 @@ module MusicBrainz
end end
end end
end end
end
end end

View File

@ -1,7 +1,9 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
module MusicBrainz module MusicBrainz
module Tools module Tools
class Proxy class Proxy
class << self
@@last_query_time = 0 @@last_query_time = 0
@@query_interval = 1.5 # Min: 1.0 Safe: 1.5 @@query_interval = 1.5 # Min: 1.0 Safe: 1.5
@@tries_limit = 5 @@tries_limit = 5
@ -9,19 +11,19 @@ module MusicBrainz
WEB_SERVICE_URL = 'http://musicbrainz.org/ws/2/' WEB_SERVICE_URL = 'http://musicbrainz.org/ws/2/'
USER_AGENT = "gem musicbrainz (https://github.com/magnolia-fan/musicbrainz) @ " + Socket.gethostname USER_AGENT = "gem musicbrainz (https://github.com/magnolia-fan/musicbrainz) @ " + Socket.gethostname
def self.query_interval=(sec) def query_interval=(sec)
@@query_interval = sec.to_f @@query_interval = sec.to_f
end end
def self.query_interval def query_interval
@@query_interval @@query_interval
end end
def self.tries_limit=(num) def tries_limit=(num)
@@tries_limit = num.to_i @@tries_limit = num.to_i
end end
def self.load(params = {}) def query(params = {})
url = WEB_SERVICE_URL + params[:resource].to_s.gsub('_', '-') + '/' + (params[:id].to_s || '') url = WEB_SERVICE_URL + params[:resource].to_s.gsub('_', '-') + '/' + (params[:id].to_s || '')
params.delete(:resource) params.delete(:resource)
params.delete(:id) unless params[:id].nil? params.delete(:id) unless params[:id].nil?
@ -31,11 +33,11 @@ module MusicBrainz
k + '=' + v k + '=' + v
}.join('&') unless params.empty? }.join('&') unless params.empty?
MusicBrainz::Tools::Cache.cache_contents(url) { MusicBrainz::Tools::Cache.cache_contents(url) {
self.get_contents url get_contents url
} }
end end
def self.get_contents(url) def get_contents(url)
response = nil response = nil
@@tries_limit.times { @@tries_limit.times {
@ -56,4 +58,5 @@ module MusicBrainz
end end
end end
end end
end
end end

View File

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

View File

@ -1,3 +1,5 @@
# -*- encoding: utf-8 -*-
require "spec_helper" require "spec_helper"
describe MusicBrainz do describe MusicBrainz do

View File

@ -1,3 +1,5 @@
# -*- encoding: utf-8 -*-
require "spec_helper" require "spec_helper"
describe MusicBrainz::Artist do describe MusicBrainz::Artist do

View File

@ -1,3 +1,5 @@
# -*- encoding: utf-8 -*-
require "spec_helper" require "spec_helper"
describe MusicBrainz::ReleaseGroup do describe MusicBrainz::ReleaseGroup do

View File

@ -1,3 +1,5 @@
# -*- encoding: utf-8 -*-
require "spec_helper" require "spec_helper"
describe MusicBrainz::Release do describe MusicBrainz::Release do

View File

@ -1,3 +1,5 @@
# -*- encoding: utf-8 -*-
require "spec_helper" require "spec_helper"
describe MusicBrainz::Track do describe MusicBrainz::Track do

View File

@ -1,3 +1,5 @@
# -*- encoding: utf-8 -*-
require "rubygems" require "rubygems"
require "bundler/setup" require "bundler/setup"
require "ap" require "ap"

View File

@ -1,3 +1,5 @@
# -*- encoding: utf-8 -*-
require "spec_helper" require "spec_helper"
describe MusicBrainz::Tools::Cache do describe MusicBrainz::Tools::Cache do