Class: SoundCloud::Client

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

Constant Summary collapse

USER_AGENT =
"SoundCloud Ruby Wrapper #{SoundCloud::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)


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

def initialize(options={})
  store_options(options)
  if access_token.nil? && (options_for_refresh_flow_present? || options_for_code_flow_present? || options_for_credentials_flow_present?)
    exchange_token
  end
  raise ArgumentError, "At least a client_id, client_secret or an access_token must be present" if client_id.nil? && client_secret.nil? && access_token.nil?
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



16
17
18
# File 'lib/soundcloud/client.rb', line 16

def options
  @options
end

Instance Method Details

#access_tokenObject



66
67
68
# File 'lib/soundcloud/client.rb', line 66

def access_token
  @options[:access_token]
end

#api_hostObject



95
96
97
# File 'lib/soundcloud/client.rb', line 95

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

#authorize_url(options = {}) ⇒ Object



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

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://#{api_host}#{AUTHORIZE_PATH}?response_type=code&client_id=#{client_id}&redirect_uri=#{URI.escape(redirect_uri)}&#{additional_params}"
end

#client_idObject

accessors for options



58
59
60
# File 'lib/soundcloud/client.rb', line 58

def client_id
  @options[:client_id]
end

#client_secretObject



62
63
64
# File 'lib/soundcloud/client.rb', line 62

def client_secret
  @options[:client_secret]
end

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



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

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)


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
137
138
139
140
# File 'lib/soundcloud/client.rb', line 108

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?

  params = if options_for_code_flow_present?
    {
      :grant_type => 'authorization_code',
      :redirect_uri => redirect_uri,
      :code => @options[:code],
    }
  elsif options_for_refresh_flow_present?
    {
      :grant_type => 'refresh_token',
      :refresh_token => refresh_token,
    }
  elsif options_for_credentials_flow_present?
    {
      :grant_type => 'client_credentials'
    }
  end

  params.merge!(:client_id => client_id, :client_secret => client_secret)

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

#expired?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/soundcloud/client.rb', line 82

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

#expires_atObject



78
79
80
# File 'lib/soundcloud/client.rb', line 78

def expires_at
  @options[:expires_at]
end

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



27
28
29
30
31
# File 'lib/soundcloud/client.rb', line 27

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

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



51
52
53
54
55
# File 'lib/soundcloud/client.rb', line 51

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

#on_exchange_token(&block) ⇒ Object



142
143
144
# File 'lib/soundcloud/client.rb', line 142

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

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



33
34
35
36
37
# File 'lib/soundcloud/client.rb', line 33

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



39
40
41
42
43
# File 'lib/soundcloud/client.rb', line 39

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

#redirect_uriObject



74
75
76
# File 'lib/soundcloud/client.rb', line 74

def redirect_uri
  @options[:redirect_uri]
end

#refresh_tokenObject



70
71
72
# File 'lib/soundcloud/client.rb', line 70

def refresh_token
  @options[:refresh_token]
end

#siteObject Also known as: host



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

def site
  @options[:site]
end

#use_ssl?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/soundcloud/client.rb', line 86

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