Class: MusicExplorer::API

Inherits:
Object
  • Object
show all
Defined in:
lib/music_explorer/api.rb

Constant Summary collapse

@@base_url =
"https://api.spotify.com/v1/"

Instance Attribute Summary collapse

Instance Method Summary collapse

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_dataObject

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_queryObject

Returns the value of attribute artist_query.



2
3
4
# File 'lib/music_explorer/api.rb', line 2

def artist_query
  @artist_query
end

#idObject

Returns the value of attribute id.



2
3
4
# File 'lib/music_explorer/api.rb', line 2

def id
  @id
end

#tokenObject

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_idObject



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_albumsObject



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_dataObject



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] = retrieve_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_genresObject



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_nameObject



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


77
78
79
80
81
82
# File 'lib/music_explorer/api.rb', line 77

def retrieve_related_artists
  #Get related artists from API in form of array
  url = "#{@@base_url}artists/#{id}/related-artists?country=US"
  related_artists = retrieve_data_from_url(url)
  return_array_with_names(related_artists["artists"])
end

#retrieve_top_tracksObject



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