Class: MetaSpotify::Album

Inherits:
Base
  • Object
show all
Defined in:
lib/meta-spotify/album.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#name, #popularity, #uri

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

lookup, search, #spotify_id

Constructor Details

#initialize(hash) ⇒ Album

Returns a new instance of Album.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/meta-spotify/album.rb', line 11

def initialize(hash)
  @name = hash['name']
  @popularity = hash['popularity'].to_f if hash.has_key? 'popularity'
  if hash.has_key? 'artist'
    @artists = []
    if hash['artist'].is_a? Array
      hash['artist'].each { |a| @artists << Artist.new(a) }
    else
      @artists << Artist.new(hash['artist'])
    end
  end
  if hash.has_key? 'tracks'
    @tracks = []
    if hash['tracks']['track'].is_a? Array
      hash['tracks']['track'].each { |a| @tracks << Track.new(a) }
    else
      @tracks << Track.new(hash['tracks']['track'])
    end
  end
  @released = hash['released'] if hash.has_key? 'released'
  @uri = hash['href'] if hash.has_key? 'href'
  
  if hash['id'].is_a? Array
    
    hash['id'].each do |id|
      case id['type']
        when 'upc' then 
          @upc = id['__content__']
        when 'mbid' then
          @musicbrainz_id = id['__content__']
          @musicbrainz_uri = id['href']
        when 'amgid' then 
          @allmusic_id = id['__content__']
          @allmusic_uri = id['href']
      end
    end
  end
  
  @available_territories = if hash.has_key?('availability') && !hash['availability']['territories'].nil?
    hash['availability']['territories'].split(/\s+/).map {|t| t.downcase } || []
  else
    []
  end
end

Instance Attribute Details

#allmusic_idObject (readonly)

Returns the value of attribute allmusic_id.



8
9
10
# File 'lib/meta-spotify/album.rb', line 8

def allmusic_id
  @allmusic_id
end

#allmusic_uriObject (readonly)

Returns the value of attribute allmusic_uri.



8
9
10
# File 'lib/meta-spotify/album.rb', line 8

def allmusic_uri
  @allmusic_uri
end

#artistsObject (readonly)

Returns the value of attribute artists.



8
9
10
# File 'lib/meta-spotify/album.rb', line 8

def artists
  @artists
end

#available_territoriesObject (readonly)

Returns the value of attribute available_territories.



8
9
10
# File 'lib/meta-spotify/album.rb', line 8

def available_territories
  @available_territories
end

#musicbrainz_idObject (readonly)

Returns the value of attribute musicbrainz_id.



8
9
10
# File 'lib/meta-spotify/album.rb', line 8

def musicbrainz_id
  @musicbrainz_id
end

#musicbrainz_uriObject (readonly)

Returns the value of attribute musicbrainz_uri.



8
9
10
# File 'lib/meta-spotify/album.rb', line 8

def musicbrainz_uri
  @musicbrainz_uri
end

#releasedObject (readonly)

Returns the value of attribute released.



8
9
10
# File 'lib/meta-spotify/album.rb', line 8

def released
  @released
end

#tracksObject (readonly)

Returns the value of attribute tracks.



8
9
10
# File 'lib/meta-spotify/album.rb', line 8

def tracks
  @tracks
end

#upcObject (readonly)

Returns the value of attribute upc.



8
9
10
# File 'lib/meta-spotify/album.rb', line 8

def upc
  @upc
end

Class Method Details

.uri_regexObject



4
5
6
# File 'lib/meta-spotify/album.rb', line 4

def self.uri_regex
  /^spotify:album:([A-Za-z0-9]+)$/
end

Instance Method Details

#http_uriObject



64
65
66
# File 'lib/meta-spotify/album.rb', line 64

def http_uri
  "http://open.spotify.com/album/#{spotify_id}"
end

#is_available_in?(territory) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/meta-spotify/album.rb', line 56

def is_available_in?(territory)
  (@available_territories.include?('worldwide') || @available_territories.include?(territory.downcase))
end

#is_not_available_in?(territory) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/meta-spotify/album.rb', line 60

def is_not_available_in?(territory)
  !is_available_in?(territory)
end