Sha256: b505323a526eb04bd1516673c84e80dde7e9c39eb32a14bc23bbac7d6ec40aae

Contents?: true

Size: 1.45 KB

Versions: 3

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module ItunesApi
  module Music
    # Wrapper for song results.
    class Song
      attr_reader_init :album,
                       :artist,
                       :duration,
                       :explicitness,
                       :genre,
                       :link,
                       :name,
                       :number,
                       :preview,
                       :store,
                       :streamable,
                       :track_id

      class << self
        def find_by_collection_id(collection_id, store)
          songs(collection_id, store).map { |song| new(*song.attributes) }
        end

        def find_by_track_id(track_id, store)
          result = songs(track_id, store).first

          new(*result.attributes) if result
        end

        private

        def songs(id, store)
          Requests::Songs.find_by_id(id, store)
        end
      end

      def explicit?
        explicitness == 'explicit'
      end

      def streamable?
        return false if streamable.nil?

        streamable
      end

      def to_hash
        {
          album: album,
          artist: artist,
          duration: duration,
          explicit: explicit?,
          genre: genre,
          link: link,
          name: name,
          number: number,
          preview: preview,
          store: store,
          streamable: streamable?,
          track_id: track_id
        }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
itunes_api-2.4.1 lib/itunes_api/music/song.rb
itunes_api-2.4.0 lib/itunes_api/music/song.rb
itunes_api-2.3.2 lib/itunes_api/music/song.rb