lib/meta-spotify.rb in meta-spotify-0.3.0 vs lib/meta-spotify.rb in meta-spotify-0.3.1

- old
+ new

@@ -1,8 +1,9 @@ $:.unshift File.dirname(__FILE__) require 'httparty' +require 'uri' module MetaSpotify API_VERSION = '1' @@ -18,11 +19,14 @@ def self.search(string, opts={}) item_name = self.name.downcase.gsub(/^.*::/,'') query = {:q => string} query[:page] = opts[:page].to_s if opts.has_key? :page - result = get("/search/#{API_VERSION}/#{item_name}", :query => query, :format => :xml) + result = get("/search/#{API_VERSION}/#{item_name}", + :query => query, + :format => :xml, + :query_string_normalizer => self.method(:normalize)) raise_errors(result) result = result[item_name+'s'] items = [] unless result[item_name].nil? if result[item_name].is_a? Array @@ -85,12 +89,22 @@ raise NotFoundError.new('404 - That resource could not be found.') when 406 raise BadRequestError.new('406 - The requested format isn\'t available') when 500 raise ServerError.new('500 - The server encountered an unexpected problem') + when 502 + raise ServerError.new('502 - The API internally received a bad response') when 503 raise ServerError.new('503 - The API is temporarily unavailable') end + end + + def self.normalize(query) + stack = [] + query.each do |key, value| + stack.push "#{key}=#{URI.encode_www_form_component value}" + end + stack.join("&") end end class MetaSpotifyError < StandardError