Class: Spotify::SDK::Connect::Device

Inherits:
Model
  • Object
show all
Defined in:
lib/spotify/sdk/connect/device.rb

Instance Attribute Summary

Attributes inherited from Model

#parent

Instance Method Summary collapse

Methods inherited from Model

alias_attribute, hash_selector, #initialize, #to_h, #to_json

Constructor Details

This class inherits a constructor from Spotify::SDK::Model

Instance Method Details

#active?Boolean

Is the device active?

Examples:

device = @sdk.connect.devices[0]
device.active?

Returns:

  • (Boolean)

    is_active Bool of whether device is active.


27
# File 'lib/spotify/sdk/connect/device.rb', line 27

alias_attribute :active?, :is_active

#change_volume!(volume_percent) ⇒ Spotify::SDK::Connect::Device Also known as: volume=

Set volume for current device. PUT /v1/me/player/volume

Examples:

device = @sdk.connect.devices[0]
device.change_volume!(30)

# or

device = @sdk.connect.devices[0]
device.volume = 30

Parameters:

  • volume_percent (Integer)

    The 0-100 value to change the volume to. 100 is maximum.

Returns:

See Also:


197
198
199
200
201
202
203
# File 'lib/spotify/sdk/connect/device.rb', line 197

def change_volume!(volume_percent)
  raise "Must be an integer" unless volume_percent.is_a?(Integer)
  endpoint = "/v1/me/player/volume?volume_percent=%i&device_id=%s" % [volume_percent, id]
  opts = {http_options: {expect_nil: true}}
  parent.send_http_request(:put, endpoint, opts)
  self
end

#next!Spotify::SDK::Connect::Device

Skip to next track on device. POST /v1/me/player/next

Examples:

device = @sdk.connect.devices[0]
device.next!

Returns:

See Also:


174
175
176
177
# File 'lib/spotify/sdk/connect/device.rb', line 174

def next!
  parent.send_http_request(:post, "/v1/me/player/next?device_id=%s" % id, http_options: {expect_nil: true})
  self
end

#pause!Spotify::SDK::Connect::Device

Pause the currently playing track on device. PUT /v1/me/player/pause

Examples:

device = @sdk.connect.devices[0]
device.pause!

Returns:

See Also:


140
141
142
143
# File 'lib/spotify/sdk/connect/device.rb', line 140

def pause!
  parent.send_http_request(:put, "/v1/me/player/pause?device_id=%s" % id, http_options: {expect_nil: true})
  self
end

#play!(config) ⇒ Spotify::SDK::Connect::Device

Play the currently playing track on device. PUT /v1/me/player/play

Examples:

device = @sdk.connect.devices[0]

# Play from a playlist, album from a specific index in that list.
# For example, play the 9th item on X playlist.
device.play!(index: 5, context: "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr")

# Play any Spotify URI. Albums, artists, tracks, playlists, and more.
device.play!(uri: "spotify:track:5MqkZd7a7u7N7hKMqquL2U")

# Similar to just uri, but you can define the context.
# Useful for playing a track that is part of a playlist, and you want the next
# songs to play from that particular context.
device.play!(uri: "spotify:track:5MqkZd7a7u7N7hKMqquL2U", context: "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr")

Parameters:

  • config (Hash)

    The play config you'd like to set. See code examples.

Returns:

See Also:


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/spotify/sdk/connect/device.rb', line 94

def play!(config)
  payload = case config.keys
            when %i[index context]
              {context_uri: config[:context], offset: {position: config[:index]}}
            when %i[uri]
              {uris: [config[:uri]]}
            when %i[uri context]
              {context_uri: config[:context], offset: {uri: config[:uri]}}
            else
              raise "Unrecognized play instructions. See documentation for details."
            end

  parent.send_http_request(:put, "/v1/me/player/play?device_id=%s" % id, http_options: {expect_nil: true},
                                                                         body:         payload.to_json)
  self
end

#playbackSpotify::SDK::Connect::PlaybackState

Get the currently playing track. Alias to Spotify::SDK::Connect#playback

Examples:

device = @sdk.connect.devices[0]
device.playback

# Same as calling the following:
@sdk.connect.playback

Returns:

See Also:

  • Spotify::SDK::Connect::Device.lib/spotify/sdk/connectlib/spotify/sdk/connect.rb

66
67
68
# File 'lib/spotify/sdk/connect/device.rb', line 66

def playback
  parent.playback
end

#previous!Spotify::SDK::Connect::Device

Skip to previous track on device. POST /v1/me/player/previous

Examples:

device = @sdk.connect.devices[0]
device.previous!

Returns:

See Also:


157
158
159
160
# File 'lib/spotify/sdk/connect/device.rb', line 157

def previous!
  parent.send_http_request(:post, "/v1/me/player/previous?device_id=%s" % id, http_options: {expect_nil: true})
  self
end

#private_session?Boolean

Is the device's session private?

Examples:

device = @sdk.connect.devices[0]
device.private_session?

Returns:

  • (Boolean)

    is_private_session Bool of whether device has a private session.


38
# File 'lib/spotify/sdk/connect/device.rb', line 38

alias_attribute :private_session?, :is_private_session

#repeat!(state) ⇒ Spotify::SDK::Connect::Device Also known as: repeat=

Set repeat mode for current device. PUT /v1/me/player/repeat

Examples:

device = @sdk.connect.devices[0]
device.repeat!(:track)
device.repeat!(:context)
device.repeat!(:off)

Parameters:

  • state (Boolean)

    What to set the repeat state to - :track, :context, or :off

Returns:

See Also:


245
246
247
248
249
250
251
# File 'lib/spotify/sdk/connect/device.rb', line 245

def repeat!(state)
  raise "Must be :track, :context, or :off" unless %i[track context off].include?(state)
  endpoint = "/v1/me/player/repeat?state=%s&device_id=%s" % [state, id]
  opts = {http_options: {expect_nil: true}}
  parent.send_http_request(:put, endpoint, opts)
  self
end

#restricted?Boolean

Is the device restricted?

Examples:

device = @sdk.connect.devices[0]
device.restricted?

Returns:

  • (Boolean)

    is_restricted Bool of whether device is restricted.


49
# File 'lib/spotify/sdk/connect/device.rb', line 49

alias_attribute :restricted?, :is_restricted

#resume!Spotify::SDK::Connect::Device

Resume the currently playing track on device. PUT /v1/me/player/play

Examples:

device = @sdk.connect.devices[0]
device.resume!

Returns:

See Also:


123
124
125
126
# File 'lib/spotify/sdk/connect/device.rb', line 123

def resume!
  parent.send_http_request(:put, "/v1/me/player/play?device_id=%s" % id, http_options: {expect_nil: true})
  self
end

#seek_ms!(position_ms) ⇒ Spotify::SDK::Connect::Device Also known as: position_ms=

Seek position (in milliseconds) for the currently playing track on the device. PUT /v1/me/player/seek

Examples:

device = @sdk.connect.devices[0]
device.seek_ms!(4000)

Parameters:

  • position_ms (Integer)

    In milliseconds, where to seek in the current track on device.

Returns:

See Also:


220
221
222
223
224
225
226
# File 'lib/spotify/sdk/connect/device.rb', line 220

def seek_ms!(position_ms)
  raise "Must be an integer" unless position_ms.is_a?(Integer)
  endpoint = "/v1/me/player/seek?position_ms=%i&device_id=%s" % [position_ms, id]
  opts = {http_options: {expect_nil: true}}
  parent.send_http_request(:put, endpoint, opts)
  self
end

#shuffle!(state) ⇒ Spotify::SDK::Connect::Device Also known as: shuffle=

Set shuffle for current device. PUT /v1/me/player/shuffle

Examples:

device = @sdk.connect.devices[0]
device.shuffle!(true)

Parameters:

  • state (Boolean)

    The true/false of if you'd like to set shuffle on.

Returns:

See Also:


268
269
270
271
272
273
274
# File 'lib/spotify/sdk/connect/device.rb', line 268

def shuffle!(state)
  raise "Must be true or false" unless [true, false].include?(state)
  endpoint = "/v1/me/player/shuffle?state=%s&device_id=%s" % [state, id]
  opts = {http_options: {expect_nil: true}}
  parent.send_http_request(:put, endpoint, opts)
  self
end

#transfer_playback!Spotify::SDK::Connect::Device

Transfer a user's playback to another device, and continue playing. PUT /v1/me/player

Examples:

device = @sdk.connect.devices[0]
device.transfer_playback!

Returns:

See Also:


290
291
292
293
# File 'lib/spotify/sdk/connect/device.rb', line 290

def transfer_playback!
  transfer_playback_method(playing: true)
  self
end

#transfer_state!Spotify::SDK::Connect::Device

Transfer a user's playback to another device, and pause. PUT /v1/me/player

Examples:

device = @sdk.connect.devices[0]
device.transfer_state!

Returns:

See Also:


307
308
309
310
# File 'lib/spotify/sdk/connect/device.rb', line 307

def transfer_state!
  transfer_playback_method(playing: false)
  self
end

#volumeInteger

Get the device's volume.

Examples:

device = @sdk.connect.devices[0]
device.volume

Returns:

  • (Integer)

    volume Get the volume. Between 0 and 100.


16
# File 'lib/spotify/sdk/connect/device.rb', line 16

alias_attribute :volume, :volume_percent