Class: RSpotify::Album

Inherits:
Base
  • Object
show all
Defined in:
lib/rspotify/album.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#external_urls, #href, #id, #type, #uri

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#complete!, #embed, #method_missing, #respond_to?

Constructor Details

#initialize(options = {}) ⇒ Album



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rspotify/album.rb', line 74

def initialize(options = {})
  @album_type             = options['album_type']
  @available_markets      = options['available_markets']
  @copyrights             = options['copyrights']
  @external_ids           = options['external_ids']
  @genres                 = options['genres']
  @images                 = options['images']
  @label                  = options['label']
  @name                   = options['name']
  @popularity             = options['popularity']
  @release_date           = options['release_date']
  @release_date_precision = options['release_date_precision']

  @artists = if options['artists']
    options['artists'].map { |a| Artist.new a }
  end

  @tracks_cache, @total_tracks = if options['tracks'] && options['tracks']['items']
    [
      options['tracks']['items'].map { |i| Track.new i },
      options['tracks']['total']
    ]
  end

  super(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RSpotify::Base

Instance Attribute Details

#album_typeString

The type of the album (album, single, compilation)



16
17
18
# File 'lib/rspotify/album.rb', line 16

def album_type
  @album_type
end

#artistsArray<Artist>

The artists of the album



16
17
18
# File 'lib/rspotify/album.rb', line 16

def artists
  @artists
end

#available_marketsArray<String>

The markets in which the album is available. See ISO 3166-1 alpha-2 country codes



16
17
18
# File 'lib/rspotify/album.rb', line 16

def available_markets
  @available_markets
end

#copyrightsArray<Hash>

The copyright statements of the album



16
17
18
# File 'lib/rspotify/album.rb', line 16

def copyrights
  @copyrights
end

#external_idsHash

Known external IDs for the album



16
17
18
# File 'lib/rspotify/album.rb', line 16

def external_ids
  @external_ids
end

#genresArray<String>

A list of the genres used to classify the album. If not yet classified, the array is empty



16
17
18
# File 'lib/rspotify/album.rb', line 16

def genres
  @genres
end

#imagesArray<Hash>

The cover art for the album in various sizes, widest first



16
17
18
# File 'lib/rspotify/album.rb', line 16

def images
  @images
end

#labelString

The label for the album



16
17
18
# File 'lib/rspotify/album.rb', line 16

def label
  @label
end

#nameString

The name of the album



16
17
18
# File 'lib/rspotify/album.rb', line 16

def name
  @name
end

#popularityInteger

The popularity of the album. The value will be between 0 and 100, with 100 being the most popular



16
17
18
# File 'lib/rspotify/album.rb', line 16

def popularity
  @popularity
end

#release_dateString

The date the album was first released, for example “1981-12-15”. Depending on the precision, it might be shown as “1981” or “1981-12”



16
17
18
# File 'lib/rspotify/album.rb', line 16

def release_date
  @release_date
end

#release_date_precisionString

The precision with which release_date value is known: “year”, “month”, or “day”



16
17
18
# File 'lib/rspotify/album.rb', line 16

def release_date_precision
  @release_date_precision
end

#total_tracksInteger

The total number of tracks in the album



16
17
18
# File 'lib/rspotify/album.rb', line 16

def total_tracks
  @total_tracks
end

Class Method Details

.find(ids, market: nil) ⇒ Album+

Returns Album object(s) with id(s) provided

Examples:

album = RSpotify::Album.find('41vPD50kQ7JeamkxQW7Vuy')
album.class #=> RSpotify::Album
album.name  #=> "AM"

ids = %w(41vPD50kQ7JeamkxQW7Vuy 4jKGRliQXa5VwxKOsiCbfL)
albums = RSpotify::Album.find(ids)
albums.class       #=> Array
albums.first.class #=> RSpotify::Album


33
34
35
# File 'lib/rspotify/album.rb', line 33

def self.find(ids, market: nil)
  super(ids, 'album', market: market)
end

.new_releases(limit: 20, offset: 0, country: nil) ⇒ Array<Album>

Get a list of new album releases featured in Spotify (shown, for example, on a Spotify player’s “Browse” tab).

Examples:

albums = RSpotify::Album.new_releases
albums = RSpotify::Album.new_releases(country: 'US', limit: 10)


47
48
49
50
51
52
53
54
# File 'lib/rspotify/album.rb', line 47

def self.new_releases(limit: 20, offset: 0, country: nil)
  url = "browse/new-releases?limit=#{limit}&offset=#{offset}"
  url << "&country=#{country}" if country
  response = RSpotify.get(url)

  return response if RSpotify.raw_response
  response['albums']['items'].map { |i| Album.new i }
end

.search(query, limit: 20, offset: 0, market: nil) ⇒ Array<Album>

Returns array of Album objects matching the query, ordered by popularity. It’s also possible to find the total number of search results for the query

Examples:

albums = RSpotify::Album.search('AM')
albums = RSpotify::Album.search('AM', limit: 10, market: 'US')
albums = RSpotify::Album.search('AM', market: { from: user })

RSpotify::Album.search('AM').total #=> 9374


70
71
72
# File 'lib/rspotify/album.rb', line 70

def self.search(query, limit: 20, offset: 0, market: nil)
  super(query, 'album', limit: limit, offset: offset, market: market)
end

Instance Method Details

#tracks(limit: 50, offset: 0, market: nil) ⇒ Array<Track>

Returns array of tracks from the album

Examples:

album = RSpotify::Album.find('41vPD50kQ7JeamkxQW7Vuy')
album.tracks.first.name #=> "Do I Wanna Know?"


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rspotify/album.rb', line 111

def tracks(limit: 50, offset: 0, market: nil)
  last_track = offset + limit - 1
  if @tracks_cache && last_track < 50 && !RSpotify.raw_response
    return @tracks_cache[offset..last_track]
  end

  url = "albums/#{@id}/tracks?limit=#{limit}&offset=#{offset}"
  url << "&market=#{market}" if market
  response = RSpotify.get(url)
  json = RSpotify.raw_response ? JSON.parse(response) : response

  tracks = json['items'].map { |i| Track.new i }
  @tracks_cache = tracks if limit == 50 && offset == 0
  return response if RSpotify.raw_response

  tracks
end