Class: Soundcloud

Inherits:
Object
  • Object
show all
Includes:
HTTMultiParty
Defined in:
lib/soundcloud.rb,
lib/soundcloud/version.rb

Defined Under Namespace

Classes: ArrayResponseWrapper, HashResponseWrapper, ResponseError

Constant Summary collapse

CLIENT_ID_PARAM_NAME =

TODO fix when api is ready for client_id

:consumer_key
API_SUBHOST =
'api'
AUTHORIZE_PATH =
'/connect'
TOKEN_PATH =
'/oauth2/token'
DEFAULT_OPTIONS =
{
  :site => 'soundcloud.com'
}
VERSION =
'0.1.8'

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Soundcloud

Returns a new instance of Soundcloud.

Raises:

  • (ArgumentError)


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

def initialize(options={})
  store_options(options)
  if access_token.nil? && (options_for_refresh_flow_present? ||
                           options_for_credentials_flow_present? || options_for_code_flow_present?)
    exchange_token
  end

  raise ArgumentError, "At least a client_id or an access_token must be present" if client_id.nil? && access_token.nil?
end

Instance Method Details

#access_tokenObject



41
# File 'lib/soundcloud.rb', line 41

def access_token;   @options[:access_token];  end

#api_hostObject



52
# File 'lib/soundcloud.rb', line 52

def api_host; [API_SUBHOST, host].join('.'); end

#authorize_url(options = {}) ⇒ Object



54
55
56
57
# File 'lib/soundcloud.rb', line 54

def authorize_url(options={})
  store_options(options)
  "https://#{host}#{AUTHORIZE_PATH}?response_type=code&client_id=#{client_id}&redirect_uri=#{URI.escape redirect_uri}"
end

#client_idObject

accessors for options



39
# File 'lib/soundcloud.rb', line 39

def client_id;      @options[:client_id];     end

#client_secretObject



40
# File 'lib/soundcloud.rb', line 40

def client_secret;  @options[:client_secret]; end

#delete(path, query = {}, options = {}) ⇒ Object



35
# File 'lib/soundcloud.rb', line 35

def delete(path, query={}, options={}); handle_response { self.class.delete *construct_query_arguments(path, options.merge(:query => query)) } end

#exchange_token(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/soundcloud.rb', line 59

def exchange_token(options={})
  store_options(options)
  raise ArgumentError, 'client_id and client_secret is required to retrieve an access_token' if client_id.nil? || client_secret.nil?
  client_params = {:client_id => client_id, :client_secret => client_secret}
  params = if options_for_refresh_flow_present?
    {:grant_type => 'refresh_token',      :refresh_token => refresh_token}
  elsif options_for_credentials_flow_present?
    {:grant_type => 'password',           :username      => @options[:username],     :password => @options[:password]}
  elsif options_for_code_flow_present?
    {:grant_type => 'authorization_code', :redirect_uri  => @options[:redirect_uri], :code => @options[:code]}
  end
  params.merge!(client_params)
  response = handle_response(false) {
    self.class.post("https://#{api_host}#{TOKEN_PATH}", :query => params)
  }
  @options.merge!(:access_token => response.access_token, :refresh_token => response.refresh_token)
  response
end

#get(path, query = {}, options = {}) ⇒ Object



32
# File 'lib/soundcloud.rb', line 32

def get   (path, query={}, options={}); handle_response { self.class.get    *construct_query_arguments(path, options.merge(:query => query)) } end

#head(path, query = {}, options = {}) ⇒ Object



36
# File 'lib/soundcloud.rb', line 36

def head  (path, query={}, options={}); handle_response { self.class.head   *construct_query_arguments(path, options.merge(:query => query)) } end

#hostObject



51
# File 'lib/soundcloud.rb', line 51

def host; site; end

#post(path, query = {}, options = {}) ⇒ Object



33
# File 'lib/soundcloud.rb', line 33

def post  (path, query={}, options={}); handle_response { self.class.post   *construct_query_arguments(path, options.merge(:query => query)) } end

#put(path, query = {}, options = {}) ⇒ Object



34
# File 'lib/soundcloud.rb', line 34

def put   (path, query={}, options={}); handle_response { self.class.put    *construct_query_arguments(path, options.merge(:query => query)) } end

#redirect_uriObject



43
# File 'lib/soundcloud.rb', line 43

def redirect_uri;   @options[:redirect_uri]; end

#refresh_tokenObject



42
# File 'lib/soundcloud.rb', line 42

def refresh_token;  @options[:refresh_token]; end

#siteObject



49
# File 'lib/soundcloud.rb', line 49

def site; @options[:site]; end

#use_ssl?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/soundcloud.rb', line 45

def use_ssl?; 
  !! @options[:use_ssl?] || access_token
end