• Stars
    star
    5
  • Rank 2,861,937 (Top 57 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Official SoundCloud API Wrapper for Ruby.

⚠️⚠️DEPRECATED - NO LONGER MAINTAINED⚠️⚠️

This repository is no longer maintained by the SoundCloud team due to capacity constraints. We're instead focusing our efforts on improving the API & the developer platform. Please note the repo might be not in sync with the latest API changes.

We recommend the community to fork this repo in order to maintain the SDK. We'd be more than happy to make a reference on our developer portal that the developers can use different SDKs build by the community. In case you need to reach out to us, please head over to https://github.com/soundcloud/api/issues


SoundCloud API Wrapper

Build Status

Description

The official SoundCloud API wrapper. It provides simple methods to handle authorization and to execute HTTP calls.

Installation

gem install soundcloud

Examples

The following examples are for the latest gem version.

SoundCloud::VERSION
# => "0.3.7"

OAuth2 client credentials flow

# register a new client, which will exchange the client_id, client_secret for an access_token
client = SoundCloud.new({
  :client_id     => YOUR_CLIENT_ID,
  :client_secret => YOUR_CLIENT_SECRET,
})

OAuth2 authorization code flow

# register a new client, providing the client_id, client_secret and redirect_uri
client = SoundCloud.new({
  :client_id     => YOUR_CLIENT_ID,
  :client_secret => YOUR_CLIENT_SECRET,
  :redirect_uri  => YOUR_REDIRECT_URI,
})

redirect client.authorize_url()
# the user should be redirected to "https://soundcloud.com/connect?client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REDIRECT_URI"
# after granting access they will be redirected back to YOUR_REDIRECT_URI with an authorization code present
# ex: <YOUR_REDIRECT_URI>?code=XXX
# in your respective handler you can build an exchange token from the transmitted code
client.exchange_token(:code => params[:code])

OAuth2 refresh token flow, upload a track and print its link

# register a new client and exchange an existing refresh_token for an access_token
# note: refresh_token can also be acquired from authorization code/client_credentials flows.
# use this flow to automatically update the token when it expires.
client = SoundCloud.new({
  :client_id     => YOUR_CLIENT_ID,
  :client_secret => YOUR_CLIENT_SECRET,
  :refresh_token => SOME_REFRESH_TOKEN
})

# upload a new track with audio.mp3 as audio and image.jpg as artwork
track = client.post('/tracks', :track => {
  :title      => 'a new track',
  :asset_data => File.new('audio.mp3')
})

# print new tracks link
puts track.permalink_url

Print links of the 10 most recent tracks

# get newest tracks
tracks = client.get('/tracks', :limit => 10)
# print each link
tracks.each do |track|
  puts track.permalink_url
end

Resolve a track url and print its id

# call the resolve endpoint with a track url
track = client.get('/resolve', :url => "http://soundcloud.com/forss/flickermood")

# print the track id
puts track.id

Initializing a client with an access token and updating the users profile description

# initializing a client with an access token
client = SoundCloud.new(:access_token => A_VALID_TOKEN)

# updating the users profile description
client.put("/me", :user => {:description => "a new description"})

Add a track to a playlist / set

# get my last playlist
playlist = client.get("/me/playlists").first

# get ids of contained tracks
track_ids = playlist.tracks.map(&:id) # => [22448500, 21928809]

# adding a new track 21778201
track_ids << 21778201 # => [22448500, 21928809, 21778201]

# map array of ids to array of track objects:
tracks = track_ids.map{|id| {:id => id}} # => [{:id=>22448500}, {:id=>21928809}, {:id=>21778201}]

# send update/put request to playlist
playlist = client.put(playlist.uri, :playlist => {
  :tracks => tracks
})

# print the list of track ids of the updated playlist:
p playlist.tracks.map(&:id)

Interface

SoundCloud.new(options={})

Stores the passed options and calls exchange_token in case all options are passed that allow an exchange of tokens.

SoundCloud#exchange_token(options={})

Stores the passed options and tries to exchange tokens if no access_token is present and:

  • client_id, client_secret is present (client credentials flow).
  • refresh_token, client_id and client_secret is present (refresh token flow).
  • client_id, client_secret, redirect_uri, and code is present (authorization code flow).

SoundCloud#authorize_url(options={})

Stores the passed options except for state and display and returns an authorize url (a part of authorization code flow). The client_id and redirect_uri options has to be present to generate the authorize url. The state and display options can be used to set the parameters accordingly in the authorize url.

SoundCloud#get, SoundCloud#post, SoundCloud#put, SoundCloud#delete, SoundCloud#head

These methods expose all available HTTP methods. They all share the signature (path_or_uri, query={}, options={}). The query hash will be merged with the options hash and passed to httparty. Depending on if the client is authorized it will either add the client_id or the access_token as a query parameter. In case an access_token is expired and a refresh_token, client_id and client_secret is present it will try to refresh the access_token and retry the call. The response is either a Hashie::Mash or an array of Hashie::Mashes. The mashes expose all resource attributes as methods and the original response through HashResponseWrapper#response.

SoundCloud#client_id, client_secret, access_token, refresh_token, use_ssl?

These methods are accessors for the stored options.

SoundCloud#on_exchange_token

A Proc passed to on_exchange_token will be called each time a token was successfully exchanged or refreshed

SoundCloud#expires_at

Returns a date based on the expires_in attribute returned from a token exchange.

SoundCloud#expired?

Will return true or false depending on if expires_at is in the past.

Error Handling

In case a request was not successful a SoundCloud::ResponseError will be raised. The original HTTParty response is available through SoundCloud::ResponseError#response.

Documentation

For more code examples, please visit the SoundCloud API Documentation.

More Repositories

1

roshi

Roshi is a large-scale CRDT set implementation for timestamped events.
Go
3,107
star
2

lhm

Online MySQL schema migrations
Ruby
1,808
star
3

lightcycle

LightCycle lets self-contained classes respond to Android’s lifecycle events
Java
706
star
4

soundcloud-custom-player

The SoundCloud custom javascript based player
JavaScript
699
star
5

chunk-manifest-webpack-plugin

Allows exporting a manifest that maps entry chunk names to their output files, instead of keeping the mapping inside the webpack bootstrap.
JavaScript
393
star
6

soundcloud-javascript

Official SoundCloud Javascript SDK
JavaScript
382
star
7

areweplayingyet

html5 audio benchmarks
JavaScript
312
star
8

cosine-lsh-join-spark

Approximate Nearest Neighbors in Spark
Scala
175
star
9

Axt

SwiftUI view testing library
Swift
163
star
10

Widget-JS-API

This is the official SoundCloud Widget Javascript API
JavaScript
149
star
11

delect

The Gradle Plugin for Dagger Reflect.
Kotlin
137
star
12

api

A public repo for our Developer Community to engage about bugs and feature requests on our Public API
136
star
13

periskop

Exception Monitoring Service
Go
123
star
14

project-dev-kpis

Key Performance Indicators of product development teams.
Python
119
star
15

soundcloud-python

A Python wrapper around the Soundcloud API
Python
95
star
16

split-by-name-webpack-plugin

Split a Webpack entry bundle into any number of arbitrarily defined smaller bundles
JavaScript
80
star
17

spark-pagerank

PageRank in Spark
Scala
74
star
18

intervene

A machine-in-the-middle proxy for development, enabling mocking and/or modification of API endpoints
JavaScript
71
star
19

normailize

Normalize emails like [email protected] into [email protected]
Ruby
67
star
20

SoundCloud-API-jQuery-plugin

SoundCloud API jQuery plugin
JavaScript
52
star
21

spdt

Streaming Parallel Decision Tree
Scala
51
star
22

twinagle

Twinagle = Twirp + Finagle
Scala
50
star
23

prometheus-clj

Clojure wrappers for the Prometheus java client
Clojure
49
star
24

simple_circuit_breaker

Simple Ruby implementation of the Circuit Breaker design pattern
Ruby
28
star
25

git-sha-webpack-plugin

Tag your webpack bundles with a Git SHA linked to the latest commit on that bundle
JavaScript
27
star
26

remixin

Mixin library for Javascript
JavaScript
24
star
27

cando

A simple access rights gem with users, roles and capabilities
Ruby
22
star
28

move-to-parent-merging-webpack-plugin

JavaScript
19
star
29

MinimalPerfectHashes.jl

An implementation of minimal perfect hash function generation as described in Czech et. al. 1992.
Julia
16
star
30

ogg

Mirror of http://svn.xiph.org/trunk/ogg/
C
11
star
31

sc-gaws

Glue code to wrap around AWS and do useful things in Go
Go
9
star
32

vorbis

Mirror of http://svn.xiph.org/trunk/vorbis/
C
8
star
33

collins_exporter

Simple Collins exporter for Prometheus
Go
8
star
34

dns-endpoint-pool

Manage and load-balance a pool of service endpoints retrieved from a DNS lookup for a service discovery name.
JavaScript
7
star
35

tremor

Mirror of http://svn.xiph.org/trunk/Tremor
C
5
star
36

periskop-scala

Scala low level client for Periskop
Scala
3
star
37

knife-scrub

Knife plugin to scrub normal attributes
Ruby
1
star
38

go-runit

go library wrapping runit service status
Go
1
star