Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 689c924

Browse files
authored
[bump to 0.2.4] Added support for position_ms in Spotify::SDK::Connect::Device#play! (#61)
* Added support for position_ms in Spotify::SDK::Connect::Device#play! * bumped version to 0.2.4
1 parent ea8c95b commit 689c924

File tree

4 files changed

+55
-21
lines changed

4 files changed

+55
-21
lines changed

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Metrics/LineLength:
1414
# Too short methods lead to extraction of single-use methods, which can make
1515
# the code easier to read (by naming things), but can also clutter the class
1616
Metrics/MethodLength:
17-
Max: 20
17+
Max: 25
1818

1919
# The guiding principle of classes is SRP, SRP can't be accurately measured by LoC
2020
Metrics/ClassLength:

lib/spotify/sdk/connect/device.rb

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,37 +76,65 @@ def playback
7676
#
7777
# # Play from a playlist, album from a specific index in that list.
7878
# # For example, play the 9th item on X playlist.
79-
# device.play!(index: 5, context: "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr")
79+
# device.play!(
80+
# index: 5,
81+
# context: "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr",
82+
# position_ms: 0
83+
# )
8084
#
8185
# # Play any Spotify URI. Albums, artists, tracks, playlists, and more.
82-
# device.play!(uri: "spotify:track:5MqkZd7a7u7N7hKMqquL2U")
86+
# device.play!(
87+
# uri: "spotify:track:5MqkZd7a7u7N7hKMqquL2U",
88+
# position_ms: 0
89+
# )
8390
#
8491
# # Similar to just uri, but you can define the context.
8592
# # Useful for playing a track that is part of a playlist, and you want the next
8693
# # songs to play from that particular context.
87-
# device.play!(uri: "spotify:track:5MqkZd7a7u7N7hKMqquL2U", context: "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr")
94+
# device.play!(
95+
# uri: "spotify:track:5MqkZd7a7u7N7hKMqquL2U",
96+
# context: "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr",
97+
# position_ms: 0
98+
# )
99+
#
100+
# # Play a track, and immediately seek to 60 seconds.
101+
# device.play!(
102+
# index: 5,
103+
# context: "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr",
104+
# position_ms: 60 * 1000
105+
# )
88106
#
89107
# @see https://developer.spotify.com/console/put-play/
90108
#
91109
# @param [Hash] config The play config you'd like to set. See code examples.
92110
# @return [Spotify::SDK::Connect::Device] self Return itself, so chained methods can be supported.
93111
#
112+
# rubocop:disable AbcSize
94113
def play!(config)
95114
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]}}
115+
when %i[index context position_ms]
116+
{context_uri: config[:context],
117+
offset: {position: config[:index]},
118+
position_ms: config[:position_ms]}
119+
when %i[uri position_ms]
120+
{uris: [config[:uri]],
121+
position_ms: config[:position_ms]}
122+
when %i[uri context position_ms]
123+
{context_uri: config[:context],
124+
offset: {uri: config[:uri]},
125+
position_ms: config[:position_ms]}
102126
else
103-
raise "Unrecognized play instructions. See documentation for details."
127+
raise <<-ERROR.strip_heredoc.strip
128+
Unrecognized play instructions.
129+
See https://www.rubydoc.info/github/bih/spotify-ruby/Spotify/SDK/Connect/Device#play!-instance_method for details.
130+
ERROR
104131
end
105132

106133
parent.send_http_request(:put, "/v1/me/player/play?device_id=%s" % id, http_options: {expect_nil: true},
107134
body: payload.to_json)
108135
self
109136
end
137+
# rubocop:enable AbcSize
110138

111139
##
112140
# Resume the currently playing track on device.

lib/spotify/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ module Spotify
1212
# MINOR version when you add functionality in a backwards-compatible manner, and
1313
# PATCH version when you make backwards-compatible bug fixes.
1414
#
15-
VERSION = "0.2.3"
15+
VERSION = "0.2.4"
1616
end

spec/lib/spotify/sdk/connect/device_spec.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,33 +55,39 @@
5555
end
5656

5757
describe "#play!" do
58-
it "from context and an index, should make an api call" do
58+
it "from context, an index, and playback_ms, should make an api call" do
5959
stub = stub_request(:put, "https://api.spotify.com/v1/me/player/play?device_id=#{raw_data[:id]}")
60-
.with(body: {context_uri: "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr", offset: {position: 5}}.to_json,
60+
.with(body: {context_uri: "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr", offset: {position: 5}, position_ms: 0}.to_json,
6161
headers: {Authorization: "Bearer access_token"})
6262

63-
subject.play!(index: 5, context: "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr")
63+
subject.play!(index: 5, context: "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr", position_ms: 0)
6464
expect(stub).to have_been_requested
6565
end
6666

67-
it "from a uri, should make an api call" do
67+
it "from a uri and playback_ms, should make an api call" do
6868
stub = stub_request(:put, "https://api.spotify.com/v1/me/player/play?device_id=#{raw_data[:id]}")
69-
.with(body: {uris: ["spotify:track:5MqkZd7a7u7N7hKMqquL2U"]}.to_json,
69+
.with(body: {uris: ["spotify:track:5MqkZd7a7u7N7hKMqquL2U"], position_ms: 123}.to_json,
7070
headers: {Authorization: "Bearer access_token"})
7171

72-
subject.play!(uri: "spotify:track:5MqkZd7a7u7N7hKMqquL2U")
72+
subject.play!(uri: "spotify:track:5MqkZd7a7u7N7hKMqquL2U", position_ms: 123)
7373
expect(stub).to have_been_requested
7474
end
7575

76-
it "from a context and uri, should make an api call" do
76+
it "from a context, uri, and playback_ms, should make an api call" do
7777
stub = stub_request(:put, "https://api.spotify.com/v1/me/player/play?device_id=#{raw_data[:id]}")
7878
.with(body: {
7979
context_uri: "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr",
80-
offset: {uri: "spotify:track:5MqkZd7a7u7N7hKMqquL2U"}
80+
offset: {uri: "spotify:track:5MqkZd7a7u7N7hKMqquL2U"},
81+
position_ms: 456
8182
}.to_json,
8283
headers: {Authorization: "Bearer access_token"})
8384

84-
subject.play!(uri: "spotify:track:5MqkZd7a7u7N7hKMqquL2U", context: "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr")
85+
subject.play!(
86+
uri: "spotify:track:5MqkZd7a7u7N7hKMqquL2U",
87+
context: "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr",
88+
position_ms: 456
89+
)
90+
8591
expect(stub).to have_been_requested
8692
end
8793

0 commit comments

Comments
 (0)