Class: SoundCloud::Client
- Inherits:
-
Object
- Object
- SoundCloud::Client
- 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
-
#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.
19 20 21 22 23 24 25 |
# File 'lib/soundcloud/client.rb', line 19 def initialize(={}) () if access_token.nil? && ( || || ) 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
#options ⇒ Object
Returns the value of attribute options.
16 17 18 |
# File 'lib/soundcloud/client.rb', line 16 def @options end |
Instance Method Details
#access_token ⇒ Object
66 67 68 |
# File 'lib/soundcloud/client.rb', line 66 def access_token @options[:access_token] end |
#api_host ⇒ Object
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 (={}) 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://#{api_host}#{AUTHORIZE_PATH}?response_type=code&client_id=#{client_id}&redirect_uri=#{URI.escape(redirect_uri)}&#{additional_params}" end |
#client_id ⇒ Object
accessors for options
58 59 60 |
# File 'lib/soundcloud/client.rb', line 58 def client_id @options[:client_id] end |
#client_secret ⇒ Object
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={}, ={}) handle_response { self.class.delete(*construct_query_arguments(path, .merge(:query => query))) } end |
#exchange_token(options = {}) ⇒ Object
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(={}) () raise ArgumentError, 'client_id and client_secret is required to retrieve an access_token' if client_id.nil? || client_secret.nil? params = if { :grant_type => 'authorization_code', :redirect_uri => redirect_uri, :code => @options[:code], } elsif { :grant_type => 'refresh_token', :refresh_token => refresh_token, } elsif { :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
82 83 84 |
# File 'lib/soundcloud/client.rb', line 82 def expired? (expires_at.nil? || expires_at < Time.now) end |
#expires_at ⇒ Object
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={}, ={}) handle_response { self.class.get(*construct_query_arguments(path, .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={}, ={}) handle_response { self.class.head(*construct_query_arguments(path, .merge(:query => query))) } end |
#on_exchange_token(&block) ⇒ Object
142 143 144 |
# File 'lib/soundcloud/client.rb', line 142 def on_exchange_token(&block) (: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={}, ={}) handle_response { self.class.post(*construct_query_arguments(path, .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={}, ={}) handle_response { self.class.put(*construct_query_arguments(path, .merge(:body => body), :body)) } end |
#redirect_uri ⇒ Object
74 75 76 |
# File 'lib/soundcloud/client.rb', line 74 def redirect_uri @options[:redirect_uri] end |
#refresh_token ⇒ Object
70 71 72 |
# File 'lib/soundcloud/client.rb', line 70 def refresh_token @options[:refresh_token] end |
#site ⇒ Object Also known as: host
90 91 92 |
# File 'lib/soundcloud/client.rb', line 90 def site @options[:site] end |
#use_ssl? ⇒ Boolean
86 87 88 |
# File 'lib/soundcloud/client.rb', line 86 def use_ssl? !!(@options[:use_ssl?] || access_token) end |