Class: AppleMusic::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/apple_music/resource.rb

Overview

Defined Under Namespace

Classes: InvalidTypeError

Constant Summary collapse

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 Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(props = {}) ⇒ Resource

Returns a new instance of Resource.



33
34
35
36
37
38
39
# File 'lib/apple_music/resource.rb', line 33

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



43
44
45
46
47
48
49
50
51
# File 'lib/apple_music/resource.rb', line 43

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

Class Attribute Details

.attributes_modelObject

Returns the value of attribute attributes_model.



23
24
25
# File 'lib/apple_music/resource.rb', line 23

def attributes_model
  @attributes_model
end

.relationships_modelObject

Returns the value of attribute relationships_model.



23
24
25
# File 'lib/apple_music/resource.rb', line 23

def relationships_model
  @relationships_model
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



31
32
33
# File 'lib/apple_music/resource.rb', line 31

def attributes
  @attributes
end

#hrefObject (readonly)

Returns the value of attribute href.



31
32
33
# File 'lib/apple_music/resource.rb', line 31

def href
  @href
end

#idObject (readonly)

Returns the value of attribute id.



31
32
33
# File 'lib/apple_music/resource.rb', line 31

def id
  @id
end

#relationshipsObject (readonly)

Returns the value of attribute relationships.



31
32
33
# File 'lib/apple_music/resource.rb', line 31

def relationships
  @relationships
end

#typeObject (readonly)

Returns the value of attribute type.



31
32
33
# File 'lib/apple_music/resource.rb', line 31

def type
  @type
end

Class Method Details

.build(props) ⇒ Object



25
26
27
28
# File 'lib/apple_music/resource.rb', line 25

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