Sha256: 513ee012887bb799ccd4c4d2bcf53c600bf4d42e481c1e93555e00c0f843be1e

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

module Beatport
  module Client
    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)
      uri = Addressable::URI.new(
        :scheme => 'https',
        :host => 'api.beatport.com',
        :path => "/catalog/3/#{path}",
        :query_values => 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 Error.new("results is an unexpected class #{result['results'].class}")
      end
    end 
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
beatport-0.2.0 lib/beatport/client.rb