Code Monkey home page Code Monkey logo

soundcloud-ruby's Introduction

Soundcloud API Wrapper

Description

The Soundcloud gem is a thin wrapper for the Soundcloud API based of the httparty gem. It is providing simple methods to handle authorization and to execute HTTP calls.

Requirements

  • httmultiparty
  • httparty
  • crack
  • multipart-upload
  • hashie

Installation

gem install soundcloud

Examples

Print links of the 10 hottest tracks

# register a client with YOUR_CLIENT_ID as client_id_
client = Soundcloud.new(:client_id => YOUR_CLIENT_ID)
# get 10 hottest tracks
tracks = client.get('/tracks', :limit => 10, :order => 'hotness')
# print each link
tracks.each do |track|
  puts track.permalink_url
end

OAuth2 user credentials flow and print the username of the authenticated user

# register a new client, which will exchange the username, password for an access_token
# NOTE: the SoundCloud API Docs advices to not use the user credentials flow in a web app.
# In any case never store the password of a user.
client = Soundcloud.new({
  :client_id      => YOUR_CLIENT_ID,
  :client_secret  => YOUR_CLIENT_SECRET,
  :username       => '[email protected]',
  :password       => 'userpass'
})

# print logged in username
puts client.get('/me').username

OAuth2 authorization code flow

client = Soundcloud.new({
  :client_id      => YOUR_CLIENT_ID,
  :client_secret  => YOUR_CLIENT_SECRET,
})

client.authorize_url(:redirect_uri => REDIRECT_URI)
# => "https://soundcloud.com/connect?client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=http://host/redirect"
client.exchange_token(:redirect_uri => uri, :code => 'CODE')

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

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

# upload a new track with track.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

Resolve a track url and print its id

 # register the client
 client = Soundcloud.new(:client_id => YOUR_CLIENT_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

Register a client for http://sandbox-soundcloud.com with an existing access_token and start following a user

# register a client for http://sandbox-soundcloud.com with existing access_token
client = Soundcloud.new(:site => 'sandbox-soundcloud.com', :access_token => SOME_ACCESS_TOKEN)

# create a new following
user_id_to_follow = 123
client.put("/me/followings/#{user_id_to_follow}")

Interface

Soundcloud.new(options={})

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

Soundcloud#exchange_token(options={})

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

  • refresh_token, client_id and client_secret is present.
  • client_id, client_secret, username, password is present
  • client_id, client_secret, redirect_uri, code is present

Soundcloud#authorize_url(options={})

Stores the passed options except for state and display and return an authorize url. The client_id and redirect_uri options need to 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::Mashs. The mashs expose all resource attributes as methods and the original response through #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 raise. The original HTTParty response is available through Soundcloud::ResponseError#response.

soundcloud-ruby's People

Contributors

joshk avatar jwagener avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.