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

USER_AGENT =
"SoundCloud Ruby Wrapper #{VERSION}"
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.3.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Soundcloud

Returns a new instance of Soundcloud.

Raises:

  • (ArgumentError)


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

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.



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

def options
  @options
end

Instance Method Details

#access_tokenObject



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

def access_token;   @options[:access_token];  end

#api_hostObject



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

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

#authorize_url(options = {}) ⇒ Object



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

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



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

def client_id;      @options[:client_id];     end

#client_secretObject



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

def client_secret;  @options[:client_secret]; end

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



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

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)


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

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

#expired?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/soundcloud.rb', line 118

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

#expires_atObject



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

def expires_at;     @options[:expires_at];    end

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



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

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

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



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

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

#hostObject



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

def host; site; end

#on_exchange_token(&block) ⇒ Object



162
163
164
# File 'lib/soundcloud.rb', line 162

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

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



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

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



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

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

#redirect_uriObject



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

def redirect_uri;   @options[:redirect_uri];  end

#refresh_tokenObject



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

def refresh_token;  @options[:refresh_token]; end

#siteObject



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

def site; @options[:site]; end

#use_ssl?Boolean

Returns:

  • (Boolean)


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

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