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:



225
226
227
228
229
230
231
232
# File 'lib/spotify/sdk/connect/device.rb', line 225

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:



202
203
204
205
# File 'lib/spotify/sdk/connect/device.rb', line 202

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:



168
169
170
171
# File 'lib/spotify/sdk/connect/device.rb', line 168

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

rubocop:disable AbcSize

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",
  position_ms: 0
)

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

# 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",
  position_ms: 0
)

# Play a track, and immediately seek to 60 seconds.
device.play!(
  index: 5,
  context: "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr",
  position_ms: 60 * 1000
)

Parameters:

  • config (Hash)

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

Returns:

See Also:



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/spotify/sdk/connect/device.rb', line 113

def play!(config)
  payload = case config.keys
            when %i[index context position_ms]
              {context_uri: config[:context],
               offset:      {position: config[:index]},
               position_ms: config[:position_ms]}
            when %i[uri position_ms]
              {uris:        [config[:uri]],
               position_ms: config[:position_ms]}
            when %i[uri context position_ms]
              {context_uri: config[:context],
               offset:      {uri: config[:uri]},
               position_ms: config[:position_ms]}
            else
              raise <<-ERROR.strip_heredoc.strip
                Unrecognized play instructions.
                See https://www.rubydoc.info/github/bih/spotify-ruby/Spotify/SDK/Connect/Device#play!-instance_method for details.
              ERROR
            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:



185
186
187
188
# File 'lib/spotify/sdk/connect/device.rb', line 185

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:



275
276
277
278
279
280
281
282
# File 'lib/spotify/sdk/connect/device.rb', line 275

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:



151
152
153
154
# File 'lib/spotify/sdk/connect/device.rb', line 151

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:



249
250
251
252
253
254
255
256
# File 'lib/spotify/sdk/connect/device.rb', line 249

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:



299
300
301
302
303
304
305
306
# File 'lib/spotify/sdk/connect/device.rb', line 299

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:



322
323
324
325
# File 'lib/spotify/sdk/connect/device.rb', line 322

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:



339
340
341
342
# File 'lib/spotify/sdk/connect/device.rb', line 339

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