lib/beatport/client.rb in beatport-0.1.10 vs lib/beatport/client.rb in beatport-0.2.0

- old
+ new

@@ -1,28 +1,48 @@ module Beatport module Client - include HTTParty - parser Beatport::Support::Parser - base_uri "http://api.beatport.com/catalog/3" - format :json -# default_params :v => '1.0', :format => 'json' + def self.client + @client ||= Signet::OAuth1::Client.new( + :client_credential_key => Beatport.consumer_key, + :client_credential_secret => Beatport.consumer_secret, + :token_credential_key => Beatport.access_token_key, + :token_credential_secret => Beatport.access_token_secret + ) + end + def self.builder + @builder ||= Support::QueryBuilder.new + end + def self.retrieve(path, klass, *args) - builder = Support::QueryBuilder.new + uri = Addressable::URI.new( + :scheme => 'https', + :host => 'api.beatport.com', + :path => "/catalog/3/#{path}", + :query_values => builder.process(*args) + ) - result = get("/#{path}", :query => builder.process(*args)) + result = client.fetch_protected_resource(:uri => uri.to_s) + result = JSON.parse(result.body) + # underscore result keys + result = Support::Inflector.process_keys(result) { |k| Support::Inflector.underscore(k) } + + if result['metadata']['error'] + raise Error.new("#{result['metadata']['error']}: #{result['metadata']['message']}") + end + case result['results'] when Array if builder.single_result? klass.new(result['results'].first) if result['results'].any? else Collection.new(klass, result) end when Hash klass.new(result['results']) else - raise "results is an unexpected class #{result['results'].class}" + raise Error.new("results is an unexpected class #{result['results'].class}") end end end end \ No newline at end of file