Class: SpotifyWeb::Song

Inherits:
Resource show all
Defined in:
lib/spotify_web/song.rb

Overview

Represents a song on Spotify

Constant Summary

Constants inherited from Resource

Resource::BASE62_CHARS

Instance Attribute Summary collapse

Attributes inherited from Resource

#gid, #id, #uri, #uri_id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#==, attribute, #attributes=, #hash, #initialize, #load, #load_metadata, #loaded?, #metadata=, #metadata_uri, #pretty_print, #pretty_print_instance_variables

Methods included from Assertions

#assert_valid_keys, #assert_valid_values

Constructor Details

This class inherits a constructor from SpotifyWeb::Resource

Instance Attribute Details

#albumSpotifyWeb::Album (readonly)

Info about the album

Returns:



52
53
54
# File 'lib/spotify_web/song.rb', line 52

attribute :album do |album|
  Album.new(client, album.to_hash)
end

#artistSpotifyWeb::Artist (readonly)

Info about the artist

Returns:



46
47
48
# File 'lib/spotify_web/song.rb', line 46

attribute :artist do |artist|
  Artist.new(client, artist[0].to_hash)
end

#disc_numberFixnum (readonly)

The disc the song is located on within the album

Returns:

  • (Fixnum)


58
# File 'lib/spotify_web/song.rb', line 58

attribute :disc_number

#lengthFixnum (readonly)

Number of seconds the song lasts

Returns:

  • (Fixnum)


68
69
70
# File 'lib/spotify_web/song.rb', line 68

attribute :length, :duration do |length|
  length / 1000
end

#numberFixnum (readonly)

The track number on the disc

Returns:

  • (Fixnum)


62
63
64
# File 'lib/spotify_web/song.rb', line 62

attribute :number do |value|
  value.to_i
end

#popularityFixnum (readonly)

The relative popularity of this song on Spotify

Returns:

  • (Fixnum)


74
75
76
# File 'lib/spotify_web/song.rb', line 74

attribute :popularity do |value|
  value.to_f
end

#restrictionsArray<SpotifyWeb::Restriction> (readonly)

The countries for which this song is permitted to be played

Returns:



80
81
82
# File 'lib/spotify_web/song.rb', line 80

attribute :restrictions, :restriction do |restrictions|
  restrictions.map {|restriction| Restriction.new(client, restriction.to_hash)}
end

#titleString (readonly)

The title of the song

Returns:

  • (String)


42
# File 'lib/spotify_web/song.rb', line 42

attribute :title, :name

Class Method Details

.from_search_result(client, attributes) ⇒ Object

:nodoc:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/spotify_web/song.rb', line 13

def self.from_search_result(client, attributes) #:nodoc:
  attributes = attributes.merge(
    'name' => attributes['title'],
    'artist' => [{
      :id => attributes['artist_id'],
      :name => attributes['artist']
    }],
    'album' => {
      :id => attributes['album_id'],
      :name => attributes['album'],
      :artist => [{
        :id => attributes['album_artist_id'],
        :name => attributes['album_artist']
      }]
    },
    'number' => attributes['track_number'],
    'restriction' => [attributes['restrictions']['restriction']].flatten.map do |restriction|
      {
        :countries_allowed => restriction['allowed'] && restriction['allowed'].split(',').join,
        :countries_forbidden => restriction['forbidden'] && restriction['forbidden'].split(',').join
      }
    end
  )

  super
end

Instance Method Details

#available?Boolean

Whether this song is available to the user

Returns:

  • (Boolean)


86
87
88
# File 'lib/spotify_web/song.rb', line 86

def available?
  restrictions.all? {|restriction| restriction.permitted?(:country)}
end

#similarArray<SpotifyWeb::Song>

Looks up songs that are similar to the current one

Returns:



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/spotify_web/song.rb', line 92

def similar
  response = api('request',
    :uri => "hm://similarity/suggest/#{uri_id}",
    :payload => Schema::Bartender::StoryRequest.new(
      :country => client.user.country,
      :language => client.user.language,
      :device => 'web'
    ),
    :response_schema => Schema::Bartender::StoryList
  )

  # Build songs based on recommendations
  songs = response['result'].stories.map do |story|
    song = story.recommended_item
    album = song.parent
    artist = album.parent

    Song.new(client,
      :uri => song.uri,
      :name => song.display_name,
      :album => {
        :uri => album.uri,
        :name => album.display_name,
        :artist => [{
          :uri => artist.uri,
          :name => artist.display_name
        }]
      },
      :artist => [{
        :uri => artist.uri,
        :name => artist.display_name
      }]
    )
  end

  ResourceCollection.new(client, songs)
end