Class: Soundcloud9000::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/soundcloud9000/client.rb

Overview

responsible for the very basic information of the app

Constant Summary collapse

DEFAULT_LIMIT =
50

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id) ⇒ Client

Returns a new instance of Client.



12
13
14
15
# File 'lib/soundcloud9000/client.rb', line 12

def initialize(client_id)
  @client_id = client_id
  @current_user = nil
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



9
10
11
# File 'lib/soundcloud9000/client.rb', line 9

def client_id
  @client_id
end

#current_userObject

Returns the value of attribute current_user.



9
10
11
# File 'lib/soundcloud9000/client.rb', line 9

def current_user
  @current_user
end

Instance Method Details

#get(path, params = {}) ⇒ Object



39
40
41
# File 'lib/soundcloud9000/client.rb', line 39

def get(path, params = {})
  JSON.parse(request(Net::HTTP::Get, path, params).body)
end

#location(url) ⇒ Object



43
44
45
46
47
# File 'lib/soundcloud9000/client.rb', line 43

def location(url)
  uri = URI.parse(url)
  res = request(Net::HTTP::Get, uri.path)
  res.header['Location'] if res.code == '302'
end

#request(type, path, params = {}) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/soundcloud9000/client.rb', line 30

def request(type, path, params = {})
  params[:client_id] = client_id
  params[:format] = 'json'

  Net::HTTP.start('api.soundcloud.com', 443, use_ssl: true) do |http|
    http.request(type.new("#{path}?#{uri_escape params}"))
  end
end

#resolve(permalink) ⇒ Object



21
22
23
24
# File 'lib/soundcloud9000/client.rb', line 21

def resolve(permalink)
  res = get('/resolve', url: "http://soundcloud.com/#{permalink}")
  get URI.parse(res['location']).path if res['location']
end

#tracks(page = 1, limit = DEFAULT_LIMIT) ⇒ Object



17
18
19
# File 'lib/soundcloud9000/client.rb', line 17

def tracks(page = 1, limit = DEFAULT_LIMIT)
  get('/tracks', offset: (page - 1) * limit, limit: limit)
end

#uri_escape(params) ⇒ Object



26
27
28
# File 'lib/soundcloud9000/client.rb', line 26

def uri_escape(params)
  URI.escape(params.collect { |k, v| "#{k}=#{v}" }.join('&'))
end