Sha256: 1da6e415e3cb9d3592536ba8b891a6d808137ccca382236ebe2feaa86834227e

Contents?: true

Size: 1.9 KB

Versions: 4

Compression:

Stored size: 1.9 KB

Contents

module MetaSpotify
  class Album < MetaSpotify::Base
    
    URI_REGEX = /^spotify:album:[A-Za-z0-9]+$/
    
    attr_reader :released, :artists, :available_territories, :tracks, :upc, 
                :musicbrainz_id, :musicbrainz_uri, :allmusic_id, :allmusic_uri
    
    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.attributes['type']
            when 'upc' then 
              @upc = id
            when 'mbid' then
              @musicbrainz_id = id
              @musicbrainz_uri = id.attributes['href']
            when 'amgid' then 
              @allmusic_id = id
              @allmusic_uri = id.attributes['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
    
    def is_available_in?(territory)
      (@available_territories.include?('worldwide') || @available_territories.include?(territory.downcase))
    end
    
    def is_not_available_in?(territory)
      !is_available_in?(territory)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
meta-spotify-0.1.6 lib/meta-spotify/album.rb
meta-spotify-0.1.5 lib/meta-spotify/album.rb
meta-spotify-0.1.4 lib/meta-spotify/album.rb
meta-spotify-0.1.3 lib/meta-spotify/album.rb