Class: MusicExplorer::API
- Inherits:
-
Object
- Object
- MusicExplorer::API
- Defined in:
- lib/music_explorer/api.rb
Constant Summary collapse
- @@base_url =
"https://api.spotify.com/v1/"
Instance Attribute Summary collapse
-
#artist_data ⇒ Object
Returns the value of attribute artist_data.
-
#artist_query ⇒ Object
Returns the value of attribute artist_query.
-
#id ⇒ Object
Returns the value of attribute id.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #get_artist_id ⇒ Object
-
#initialize(artist_query) ⇒ API
constructor
A new instance of API.
- #retrieve_albums ⇒ Object
- #retrieve_artist_data ⇒ Object
- #retrieve_data_from_url(url) ⇒ Object
- #retrieve_genres ⇒ Object
- #retrieve_name ⇒ Object
- #retrieve_related_artists ⇒ Object
- #retrieve_top_tracks ⇒ Object
- #return_array_with_names(array) ⇒ Object
Constructor Details
#initialize(artist_query) ⇒ API
Returns a new instance of API.
6 7 8 9 10 11 12 13 14 15 |
# 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 |
Instance Attribute Details
#artist_data ⇒ Object
Returns the value of attribute artist_data.
2 3 4 |
# File 'lib/music_explorer/api.rb', line 2 def artist_data @artist_data end |
#artist_query ⇒ Object
Returns the value of attribute artist_query.
2 3 4 |
# File 'lib/music_explorer/api.rb', line 2 def artist_query @artist_query end |
#id ⇒ Object
Returns the value of attribute id.
2 3 4 |
# File 'lib/music_explorer/api.rb', line 2 def id @id end |
#token ⇒ Object
Returns the value of attribute token.
2 3 4 |
# File 'lib/music_explorer/api.rb', line 2 def token @token end |
Instance Method Details
#get_artist_id ⇒ Object
17 18 19 20 21 22 23 |
# 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 ⇒ Object
70 71 72 73 74 75 |
# 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 ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# 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] = self.artist_data end |
#retrieve_data_from_url(url) ⇒ Object
36 37 38 39 40 41 |
# 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 ⇒ Object
57 58 59 60 61 |
# 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 ⇒ Object
49 50 51 52 53 54 55 |
# 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_related_artists ⇒ Object
77 78 79 80 81 82 |
# File 'lib/music_explorer/api.rb', line 77 def #Get related artists from API in form of array url = "#{@@base_url}artists/#{id}/related-artists?country=US" = retrieve_data_from_url(url) return_array_with_names(["artists"]) end |
#retrieve_top_tracks ⇒ Object
63 64 65 66 67 68 |
# 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) ⇒ Object
43 44 45 46 47 |
# File 'lib/music_explorer/api.rb', line 43 def return_array_with_names(array) array.map do |item| item["name"] end end |