Class: SoundCloud::Client

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

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 {}
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
# File 'lib/soundcloud/client.rb', line 17

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.



14
15
16
# File 'lib/soundcloud/client.rb', line 14

def options
  @options
end

Instance Method Details

#access_tokenObject



64
65
66
# File 'lib/soundcloud/client.rb', line 64

def access_token
  @options[:access_token]
end

#api_hostObject



93
94
95
# File 'lib/soundcloud/client.rb', line 93

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

#authorize_url(options = {}) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/soundcloud/client.rb', line 97

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



56
57
58
# File 'lib/soundcloud/client.rb', line 56

def client_id
  @options[:client_id]
end

#client_secretObject



60
61
62
# File 'lib/soundcloud/client.rb', line 60

def client_secret
  @options[:client_secret]
end

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



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

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)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/soundcloud/client.rb', line 106

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)


80
81
82
# File 'lib/soundcloud/client.rb', line 80

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

#expires_atObject



76
77
78
# File 'lib/soundcloud/client.rb', line 76

def expires_at
  @options[:expires_at]
end

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



25
26
27
28
29
# File 'lib/soundcloud/client.rb', line 25

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

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



49
50
51
52
53
# File 'lib/soundcloud/client.rb', line 49

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

#on_exchange_token(&block) ⇒ Object



138
139
140
# File 'lib/soundcloud/client.rb', line 138

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

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



31
32
33
34
35
# File 'lib/soundcloud/client.rb', line 31

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



37
38
39
40
41
# File 'lib/soundcloud/client.rb', line 37

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

#redirect_uriObject



72
73
74
# File 'lib/soundcloud/client.rb', line 72

def redirect_uri
  @options[:redirect_uri]
end

#refresh_tokenObject



68
69
70
# File 'lib/soundcloud/client.rb', line 68

def refresh_token
  @options[:refresh_token]
end

#siteObject Also known as: host



88
89
90
# File 'lib/soundcloud/client.rb', line 88

def site
  @options[:site]
end

#use_ssl?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/soundcloud/client.rb', line 84

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