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, UnauthorizedResponseError

Constant Summary collapse

CLIENT_ID_PARAM_NAME =
:client_id
API_SUBHOST =
'api'
AUTHORIZE_PATH =
'/connect'
TOKEN_PATH =
'/oauth2/token'
DEFAULT_OPTIONS =
{
  :site              => 'soundcloud.com',
  :on_exchange_token => lambda {}
}
VERSION =
'0.2.6'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Soundcloud

Returns a new instance of Soundcloud.

Raises:

  • (ArgumentError)


92
93
94
95
96
97
98
99
100
# File 'lib/soundcloud.rb', line 92

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 Attribute Details

#optionsObject

Returns the value of attribute options.



90
91
92
# File 'lib/soundcloud.rb', line 90

def options
  @options
end

Instance Method Details

#access_tokenObject



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

def access_token;   @options[:access_token];  end

#api_hostObject



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

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

#authorize_url(options = {}) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/soundcloud.rb', line 129

def authorize_url(options={})
  additional_params = [:display, :state, :scope].map do |param_name|
    value = options.delete(param_name)
    "#{param_name}=#{CGI.escape value}" unless value.nil?
  end.compact.join("&")

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

#client_idObject

accessors for options



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

def client_id;      @options[:client_id];     end

#client_secretObject



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

def client_secret;  @options[:client_secret]; end

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



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

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)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/soundcloud.rb', line 139

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)
  @options[:expires_at] = Time.now + response.expires_in
  @options[:on_exchange_token].call(*[(self if @options[:on_exchange_token].arity == 1)].compact)
  response
end

#expired?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/soundcloud.rb', line 116

def expired?
  (expires_at.nil? || expires_at < Time.now)
end

#expires_atObject



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

def expires_at;     @options[:expires_at];    end

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



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

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

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



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

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

#hostObject



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

def host; site; end

#on_exchange_token(&block) ⇒ Object



160
161
162
# File 'lib/soundcloud.rb', line 160

def on_exchange_token(&block)
  store_options(:on_exchange_token => block)
end

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



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

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

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



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

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

#redirect_uriObject



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

def redirect_uri;   @options[:redirect_uri];  end

#refresh_tokenObject



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

def refresh_token;  @options[:refresh_token]; end

#siteObject



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

def site; @options[:site]; end

#use_ssl?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/soundcloud.rb', line 120

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