Sha256: 20b6f4dfae8f943a1185b1efcef75cacc5e9eae51a69a018f3865ff70e9d5190

Contents?: true

Size: 1.78 KB

Versions: 3

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module AppleMusic
  # https://developer.apple.com/documentation/applemusicapi/resource
  class Resource
    class InvalidTypeError < StandardError; end

    RESOURCE_MAP = {
      'activities' => 'AppleMusic::Activity',
      'albums' => 'AppleMusic::Album',
      'apple-curators' => 'AppleMusic::AppleCurator',
      'artists' => 'AppleMusic::Artist',
      'curators' => 'AppleMusic::Curator',
      'genres' => 'AppleMusic::Genre',
      'music-videos' => 'AppleMusic::MusicVideo',
      'playlists' => 'AppleMusic::Playlist',
      'songs' => 'AppleMusic::Song',
      'stations' => 'AppleMusic::Station',
      'storefronts' => 'AppleMusic::Storefront'
    }.freeze

    class << self
      attr_accessor :attributes_model, :relationships_model

      def build(props)
        class_name = RESOURCE_MAP[props['type']] || raise(InvalidTypeError, "#{props['type']} type is undefined.")
        const_get(class_name).new(props)
      end
    end

    attr_reader :href, :id, :type, :attributes, :relationships

    def initialize(props = {})
      @href = props['href']
      @id = props['id']
      @type = props['type']
      @attributes = self.class.attributes_model.new(props['attributes']) if props['attributes']
      @relationships = self.class.relationships_model.new(props['relationships']) if props['relationships']
    end

    private

    def method_missing(name, *args, &block)
      if attributes.respond_to?(name)
        attributes.send(name, *args, &block)
      elsif relationships.respond_to?(name)
        relationships.send(name, *args, &block)
      else
        super
      end
    end

    def respond_to_missing?(name, include_private = false)
      attributes.respond_to?(name, include_private) || relationships.respond_to?(name, include_private)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
apple_music-0.3.1 lib/apple_music/resource.rb
apple_music-0.3.0 lib/apple_music/resource.rb
apple_music-0.2.0 lib/apple_music/resource.rb