class Ejaydj::Spotify::Client
Constants
- ACCOUNT_API_URL
- API_URL
Public Class Methods
new(config={})
click to toggle source
# File lib/ejaydj/spotify/client.rb, line 12 def initialize(config={}) @rest_client = config[:rest_client] || RestClient @client_id = config[:client_id] @client_secret = config[:client_secret] end
Public Instance Methods
playlist_tracks(user_id: nil, playlist_id: nil)
click to toggle source
# File lib/ejaydj/spotify/client.rb, line 24 def playlist_tracks(user_id: nil, playlist_id: nil) url = "#{API_URL}/v1/users/#{user_id}/playlists/#{playlist_id}/tracks" response = get_request(url) fetch_items(response) end
user_playlists(user_id: nil)
click to toggle source
# File lib/ejaydj/spotify/client.rb, line 18 def user_playlists(user_id: nil) url = "#{API_URL}/v1/users/#{user_id}/playlists?limit=50" response = get_request(url) fetch_items(response) end
Private Instance Methods
fetch_items(response)
click to toggle source
# File lib/ejaydj/spotify/client.rb, line 32 def fetch_items(response) response_items = response["items"] next_url = response["next"] while next_url do response = get_request(next_url) response_items += response["items"] next_url = response["next"] end response_items end
get_access_token()
click to toggle source
# File lib/ejaydj/spotify/client.rb, line 54 def get_access_token encoded_client_id_secret = Base64.strict_encode64("#{@client_id}:#{@client_secret}") payload = {grant_type: 'client_credentials'} headers = {"Authorization" => "Basic #{encoded_client_id_secret}"} response = @rest_client.post("#{ACCOUNT_API_URL}/api/token", payload, headers) return JSON.parse(response)["access_token"] end
get_request(url)
click to toggle source
# File lib/ejaydj/spotify/client.rb, line 46 def get_request(url) JSON.parse(@rest_client.get(url, headers)) end
headers()
click to toggle source
# File lib/ejaydj/spotify/client.rb, line 50 def headers {"Authorization" => "Bearer #{get_access_token}"} end