Sha256: 9c31b67514db1731b3635123d9484e595e0891b694398257f1963dd141636508

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

require 'date'
module ItunesApi
  module Music
    # Wrapper for album results.
    class Album
      attr_reader_init :artwork, :collection_id, :genre, :name, :release_on, :store

      class << self
        def for_artist(apple_id, store)
          albums(apple_id, store).map { |album| new(*album.attributes) }
        end

        private

        def albums(apple_id, store)
          Requests::Albums.find_by_apple_id(apple_id, store)
        end
      end

      def availability
        @availability ||= build_availability
      end

      def to_hash
        {
          artwork: artwork,
          availability: availability,
          collection_id: collection_id,
          name: name,
          release_on: release_on,
          store: store
        }
      end

      def tracklist
        @tracklist ||= songs.map { |song| Song.new(*song.attributes) }
      end

      private

      def apple_music?
        tracklist.any?(&:streamable?)
      end

      def build_availability
        prefix = pre_release? ? 'pre_release_' : ''
        suffix = apple_music? ? 'streaming' : 'sale'
        "#{prefix}#{suffix}".to_sym
      end

      def pre_release?
        release_on > Date.today
      end

      def songs
        @songs ||= Requests::Songs.find_by_collection_id(collection_id, store)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
itunes_api-2.0.0 lib/itunes_api/music/album.rb