Class: Soundcloud2000::Client

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

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.



10
11
12
# File 'lib/soundcloud2000/client.rb', line 10

def initialize(client_id)
  @client_id = client_id
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



8
9
10
# File 'lib/soundcloud2000/client.rb', line 8

def client_id
  @client_id
end

Instance Method Details

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



38
39
40
# File 'lib/soundcloud2000/client.rb', line 38

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

#location(url) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/soundcloud2000/client.rb', line 42

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

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



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

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



18
19
20
21
22
23
# File 'lib/soundcloud2000/client.rb', line 18

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

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



14
15
16
# File 'lib/soundcloud2000/client.rb', line 14

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

#uri_escape(params) ⇒ Object



25
26
27
# File 'lib/soundcloud2000/client.rb', line 25

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