spotify-ruby 0.1.1 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Spotify
4
+ class SDK
5
+ class Album < Model
6
+ ##
7
+ # Is this an album?
8
+ # Note: This is mostly to support other types of albums in the future.
9
+ #
10
+ # @example
11
+ # album = @sdk.connect.playback.item.album
12
+ # album.album?
13
+ #
14
+ # @return [TrueClass,FalseClass] is_album Returns true if type is an album.
15
+ #
16
+ def album?
17
+ type == "album"
18
+ end
19
+
20
+ ##
21
+ # Display the album's images.
22
+ #
23
+ # @example
24
+ # album = @sdk.connect.playback.item.album
25
+ # album.images[0] # => [#<Spotify::SDK::Image>, #<Spotify::SDK::Image>, ...]
26
+ #
27
+ # @return [Array] album_images Contains a list of images, wrapped in Spotify::SDK::Image
28
+ #
29
+ def images
30
+ super.map do |image|
31
+ Spotify::SDK::Image.new(image, parent)
32
+ end
33
+ end
34
+
35
+ ##
36
+ # Get the artists/creators for this album.
37
+ #
38
+ # @example
39
+ # @sdk.connect.playback.item.album.artists
40
+ #
41
+ # @return [Array] artists A list of artists, wrapped in Spotify::SDK::Artist
42
+ #
43
+ def artists
44
+ super.map do |artist|
45
+ Spotify::SDK::Artist.new(artist, parent)
46
+ end
47
+ end
48
+
49
+ ##
50
+ # Get the primary artist/creator for this album.
51
+ #
52
+ # @example
53
+ # @sdk.connect.playback.item.album.artist
54
+ #
55
+ # @return [Spotify::SDK::Artist] artist The primary artist, wrapped in Spotify::SDK::Artist
56
+ #
57
+ def artist
58
+ artists.first
59
+ end
60
+
61
+ ##
62
+ # Get the Spotify URI for this album.
63
+ # Alias to self.uri
64
+ #
65
+ # @example
66
+ # @sdk.connect.playback.item.album.spotify_uri # => "spotify:track:..."
67
+ #
68
+ # @return [String] spotify_uri The direct URI to this Spotify resource.
69
+ #
70
+ alias_attribute :spotify_uri, :uri
71
+
72
+ ##
73
+ # Get the Spotify HTTP URL for this album.
74
+ # Alias to self.external_urls[:spotify]
75
+ #
76
+ # @example
77
+ # @sdk.connect.playback.item.album.spotify_url # => "https://open.spotify.com/..."
78
+ #
79
+ # @return [String] spotify_url The direct HTTP URL to this Spotify resource.
80
+ #
81
+ alias_attribute :spotify_url, "external_urls.spotify"
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,162 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Spotify
4
+ class SDK
5
+ class Artist < Model
6
+ ##
7
+ # Do we have the full information for this artist?
8
+ #
9
+ # @example
10
+ # artist = @sdk.connect.playback.artist
11
+ # artist.full_information? # => false
12
+ #
13
+ # @return [FalseClass,TrueClass] is_full_info Does this contain everything?
14
+ #
15
+ def full_information?
16
+ to_h.key?(:images)
17
+ end
18
+
19
+ ##
20
+ # Get full information for this artist by calling /v1/artists/:id
21
+ #
22
+ # @example
23
+ # artist = @sdk.connect.playback.artist
24
+ # artist.retrieve_full_information! unless artist.full_information?
25
+ #
26
+ # @return [TrueClass] success Always returns true.
27
+ #
28
+ def retrieve_full_information!
29
+ unless full_information?
30
+ parent.send_http_request(:get, "/v1/artists/%s" % id).map do |key, value|
31
+ send("%s=" % key, value)
32
+ end
33
+ end
34
+
35
+ true
36
+ end
37
+
38
+ ##
39
+ # Helper method for setting the following status.
40
+ # Requires the `user-follow-modify` scope.
41
+ # If true, PUT /v1/me/following otherwise DELETE /v1/me/following
42
+ #
43
+ # @example
44
+ # @sdk.playback.item.artist.following = true
45
+ # @sdk.playback.item.artist.following = false
46
+ #
47
+ def following=(should_follow)
48
+ raise "#following= must be true or false" unless [true, false].include?(should_follow)
49
+ should_follow ? follow! : unfollow!
50
+ end
51
+
52
+ ##
53
+ # Follow the artist.
54
+ # Requires the `user-follow-modify` scope.
55
+ # PUT /v1/me/following
56
+ #
57
+ # @example
58
+ # @sdk.playback.item.artist.follow!
59
+ #
60
+ # @return [Spotify::SDK::Artist] self Return the artist object, for chaining methods.
61
+ #
62
+ def follow!
63
+ parent.send_http_request(:put, "/v1/me/following?type=artist&ids=%s" % id, http_options: {expect_nil: true})
64
+ self
65
+ end
66
+
67
+ ##
68
+ # Unfollow the artist.
69
+ # Requires the `user-follow-modify` scope.
70
+ # DELETE /v1/me/following
71
+ #
72
+ # @example
73
+ # @sdk.playback.item.artist.unfollow!
74
+ #
75
+ # @return [Spotify::SDK::Artist] self Return the artist object, for chaining methods.
76
+ #
77
+ def unfollow!
78
+ parent.send_http_request(:delete, "/v1/me/following?type=artist&ids=%s" % id, http_options: {expect_nil: true})
79
+ self
80
+ end
81
+
82
+ ##
83
+ # Display the artist's images. If not obtained, request them from the API.
84
+ #
85
+ # @example
86
+ # artist = @sdk.connect.playback.artist
87
+ # artist.images[0] # => [#<Spotify::SDK::Image>, #<Spotify::SDK::Image>, ...]
88
+ #
89
+ # @return [Array] images Contains a list of images, wrapped in Spotify::SDK::Image
90
+ #
91
+ def images
92
+ retrieve_full_information! unless full_information?
93
+ super.map {|image| Spotify::SDK::Image.new(image, parent) }
94
+ end
95
+
96
+ ##
97
+ # Display the artist's popularity. If not obtained, request them from the API.
98
+ #
99
+ # @example
100
+ # artist = @sdk.connect.playback.artist
101
+ # artist.popularity # => 90
102
+ #
103
+ # @return [Integer] popularity The number of popularity, between 0-100.
104
+ #
105
+ def popularity
106
+ retrieve_full_information! unless full_information?
107
+ super
108
+ end
109
+
110
+ ##
111
+ # Display the artist's genres. If not obtained, request them from the API.
112
+ #
113
+ # @example
114
+ # artist = @sdk.connect.playback.artist
115
+ # artist.genres # => ["hip hop", "pop rap", "rap", ...]
116
+ #
117
+ # @return [Array] genres An array of genres, denoted in strings.
118
+ #
119
+ def genres
120
+ retrieve_full_information! unless full_information?
121
+ super
122
+ end
123
+
124
+ ##
125
+ # Return the Spotify URL for this artist.
126
+ #
127
+ # @example
128
+ # artist = @sdk.connect.playback.artist
129
+ # artist.spotify_url # => "https://open.spotify.com/artist/..."
130
+ #
131
+ # @return [String] spotify_url The URL to open this artist on open.spotify.com
132
+ #
133
+ def spotify_url
134
+ external_urls[:spotify]
135
+ end
136
+
137
+ ##
138
+ # Return the Spotify URI for this artist.
139
+ #
140
+ # @example
141
+ # artist = @sdk.connect.playback.artist
142
+ # artist.spotify_uri # => "spotify:uri:..."
143
+ #
144
+ # @return [String] spotify_uri The URI to open this artist in official apps.
145
+ #
146
+ alias_attribute :spotify_uri, :uri
147
+
148
+ ##
149
+ # Return the followers on Spotify for this artist.
150
+ #
151
+ # @example
152
+ # artist = @sdk.connect.playback.artist
153
+ # artist.followers # => 13913
154
+ #
155
+ # @return [Integer] followers The number of users following this artist.
156
+ #
157
+ def followers
158
+ super[:total]
159
+ end
160
+ end
161
+ end
162
+ end
@@ -13,19 +13,19 @@ module Spotify
13
13
  # Initiate a Spotify SDK Base component.
14
14
  #
15
15
  # @example
16
- # @sdk = Spotify::SDK.new("access_token")
16
+ # @sdk = Spotify::SDK.new(@session)
17
17
  # @auth = Spotify::SDK::Base.new(@sdk)
18
18
  #
19
- # @sdk = Spotify::SDK.new("access_token_here")
19
+ # @sdk = Spotify::SDK.new(@session)
20
20
  # @sdk.to_hash # => { access_token: ..., expires_at: ... }
21
21
  #
22
- # @param [Spotify::SDK] sdk An instance of Spotify::SDK as a reference point.
22
+ # @param [Spotify::SDK] parent An instance of Spotify::SDK as a reference point.
23
23
  #
24
- def initialize(sdk)
25
- @sdk = sdk
24
+ def initialize(parent)
25
+ @parent = parent
26
26
  @options = {
27
27
  headers: {
28
- Authorization: "Bearer %s" % sdk.access_token
28
+ Authorization: "Bearer %s" % @parent.session.access_token
29
29
  }
30
30
  }
31
31
  end
@@ -38,21 +38,39 @@ module Spotify
38
38
  # send_http_request(:get, "/v1/me/player/devices", @options)
39
39
  #
40
40
  # # Return the raw HTTParty::Response object.
41
- # send_http_request(:get, "/v1/me/player/devices", @options.merge(raw: true))
41
+ # send_http_request(:get, "/v1/me/player/devices", @options.merge({http_options: { raw: true }}))
42
42
  #
43
- # @param [Hash,HTTParty::Response] response The response from the HTTP request.
44
- # @return
43
+ # # Return true for HTTP requests that return a 200 OK with an empty response.
44
+ # send_http_request(:put, "/v1/me/player/pause", @options.merge({http_options: { expect_nil: true }}))
45
45
  #
46
- def send_http_request(method, endpoint, opts={}, &_block)
47
- sdk_opts = opts[:_sdk_opts].presence || {}
48
- opts_sdk = {raw: false, expect_nil: false}.merge(sdk_opts)
49
- response = self.class.send(method, endpoint, @options.merge(opts))
50
- response = response.parsed_response.try(:deep_symbolize_keys) if opts_sdk[:raw] == false
51
- response = true if opts_sdk[:expect_nil] == true && response.nil?
46
+ # @param [Symbol] method The HTTP method you want to perform. Examples are :get, :post, :put, :delete
47
+ # @param [String] endpoint The HTTP endpoint you'd like to call. Example: /v1/me
48
+ # @param [Hash] override_opts Any headers, HTTParty config or application-specific config (see `http_options`)
49
+ # @return [Hash,HTTParty::Response,TrueClass] response The response from the HTTP request.
50
+ #
51
+ # TODO: Address and fix cyclomatic & code complexity issues by Rubocop.
52
+ # rubocop:disable CyclomaticComplexity, PerceivedComplexity, AbcSize
53
+ def send_http_request(method, endpoint, override_opts={})
54
+ opts = {
55
+ raw: false,
56
+ expect_nil: false
57
+ }.merge(override_opts[:http_options].presence || {})
58
+
59
+ httparty = self.class.send(method, endpoint, @options.merge(override_opts))
60
+ response = httparty.parsed_response
61
+ response = response.try(:deep_symbolize_keys) || response
62
+ raise response[:error][:message] if response.is_a?(Hash) && response[:error].present?
63
+ return httparty if opts[:raw] == true
64
+ response = opts[:expect_nil] ? true : raise("No response returned") if response.nil?
52
65
  response
53
66
  end
67
+ # rubocop:enable CyclomaticComplexity, PerceivedComplexity, AbcSize
68
+
69
+ def inspect # :nodoc:
70
+ "#<%s:0x00%x>" % [self.class.name, (object_id << 1)]
71
+ end
54
72
 
55
- attr_reader :sdk
73
+ attr_reader :parent
56
74
  end
57
75
  end
58
76
  end
@@ -3,6 +3,25 @@
3
3
  module Spotify
4
4
  class SDK
5
5
  class Connect < Base
6
+ ##
7
+ # Get the current playback.
8
+ # GET /v1/me/player
9
+ #
10
+ # @example
11
+ # playback = @sdk.connect.playback
12
+ #
13
+ # @see https://developer.spotify.com/console/get-user-player/
14
+ # @see https://developer.spotify.com/documentation/web-api/reference/player/get-information-about-the-users-current-playback/
15
+ #
16
+ # @param [String] market The market you'd like to request.
17
+ # @param [Hash] override_opts Custom options for HTTParty.
18
+ # @return [Spotify::SDK::Connect::PlaybackState] playback_state Return the playback state object.
19
+ #
20
+ def playback(market="from_token", override_opts={})
21
+ playback_state = send_http_request(:get, "/v1/me/player?market=%s" % market, override_opts)
22
+ Spotify::SDK::Connect::PlaybackState.new(playback_state, self)
23
+ end
24
+
6
25
  ##
7
26
  # Collect all the user's available devices.
8
27
  # GET /v1/me/player/devices
@@ -10,10 +29,10 @@ module Spotify
10
29
  # @example
11
30
  # @sdk.connect.devices # => [#<Spotify::SDK::Connect::Device:...>, ...]
12
31
  #
13
- # @see https://developer.spotify.com/web-api/console/get-users-available-devices/
32
+ # @see https://developer.spotify.com/console/get-users-available-devices/
14
33
  #
15
34
  # @param [Hash] override_opts Custom options for HTTParty.
16
- # @return
35
+ # @return [Array] devices A list of all devices.
17
36
  #
18
37
  def devices(override_opts={})
19
38
  response = send_http_request(:get, "/v1/me/player/devices", override_opts)
@@ -21,6 +40,36 @@ module Spotify
21
40
  Spotify::SDK::Connect::Device.new(device, self)
22
41
  end
23
42
  end
43
+
44
+ ##
45
+ # Collect all the active devices.
46
+ #
47
+ # @example
48
+ # @sdk.connect.active_devices # => [#<Spotify::SDK::Connect::Device:...>, ...]
49
+ #
50
+ # @see https://developer.spotify.com/console/get-users-available-devices/
51
+ #
52
+ # @param [Hash] override_opts Custom options for HTTParty.
53
+ # @return [Array] devices A list of all devices that are marked as `is_active`.
54
+ #
55
+ def active_devices(override_opts={})
56
+ devices(override_opts).select(&:active?)
57
+ end
58
+
59
+ ##
60
+ # Collect the first active device.
61
+ #
62
+ # @example
63
+ # @sdk.connect.active_device # => #<Spotify::SDK::Connect::Device:...>
64
+ #
65
+ # @see https://developer.spotify.com/console/get-users-available-devices/
66
+ #
67
+ # @param [Hash] override_opts Custom options for HTTParty.
68
+ # @return [Array,NilClass] device The first device with `is_active`. If no device found, returns `nil`.
69
+ #
70
+ def active_device(override_opts={})
71
+ devices(override_opts).find(&:active?)
72
+ end
24
73
  end
25
74
  end
26
75
  end
@@ -4,14 +4,286 @@ module Spotify
4
4
  class SDK
5
5
  class Connect
6
6
  class Device < Model
7
+ ##
8
+ # Get the device's volume.
9
+ #
10
+ # @example
11
+ # device = @sdk.connect.devices[0]
12
+ # device.volume
13
+ #
14
+ # @return [Integer] volume Get the volume. Between 0 and 100.
15
+ #
16
+ alias_attribute :volume, :volume_percent
17
+
18
+ ##
19
+ # Is the device active?
20
+ #
21
+ # @example
22
+ # device = @sdk.connect.devices[0]
23
+ # device.active?
24
+ #
25
+ # @return [Boolean] is_active Bool of whether device is active.
26
+ #
27
+ alias_attribute :active?, :is_active
28
+
29
+ ##
30
+ # Is the device's session private?
31
+ #
32
+ # @example
33
+ # device = @sdk.connect.devices[0]
34
+ # device.private_session?
35
+ #
36
+ # @return [Boolean] is_private_session Bool of whether device has a private session.
37
+ #
38
+ alias_attribute :private_session?, :is_private_session
39
+
40
+ ##
41
+ # Is the device restricted?
42
+ #
43
+ # @example
44
+ # device = @sdk.connect.devices[0]
45
+ # device.restricted?
46
+ #
47
+ # @return [Boolean] is_restricted Bool of whether device is restricted.
48
+ #
49
+ alias_attribute :restricted?, :is_restricted
50
+
51
+ ##
52
+ # Get the currently playing track.
53
+ # Alias to Spotify::SDK::Connect#playback
54
+ #
55
+ # @example
56
+ # device = @sdk.connect.devices[0]
57
+ # device.playback
58
+ #
59
+ # # Same as calling the following:
60
+ # @sdk.connect.playback
61
+ #
62
+ # @see lib/spotify/sdk/connect.rb
63
+ #
64
+ # @return [Spotify::SDK::Connect::PlaybackState] self Return the playback state object.
65
+ #
66
+ def playback
67
+ parent.playback
68
+ end
69
+
70
+ ##
71
+ # Play the currently playing track on device.
72
+ # PUT /v1/me/player/play
73
+ #
74
+ # @example
75
+ # device = @sdk.connect.devices[0]
76
+ #
77
+ # # Play from a playlist, album from a specific index in that list.
78
+ # # For example, play the 9th item on X playlist.
79
+ # device.play!(index: 5, context: "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr")
80
+ #
81
+ # # Play any Spotify URI. Albums, artists, tracks, playlists, and more.
82
+ # device.play!(uri: "spotify:track:5MqkZd7a7u7N7hKMqquL2U")
83
+ #
84
+ # # Similar to just uri, but you can define the context.
85
+ # # Useful for playing a track that is part of a playlist, and you want the next
86
+ # # songs to play from that particular context.
87
+ # device.play!(uri: "spotify:track:5MqkZd7a7u7N7hKMqquL2U", context: "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr")
88
+ #
89
+ # @see https://developer.spotify.com/console/put-play/
90
+ #
91
+ # @param [Hash] config The play config you'd like to set. See code examples.
92
+ # @return [Spotify::SDK::Connect::Device] self Return itself, so chained methods can be supported.
93
+ #
94
+ def play!(config)
95
+ payload = case config.keys
96
+ when %i[index context]
97
+ {context_uri: config[:context], offset: {position: config[:index]}}
98
+ when %i[uri]
99
+ {uris: [config[:uri]]}
100
+ when %i[uri context]
101
+ {context_uri: config[:context], offset: {uri: config[:uri]}}
102
+ else
103
+ raise "Unrecognized play instructions. See documentation for details."
104
+ end
105
+
106
+ parent.send_http_request(:put, "/v1/me/player/play?device_id=%s" % id, http_options: {expect_nil: true},
107
+ body: payload.to_json)
108
+ self
109
+ end
110
+
111
+ ##
112
+ # Resume the currently playing track on device.
113
+ # PUT /v1/me/player/play
114
+ #
115
+ # @example
116
+ # device = @sdk.connect.devices[0]
117
+ # device.resume!
118
+ #
119
+ # @see https://developer.spotify.com/console/put-play/
120
+ #
121
+ # @return [Spotify::SDK::Connect::Device] self Return itself, so chained methods can be supported.
122
+ #
123
+ def resume!
124
+ parent.send_http_request(:put, "/v1/me/player/play?device_id=%s" % id, http_options: {expect_nil: true})
125
+ self
126
+ end
127
+
128
+ ##
129
+ # Pause the currently playing track on device.
130
+ # PUT /v1/me/player/pause
131
+ #
132
+ # @example
133
+ # device = @sdk.connect.devices[0]
134
+ # device.pause!
135
+ #
136
+ # @see https://developer.spotify.com/console/put-pause/
137
+ #
138
+ # @return [Spotify::SDK::Connect::Device] self Return itself, so chained methods can be supported.
139
+ #
140
+ def pause!
141
+ parent.send_http_request(:put, "/v1/me/player/pause?device_id=%s" % id, http_options: {expect_nil: true})
142
+ self
143
+ end
144
+
145
+ ##
146
+ # Skip to previous track on device.
147
+ # PUT /v1/me/player/previous
148
+ #
149
+ # @example
150
+ # device = @sdk.connect.devices[0]
151
+ # device.previous!
152
+ #
153
+ # @see https://developer.spotify.com/console/put-previous/
154
+ #
155
+ # @return [Spotify::SDK::Connect::Device] self Return itself, so chained methods can be supported.
156
+ #
157
+ def previous!
158
+ parent.send_http_request(:put, "/v1/me/player/previous?device_id=%s" % id, http_options: {expect_nil: true})
159
+ self
160
+ end
161
+
162
+ ##
163
+ # Skip to next track on device.
164
+ # PUT /v1/me/player/next
165
+ #
166
+ # @example
167
+ # device = @sdk.connect.devices[0]
168
+ # device.next!
169
+ #
170
+ # @see https://developer.spotify.com/console/put-next/
171
+ #
172
+ # @return [Spotify::SDK::Connect::Device] self Return itself, so chained methods can be supported.
173
+ #
174
+ def next!
175
+ parent.send_http_request(:put, "/v1/me/player/next?device_id=%s" % id, http_options: {expect_nil: true})
176
+ self
177
+ end
178
+
179
+ ##
180
+ # Set volume for current device.
181
+ # PUT /v1/me/player/volume
182
+ #
183
+ # @example
184
+ # device = @sdk.connect.devices[0]
185
+ # device.change_volume!(30)
186
+ #
187
+ # # or
188
+ #
189
+ # device = @sdk.connect.devices[0]
190
+ # device.volume = 30
191
+ #
192
+ # @see https://developer.spotify.com/console/put-volume/
193
+ #
194
+ # @param [Integer] volume_percent The 0-100 value to change the volume to. 100 is maximum.
195
+ # @return [Spotify::SDK::Connect::Device] self Return itself, so chained methods can be supported.
196
+ #
197
+ def change_volume!(volume_percent)
198
+ raise "Must be an integer" unless volume_percent.is_a?(Integer)
199
+ endpoint = "/v1/me/player/volume?volume_percent=%i&device_id=%s" % [volume_percent, id]
200
+ opts = {http_options: {expect_nil: true}}
201
+ parent.send_http_request(:put, endpoint, opts)
202
+ self
203
+ end
204
+
205
+ alias_method :volume=, :change_volume!
206
+
207
+ ##
208
+ # Seek position (in milliseconds) for the currently playing track on the device.
209
+ # PUT /v1/me/player/seek
210
+ #
211
+ # @example
212
+ # device = @sdk.connect.devices[0]
213
+ # device.seek_ms!(4000)
214
+ #
215
+ # @see https://developer.spotify.com/console/put-seek/
216
+ #
217
+ # @param [Integer] position_ms In milliseconds, where to seek in the current track on device.
218
+ # @return [Spotify::SDK::Connect::Device] self Return itself, so chained methods can be supported.
219
+ #
220
+ def seek_ms!(position_ms)
221
+ raise "Must be an integer" unless position_ms.is_a?(Integer)
222
+ endpoint = "/v1/me/player/seek?position_ms=%i&device_id=%s" % [position_ms, id]
223
+ opts = {http_options: {expect_nil: true}}
224
+ parent.send_http_request(:put, endpoint, opts)
225
+ self
226
+ end
227
+
228
+ alias_method :position_ms=, :seek_ms!
229
+
230
+ ##
231
+ # Set repeat mode for current device.
232
+ # PUT /v1/me/player/repeat
233
+ #
234
+ # @example
235
+ # device = @sdk.connect.devices[0]
236
+ # device.repeat!(:track)
237
+ # device.repeat!(:context)
238
+ # device.repeat!(:off)
239
+ #
240
+ # @see https://developer.spotify.com/console/put-repeat/
241
+ #
242
+ # @param [Boolean] state What to set the repeat state to - :track, :context, or :off
243
+ # @return [Spotify::SDK::Connect::Device] self Return itself, so chained methods can be supported.
244
+ #
245
+ def repeat!(state)
246
+ raise "Must be :track, :context, or :off" unless %i[track context off].include?(state)
247
+ endpoint = "/v1/me/player/repeat?state=%s&device_id=%s" % [state, id]
248
+ opts = {http_options: {expect_nil: true}}
249
+ parent.send_http_request(:put, endpoint, opts)
250
+ self
251
+ end
252
+
253
+ alias_method :repeat=, :repeat!
254
+
255
+ ##
256
+ # Set shuffle for current device.
257
+ # PUT /v1/me/player/shuffle
258
+ #
259
+ # @example
260
+ # device = @sdk.connect.devices[0]
261
+ # device.shuffle!(true)
262
+ #
263
+ # @see https://developer.spotify.com/console/put-shuffle/
264
+ #
265
+ # @param [Boolean] state The true/false of if you'd like to set shuffle on.
266
+ # @return [Spotify::SDK::Connect::Device] self Return itself, so chained methods can be supported.
267
+ #
268
+ def shuffle!(state)
269
+ raise "Must be true or false" unless [true, false].include?(state)
270
+ endpoint = "/v1/me/player/shuffle?state=%s&device_id=%s" % [state, id]
271
+ opts = {http_options: {expect_nil: true}}
272
+ parent.send_http_request(:put, endpoint, opts)
273
+ self
274
+ end
275
+
276
+ alias_method :shuffle=, :shuffle!
277
+
7
278
  ##
8
279
  # Transfer a user's playback to another device, and continue playing.
9
280
  # PUT /v1/me/player
10
281
  #
11
282
  # @example
12
- # device = @sdk.connect.transfer_playback!
283
+ # device = @sdk.connect.devices[0]
284
+ # device.transfer_playback!
13
285
  #
14
- # @see https://developer.spotify.com/web-api/transfer-a-users-playback/
286
+ # @see https://developer.spotify.com/console/transfer-a-users-playback/
15
287
  #
16
288
  # @return [Spotify::SDK::Connect::Device] self Return itself, so chained methods can be supported.
17
289
  #
@@ -25,9 +297,10 @@ module Spotify
25
297
  # PUT /v1/me/player
26
298
  #
27
299
  # @example
28
- # device = @sdk.connect.transfer_state!
300
+ # device = @sdk.connect.devices[0]
301
+ # device.transfer_state!
29
302
  #
30
- # @see https://developer.spotify.com/web-api/transfer-a-users-playback/
303
+ # @see https://developer.spotify.com/console/transfer-a-users-playback/
31
304
  #
32
305
  # @return [Spotify::SDK::Connect::Device] self Return itself, so chained methods can be supported.
33
306
  #
@@ -38,10 +311,12 @@ module Spotify
38
311
 
39
312
  private
40
313
 
41
- def transfer_playback_method(playing:)
314
+ def transfer_playback_method(playing:) # :nodoc:
42
315
  override_opts = {
43
- _sdk_opts: {expect_nil: true},
44
- body: {
316
+ http_options: {
317
+ expect_nil: true
318
+ },
319
+ body: {
45
320
  device_ids: [id],
46
321
  play: playing
47
322
  }.to_json