Sha256: 8a45362afbf60514bba0ac3caad259cdaaeab7e97ce923bd583d91df69210b70

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

module ItunesApi
  module Requests
    # Allows querying the API via lookup.
    class Lookup
      include Base
      attr_reader_init :artist_id
      selfie :artist_with_albums

      def artist_with_albums
        return artist unless artist

        artist.merge(albums: albums)
      end

      private

      def action
        'lookup'
      end

      def albums
        albums_data.map do |album|
          symbolize_keys(unwrapped(album))
        end
      end

      def albums_data
        results.find_all { |wrappers| wrappers['wrapperType'] == 'collection' }
      end

      def artist
        return artist_data unless artist_data

        symbolize_keys(unwrapped(artist_data))
      end

      def artist_data
        @artist_data ||= results.find do |wrappers|
          wrappers['wrapperType'] == 'artist'
        end
      end

      def query
        {
          entity: 'album',
          id: @artist_id,
          country: country_code,
          limit: LIMIT,
          sort: 'recent'
        }
      end

      def unwrapped(data_hash)
        data_hash.tap { |hash| hash.delete('wrapperType') }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
itunes_api-0.8.2 lib/itunes_api/requests/lookup.rb
itunes_api-0.8.1 lib/itunes_api/requests/lookup.rb
itunes_api-0.8.0 lib/itunes_api/requests/lookup.rb