Class: SoundCloud::Client
- Inherits:
-
Object
- Object
- SoundCloud::Client
- 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
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #access_token ⇒ Object
- #api_host ⇒ Object
- #authorize_url(options = {}) ⇒ Object
-
#client_id ⇒ Object
accessors for options.
- #client_secret ⇒ Object
- #delete(path, query = {}, options = {}) ⇒ Object
- #exchange_token(options = {}) ⇒ Object
- #expired? ⇒ Boolean
- #expires_at ⇒ Object
- #get(path, query = {}, options = {}) ⇒ Object
- #head(path, query = {}, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #on_exchange_token(&block) ⇒ Object
- #post(path, body = {}, options = {}) ⇒ Object
- #put(path, body = {}, options = {}) ⇒ Object
- #redirect_uri ⇒ Object
- #refresh_token ⇒ Object
- #site ⇒ Object (also: #host)
- #use_ssl? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 22 23 |
# File 'lib/soundcloud/client.rb', line 17 def initialize(={}) () if access_token.nil? && ( || || ) 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
#options ⇒ Object
Returns the value of attribute options.
14 15 16 |
# File 'lib/soundcloud/client.rb', line 14 def @options end |
Instance Method Details
#access_token ⇒ Object
64 65 66 |
# File 'lib/soundcloud/client.rb', line 64 def access_token @options[:access_token] end |
#api_host ⇒ Object
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 (={}) additional_params = [:display, :state, :scope].map do |param_name| value = .delete(param_name) "#{param_name}=#{CGI.escape value}" unless value.nil? end.compact.join("&") () "https://#{host}#{AUTHORIZE_PATH}?response_type=code_and_token&client_id=#{client_id}&redirect_uri=#{URI.escape(redirect_uri)}&#{additional_params}" end |
#client_id ⇒ Object
accessors for options
56 57 58 |
# File 'lib/soundcloud/client.rb', line 56 def client_id @options[:client_id] end |
#client_secret ⇒ Object
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={}, ={}) handle_response { self.class.delete(*construct_query_arguments(path, .merge(:query => query))) } end |
#exchange_token(options = {}) ⇒ Object
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(={}) () 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 { :grant_type => 'refresh_token', :refresh_token => refresh_token, } elsif { :grant_type => 'password', :username => @options[:username], :password => @options[:password], } elsif { :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
80 81 82 |
# File 'lib/soundcloud/client.rb', line 80 def expired? (expires_at.nil? || expires_at < Time.now) end |
#expires_at ⇒ Object
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={}, ={}) handle_response { self.class.get(*construct_query_arguments(path, .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={}, ={}) handle_response { self.class.head(*construct_query_arguments(path, .merge(:query => query))) } end |
#on_exchange_token(&block) ⇒ Object
138 139 140 |
# File 'lib/soundcloud/client.rb', line 138 def on_exchange_token(&block) (: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={}, ={}) handle_response { self.class.post(*construct_query_arguments(path, .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={}, ={}) handle_response { self.class.put(*construct_query_arguments(path, .merge(:body => body), :body)) } end |
#redirect_uri ⇒ Object
72 73 74 |
# File 'lib/soundcloud/client.rb', line 72 def redirect_uri @options[:redirect_uri] end |
#refresh_token ⇒ Object
68 69 70 |
# File 'lib/soundcloud/client.rb', line 68 def refresh_token @options[:refresh_token] end |
#site ⇒ Object Also known as: host
88 89 90 |
# File 'lib/soundcloud/client.rb', line 88 def site @options[:site] end |
#use_ssl? ⇒ Boolean
84 85 86 |
# File 'lib/soundcloud/client.rb', line 84 def use_ssl? !!(@options[:use_ssl?] || access_token) end |