Sha256: cddff77b70bdf7cc78d661359983815c4dc72c3d9c7d3784548b3d02d8b657e3

Contents?: true

Size: 1.55 KB

Versions: 4

Compression:

Stored size: 1.55 KB

Contents

module Beatport
  module Client
    def self.connection
      @connection ||= Faraday.new do |conn|
        conn.use Support::Middleware
        conn.adapter  Faraday.default_adapter  # make requests with Net::HTTP
      end
    end

    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.uri(path, args)
      Addressable::URI.new(
        :scheme => 'https',
        :host => 'oauth-api.beatport.com',
        :path => "/catalog/3/#{path}",
        :query_values => builder.process(*args)
      )
    end

    def self.retrieve(path, klass, *args)
      result = client.fetch_protected_resource(
        :connection => connection,
        :uri => uri(path, args).to_s
      ).body

      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

4 entries across 4 versions & 1 rubygems

Version Path
beatport-0.4.0 lib/beatport/client.rb
beatport-0.3.0 lib/beatport/client.rb
beatport-0.2.3 lib/beatport/client.rb
beatport-0.2.2 lib/beatport/client.rb