lib/rockstar/base.rb in rockstar-0.6.3 vs lib/rockstar/base.rb in rockstar-0.6.4

- old
+ new

@@ -1,39 +1,39 @@ -module Rockstar - +module Rockstar + API_URL = 'http://ws.audioscrobbler.com/' API_VERSION = '2.0' - + class Base class << self def connection @connection ||= REST::Connection.new(API_URL + API_VERSION + "/") end - + def fetch_and_parse(resource, params = {}, sign_request = false) Hpricot::XML(connection.get(resource, sign_request, params)) end - + def get_instance(api_method, instance_name, element, params, sign_request = false) rockstar_class = "rockstar/#{element.to_s}".camelize.constantize doc = self.fetch_and_parse(api_method, params, sign_request) (doc/element).inject([]) { |elements, el| elements << rockstar_class.new_from_xml(el, doc); elements } end end - + private # in order for subclass to use, it must have api_path method def get_instance(api_method, instance_name, element, params, force = false, sign_request = false) rockstar_class = "rockstar/#{element.to_s}".camelize.constantize - + if instance_variable_get("@#{instance_name}").nil? || force doc = self.class.fetch_and_parse(api_method, params, sign_request) elements = (doc/element).inject([]) { |elements, el| elements << rockstar_class.new_from_xml(el, doc); elements } instance_variable_set("@#{instance_name}", elements) end instance_variable_get("@#{instance_name}") end - + def self.fix_url(url) if (!url.blank? && !url.match("^http://")) url = "http://#{url}" end url