Module: Beatport::Client

Defined in:
lib/beatport/client.rb

Class Method Summary collapse

Class Method Details

.builderObject



19
20
21
# File 'lib/beatport/client.rb', line 19

def self.builder
  @builder ||= Support::QueryBuilder.new
end

.clientObject



10
11
12
13
14
15
16
17
# File 'lib/beatport/client.rb', line 10

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

.connectionObject



3
4
5
6
7
8
# File 'lib/beatport/client.rb', line 3

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

.retrieve(path, klass, *args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/beatport/client.rb', line 32

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

.uri(path, args) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/beatport/client.rb', line 23

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