class MusicExplorer::API

Attributes

artist_data[RW]
artist_query[RW]
id[RW]
token[RW]

Public Class Methods

new(artist_query) click to toggle source
# File lib/music_explorer/api.rb, line 6
def initialize(artist_query)
  #initialize the API with credentials
  RSpotify.authenticate(ENV['CLIENT_ID'], ENV['CLIENT_SECRET'])
  #get the token necessary to look up info from API
  @token = RSpotify.client_token
  #store artist that will be looked up from API
  @artist_query = artist_query
  get_artist_id
  #methods for authentication with Spotify and setup will go here
end

Public Instance Methods

get_artist_id() click to toggle source
# File lib/music_explorer/api.rb, line 17
def get_artist_id
  #conduct the initial search, get the first result, and return the ID (necessary for other API calls)
  url = "#{@@base_url}search?q=#{@artist_query}&type=artist"
  search = retrieve_data_from_url(url)
  first_result = search["artists"]["items"][0]
  @id = first_result["id"]
end
retrieve_albums() click to toggle source
# File lib/music_explorer/api.rb, line 70
def retrieve_albums
  #Get all of artists's albums from API in form of array
  url = "#{@@base_url}artists/#{id}/albums?country=US"
  albums = retrieve_data_from_url(url)
  return_array_with_names(albums["items"])
end
retrieve_artist_data() click to toggle source
# File lib/music_explorer/api.rb, line 25
def retrieve_artist_data
  #calls other methods (which interact directly with API) to fill out hash of artist data
  self.artist_data = {}
  self.artist_data[:name] = retrieve_name
  self.artist_data[:genres] = retrieve_genres
  self.artist_data[:top_tracks] = retrieve_top_tracks
  self.artist_data[:albums] = retrieve_albums
  self.artist_data[:related_artists] = retrieve_related_artists
  self.artist_data
end
retrieve_data_from_url(url) click to toggle source
# File lib/music_explorer/api.rb, line 36
def retrieve_data_from_url(url)
  url = URI.parse(URI.escape(url))
  HTTParty.get(url, 
    {headers: {"Authorization" => "Bearer #{@token}"}}
  )
end
retrieve_genres() click to toggle source
# File lib/music_explorer/api.rb, line 57
def retrieve_genres
  url = "#{@@base_url}artists/#{id}"
  artist = retrieve_data_from_url(url)
  artist["genres"]
end
retrieve_name() click to toggle source
# File lib/music_explorer/api.rb, line 49
def retrieve_name
  #Get name of artist based on search from API
  #search for the artist
  url = "#{@@base_url}artists/#{id}"
  artist = retrieve_data_from_url(url)
  artist["name"]
end
retrieve_top_tracks() click to toggle source
# File lib/music_explorer/api.rb, line 63
def retrieve_top_tracks
  #Get top tracks for artists from API in form of arrayxw
  url = "#{@@base_url}artists/#{id}/top-tracks?country=US"
  top_tracks = retrieve_data_from_url(url)
  return_array_with_names(top_tracks["tracks"])
end
return_array_with_names(array) click to toggle source
# File lib/music_explorer/api.rb, line 43
def return_array_with_names(array)
  array.map do |item|
    item["name"]
  end
end