Class: Song
- Inherits:
-
Object
- Object
- Song
- Defined in:
- lib/top_100/song.rb
Constant Summary collapse
- @@songs =
[]
Instance Attribute Summary collapse
-
#artist_name ⇒ Object
Returns the value of attribute artist_name.
-
#artist_url ⇒ Object
Returns the value of attribute artist_url.
-
#name ⇒ Object
Returns the value of attribute name.
-
#rank ⇒ Object
Returns the value of attribute rank.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
-
.all ⇒ Object
class methods.
- .find_by_rank(rank) ⇒ Object
Instance Method Summary collapse
-
#initialize(song_hash) ⇒ Song
constructor
A new instance of Song.
-
#slug ⇒ Object
slug method used to help make query for spotify API request.
- #spotify_link ⇒ Object
Constructor Details
#initialize(song_hash) ⇒ Song
Returns a new instance of Song.
5 6 7 8 |
# File 'lib/top_100/song.rb', line 5 def initialize(song_hash) song_hash.each {|key, value| self.send("#{key}=", value)} @@songs << self end |
Instance Attribute Details
#artist_name ⇒ Object
Returns the value of attribute artist_name.
2 3 4 |
# File 'lib/top_100/song.rb', line 2 def artist_name @artist_name end |
#artist_url ⇒ Object
Returns the value of attribute artist_url.
2 3 4 |
# File 'lib/top_100/song.rb', line 2 def artist_url @artist_url end |
#name ⇒ Object
Returns the value of attribute name.
2 3 4 |
# File 'lib/top_100/song.rb', line 2 def name @name end |
#rank ⇒ Object
Returns the value of attribute rank.
2 3 4 |
# File 'lib/top_100/song.rb', line 2 def rank @rank end |
#url ⇒ Object
Returns the value of attribute url.
2 3 4 |
# File 'lib/top_100/song.rb', line 2 def url @url end |
Class Method Details
.all ⇒ Object
class methods
25 26 27 |
# File 'lib/top_100/song.rb', line 25 def self.all @@songs end |
Instance Method Details
#slug ⇒ Object
slug method used to help make query for spotify API request
11 12 13 |
# File 'lib/top_100/song.rb', line 11 def slug self.name.gsub(/\s+/, "%20").delete('^a-zA-Z0-9\%').downcase end |
#spotify_link ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/top_100/song.rb', line 15 def spotify_link response = open("https://api.spotify.com/v1/search?q=#{self.slug}&type=track").read json_info = JSON.parse(response) # songs share same track name, ensure that artist name includes the first artist to find a match. song_details = json_info["tracks"]["items"].find { |info| self.artist_name.downcase.include?(info["artists"][0]["name"].downcase) } song_details == nil ? nil : song_details["preview_url"] end |