Class: AppleMusic::Song

Inherits:
Resource show all
Defined in:
lib/apple_music/song.rb,
lib/apple_music/song/attributes.rb,
lib/apple_music/song/relationships.rb

Overview

Defined Under Namespace

Classes: Attributes, Relationships

Constant Summary

Constants inherited from Resource

Resource::RESOURCE_MAP

Instance Attribute Summary

Attributes inherited from Resource

#attributes, #href, #id, #relationships, #type

Class Method Summary collapse

Methods inherited from Resource

build, #initialize

Constructor Details

This class inherits a constructor from AppleMusic::Resource

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class AppleMusic::Resource

Class Method Details

.find(id, **options) ⇒ Object



9
10
11
12
13
# File 'lib/apple_music/song.rb', line 9

def find(id, **options)
  storefront = Storefront.lookup(options.delete(:storefront))
  response = AppleMusic.get("catalog/#{storefront}/songs/#{id}", options)
  Response.new(response.body).data.first
end

.get_collection_by_ids(ids, **options) ⇒ Object

e.g. AppleMusic::Song.get_collection_by_ids([203709340, 201281527]) developer.apple.com/documentation/applemusicapi/get_multiple_catalog_songs_by_id



29
30
31
32
33
34
# File 'lib/apple_music/song.rb', line 29

def get_collection_by_ids(ids, **options)
  ids = ids.is_a?(Array) ? ids.join(',') : ids
  storefront = Storefront.lookup(options.delete(:storefront))
  response = AppleMusic.get("catalog/#{storefront}/songs", options.merge(ids: ids))
  Response.new(response.body).data
end

.get_collection_by_isrc(isrc, **options) ⇒ Object

e.g. AppleMusic::Song.get_collection_by_isrc(‘NLH851300057’) developer.apple.com/documentation/applemusicapi/get_multiple_catalog_songs_by_isrc



38
39
40
41
42
43
# File 'lib/apple_music/song.rb', line 38

def get_collection_by_isrc(isrc, **options)
  isrc = isrc.is_a?(Array) ? isrc.join(',') : isrc
  storefront = Storefront.lookup(options.delete(:storefront))
  response = AppleMusic.get("catalog/#{storefront}/songs", options.merge('filter[isrc]': isrc))
  Response.new(response.body).data
end

.get_relationship(id, relationship_type, **options) ⇒ Object



47
48
49
50
51
# File 'lib/apple_music/song.rb', line 47

def get_relationship(id, relationship_type, **options)
  storefront = Storefront.lookup(options.delete(:storefront))
  response = AppleMusic.get("catalog/#{storefront}/songs/#{id}/#{relationship_type}", options)
  Response.new(response.body).data
end

.list(**options) ⇒ Object

e.g. AppleMusic::Song.list(ids: ‘203709340,201281527’) e.g. AppleMusic::Song.list(isrc: ‘NLH851300057’)



17
18
19
20
21
22
23
24
25
# File 'lib/apple_music/song.rb', line 17

def list(**options)
  if options[:ids]
    get_collection_by_ids(options.delete(:ids), options)
  elsif options[:isrc]
    get_collection_by_isrc(options.delete(:isrc), options)
  else
    raise ParameterMissing, 'required parameter :ids or :isrc is missing'
  end
end

e.g. AppleMusic::Song.related_albums(900032829)



54
55
56
# File 'lib/apple_music/song.rb', line 54

def related_albums(id, **options)
  get_relationship(id, :albums, options)
end

e.g. AppleMusic::Song.related_artists(900032829)



59
60
61
# File 'lib/apple_music/song.rb', line 59

def related_artists(id, **options)
  get_relationship(id, :artists, options)
end

e.g. AppleMusic::Song.related_genres(900032829)



64
65
66
# File 'lib/apple_music/song.rb', line 64

def related_genres(id, **options)
  get_relationship(id, :genres, options)
end

e.g. AppleMusic::Song.related_station(900032829)



69
70
71
# File 'lib/apple_music/song.rb', line 69

def related_station(id, **options)
  get_relationship(id, :station, options).first
end

.search(term, **options) ⇒ Object



73
74
75
# File 'lib/apple_music/song.rb', line 73

def search(term, **options)
  AppleMusic.search(**options.merge(term: term, types: :songs)).songs
end