module RSpotify
Constants
- API_URI
- AUTHORIZE_URI
- TOKEN_URI
- VERBS
- VERSION
Attributes
client_token[R]
raw_response[RW]
Public Class Methods
authenticate(client_id, client_secret)
click to toggle source
Authenticates access to restricted data. Requires {developer.spotify.com/my-applications user credentials}
@param client_id [String] @param client_secret [String]
@example
RSpotify.authenticate("<your_client_id>", "<your_client_secret>") playlist = RSpotify::Playlist.find('wizzler', '00wHcTN0zQiun4xri9pmvX') playlist.name #=> "Movie Soundtrack Masterpieces"
# File lib/rspotify/connection.rb, line 28 def authenticate(client_id, client_secret) @client_id, @client_secret = client_id, client_secret request_body = { grant_type: 'client_credentials' } response = RestClient.post(TOKEN_URI, request_body, auth_header) @client_token = JSON.parse(response)['access_token'] true end
resolve_auth_request(user_id, url)
click to toggle source
# File lib/rspotify/connection.rb, line 43 def resolve_auth_request(user_id, url) users_credentials = if User.class_variable_defined?('@@users_credentials') User.class_variable_get('@@users_credentials') end if users_credentials && users_credentials[user_id] User.oauth_get(user_id, url) else get(url) end end
Private Class Methods
auth_header()
click to toggle source
# File lib/rspotify/connection.rb, line 111 def auth_header authorization = Base64.strict_encode64("#{@client_id}:#{@client_secret}") { 'Authorization' => "Basic #{authorization}" } end
get_headers(params)
click to toggle source
# File lib/rspotify/connection.rb, line 116 def get_headers(params) params.find{|param| param.is_a?(Hash) && param['Authorization']} end
request_was_user_authenticated?(*params)
click to toggle source
# File lib/rspotify/connection.rb, line 94 def request_was_user_authenticated?(*params) users_credentials = if User.class_variable_defined?('@@users_credentials') User.class_variable_get('@@users_credentials') end headers = get_headers(params) if users_credentials creds = users_credentials.map{|_user_id, creds| "Bearer #{creds['token']}"} if creds.include?(headers['Authorization']) return true end end false end
retry_connection(verb, url, params)
click to toggle source
Added this method for testing
# File lib/rspotify/connection.rb, line 90 def retry_connection(verb, url, params) RestClient.send(verb, url, *params) end
send_request(verb, path, *params)
click to toggle source
# File lib/rspotify/connection.rb, line 57 def send_request(verb, path, *params) url = path.start_with?('http') ? path : API_URI + path url, query = *url.split('?') url = Addressable::URI.encode(url) url << "?#{query}" if query begin headers = get_headers(params) headers['Accept-Language'] = ENV['ACCEPT_LANGUAGE'] if ENV['ACCEPT_LANGUAGE'] response = RestClient.send(verb, url, *params) rescue RestClient::Unauthorized => e raise e if request_was_user_authenticated?(*params) raise MissingAuthentication unless @client_id && @client_secret authenticate(@client_id, @client_secret) headers = get_headers(params) headers['Authorization'] = "Bearer #{@client_token}" response = retry_connection(verb, url, params) end return response if raw_response begin JSON.parse(response) unless response.nil? || response.empty? rescue JSON::ParserError, TypeError => e response.to_s # Fall back to raw body when the response is not actually a valid JSON response end end