spotify-ruby 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/COVERAGE.md +142 -0
  4. data/README.md +60 -5
  5. data/lib/spotify.rb +7 -0
  6. data/lib/spotify/auth.rb +10 -18
  7. data/lib/spotify/sdk.rb +109 -0
  8. data/lib/spotify/sdk/base.rb +58 -0
  9. data/lib/spotify/sdk/connect.rb +26 -0
  10. data/lib/spotify/sdk/connect/device.rb +55 -0
  11. data/lib/spotify/sdk/initialization.rb +76 -0
  12. data/lib/spotify/sdk/initialization/base.rb +74 -0
  13. data/lib/spotify/sdk/initialization/oauth_access_token.rb +34 -0
  14. data/lib/spotify/sdk/initialization/plain_string.rb +26 -0
  15. data/lib/spotify/sdk/initialization/query_hash.rb +45 -0
  16. data/lib/spotify/sdk/initialization/query_string.rb +51 -0
  17. data/lib/spotify/sdk/initialization/url_string.rb +49 -0
  18. data/lib/spotify/sdk/model.rb +53 -0
  19. data/lib/spotify/version.rb +1 -1
  20. data/spotify-ruby.gemspec +5 -3
  21. metadata +45 -55
  22. data/SPEC.md +0 -18
  23. data/html/README_md.html +0 -189
  24. data/html/Spotify.html +0 -115
  25. data/html/Spotify/Auth.html +0 -281
  26. data/html/Spotify/Errors.html +0 -103
  27. data/html/Spotify/Errors/AuthClientCredentialsError.html +0 -106
  28. data/html/created.rid +0 -5
  29. data/html/css/fonts.css +0 -167
  30. data/html/css/rdoc.css +0 -590
  31. data/html/fonts/Lato-Light.ttf +0 -0
  32. data/html/fonts/Lato-LightItalic.ttf +0 -0
  33. data/html/fonts/Lato-Regular.ttf +0 -0
  34. data/html/fonts/Lato-RegularItalic.ttf +0 -0
  35. data/html/fonts/SourceCodePro-Bold.ttf +0 -0
  36. data/html/fonts/SourceCodePro-Regular.ttf +0 -0
  37. data/html/images/add.png +0 -0
  38. data/html/images/arrow_up.png +0 -0
  39. data/html/images/brick.png +0 -0
  40. data/html/images/brick_link.png +0 -0
  41. data/html/images/bug.png +0 -0
  42. data/html/images/bullet_black.png +0 -0
  43. data/html/images/bullet_toggle_minus.png +0 -0
  44. data/html/images/bullet_toggle_plus.png +0 -0
  45. data/html/images/date.png +0 -0
  46. data/html/images/delete.png +0 -0
  47. data/html/images/find.png +0 -0
  48. data/html/images/loadingAnimation.gif +0 -0
  49. data/html/images/macFFBgHack.png +0 -0
  50. data/html/images/package.png +0 -0
  51. data/html/images/page_green.png +0 -0
  52. data/html/images/page_white_text.png +0 -0
  53. data/html/images/page_white_width.png +0 -0
  54. data/html/images/plugin.png +0 -0
  55. data/html/images/ruby.png +0 -0
  56. data/html/images/tag_blue.png +0 -0
  57. data/html/images/tag_green.png +0 -0
  58. data/html/images/transparent.png +0 -0
  59. data/html/images/wrench.png +0 -0
  60. data/html/images/wrench_orange.png +0 -0
  61. data/html/images/zoom.png +0 -0
  62. data/html/index.html +0 -189
  63. data/html/js/darkfish.js +0 -161
  64. data/html/js/jquery.js +0 -4
  65. data/html/js/navigation.js +0 -142
  66. data/html/js/navigation.js.gz +0 -0
  67. data/html/js/search.js +0 -109
  68. data/html/js/search_index.js +0 -1
  69. data/html/js/search_index.js.gz +0 -0
  70. data/html/js/searcher.js +0 -229
  71. data/html/js/searcher.js.gz +0 -0
  72. data/html/table_of_contents.html +0 -83
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: edad90fdfafd7b93553043d283cf717520517d94
4
- data.tar.gz: 9188847f32388191255de3ed13f45a6ab272f771
3
+ metadata.gz: 730ba39538bd63e5fffa9a125bc0148259babed8
4
+ data.tar.gz: 4f1f520071fb8b1817544ea7b1ce65e221334026
5
5
  SHA512:
6
- metadata.gz: 70d0432c498285d1d06306ae2132d246729948f17595bc0d921af659476dbd8f5ad1641a258bc017ae46daeaedd2b927c98636f49b324ce61f11601023a2df4c
7
- data.tar.gz: 2c2abd2f27cdec8924fc3f82ada20cf5ad28a1f87d9538625a2b62b0266855d86b3a5b6a24d350cf7bbf6c84041b7d72588463e718ad167dc09577f5c763ec6a
6
+ metadata.gz: 9c64d621832d0eb33b11a31d8ee9eb78da4f3f8f7f0e16e953dc1bab50dd6bc184794acd6d6fec0a1169dbb6c35ad4ce9838ae7a28e6f4ec24c1fe54e8e6c06e
7
+ data.tar.gz: 5764f6fb72f0ef04be219d2ed7fd4d39645c69b9bcc786866c6541dca7f81e677a4b6aab6e06196403b086bbf030bd20826701ee66547b75589a6922510592d3
data/.gitignore CHANGED
@@ -4,6 +4,7 @@
4
4
  /_yardoc/
5
5
  /coverage/
6
6
  /doc/
7
+ /html/
7
8
  /pkg/
8
9
  /spec/reports/
9
10
  /tmp/
@@ -12,3 +13,4 @@
12
13
  # rspec failure tracking
13
14
  .rspec_status
14
15
  .coveralls.yml
16
+ .DS_Store
@@ -0,0 +1,142 @@
1
+ potify API Coverage
2
+
3
+ This covers all the Spotify API endpoints that are covered.
4
+
5
+ ### Albums Endpoints
6
+
7
+ | Endpoint | Description | Coverage Status |
8
+ | -------- | ----------- | --------------- |
9
+ | GET /v1/albums | Get Several Albums | × |
10
+ | GET /v1/albums/{id}/tracks | Get an Album's Tracks | × |
11
+
12
+ ### Artists Endpoints
13
+
14
+ | Endpoint | Description | Coverage Status |
15
+ | -------- | ----------- | --------------- |
16
+ | GET /v1/artists | Get Several Artists | × |
17
+ | GET /v1/artists/{id}/albums | Get an Artist's Albums | × |
18
+ | GET /v1/artists/{id}/top-tracks | Get an Artist's Top Tracks | × |
19
+ | GET /v1/artists/{id}/related-artists | Get an Artist's Related Artists | × |
20
+
21
+ ### Tracks Endpoints
22
+
23
+ | Endpoint | Description | Coverage Status |
24
+ | -------- | ----------- | --------------- |
25
+ | GET /v1/tracks | Get Several Tracks | × |
26
+
27
+ ### Audio Features Endpoints
28
+
29
+ | Endpoint | Description | Coverage Status |
30
+ | -------- | ----------- | --------------- |
31
+ | GET /v1/audio-features/{id} | Get Audio Features for a Track | × |
32
+ | GET /v1/audio-features | Get Audio Features for Several Tracks | × |
33
+
34
+ ### Analysis Endpoints
35
+
36
+ | Endpoint | Description | Coverage Status |
37
+ | -------- | ----------- | --------------- |
38
+ | analysis | GET /v1/audio-analysis/{id} | Get Audio Analysis for a Track | × |
39
+
40
+ ### Search Endpoints
41
+
42
+ | Endpoint | Description | Coverage Status |
43
+ | -------- | ----------- | --------------- |
44
+ | search | GET /v1/search | Search for an Item | × |
45
+
46
+ ### Users Endpoints
47
+
48
+ | Endpoint | Description | Coverage Status |
49
+ | -------- | ----------- | --------------- |
50
+ | users | GET /v1/users/{user_id} | Get a User's Profile | × |
51
+ | users | GET /v1/me | Get Current User's Profile | × |
52
+
53
+ ### Saved Content Endpoints
54
+
55
+ | Endpoint | Description | Coverage Status |
56
+ | -------- | ----------- | --------------- |
57
+ | GET /v1/me/tracks | Get Current User's Saved Tracks | × |
58
+ | GET /v1/me/tracks/contains | Check Current User's Saved Tracks | × |
59
+ | PUT /v1/me/tracks | Save Tracks for Current User | × |
60
+ | DELETE /v1/me/tracks | Remove Tracks for Current User | × |
61
+ | GET /v1/me/albums | Get Current User's Saved Albums | × |
62
+ | GET /v1/me/albums/contains | Check Current User's Saved Albums | × |
63
+ | PUT /v1/me/albums | Save Albums for Current User | × |
64
+ | DELETE /v1/me/albums | Remove Albums for Current User | × |
65
+
66
+ ### Discovery Endpoints
67
+
68
+ | Endpoint | Description | Coverage Status |
69
+ | -------- | ----------- | --------------- |
70
+ | GET /v1/browse/new-releases | Get a List of New Releases | × |
71
+ | GET /v1/browse/featured-playlists | Get a List of Featured Playlists | × |
72
+
73
+ ### Categories Endpoints
74
+
75
+ | Endpoint | Description | Coverage Status |
76
+ | -------- | ----------- | --------------- |
77
+ | GET /v1/browse/categories | Get a List of Browse Categories | × |
78
+ | GET /v1/browse/categories/{category_id} | Get a Single Browse Category | × |
79
+ | GET /v1/browse/categories/{category_id}/playlists | Get a Category's playlists | × |
80
+
81
+ ### Recommendation Endpoints
82
+
83
+ | Endpoint | Description | Coverage Status |
84
+ | -------- | ----------- | --------------- |
85
+ | GET /v1/recommendations | Get Recommendations Based on Seeds | × |
86
+ | GET /v1/recommendations/available-genre-seeds | Get Available Genre Seeds | × |
87
+
88
+ ### Follow Endpoints
89
+
90
+ | Endpoint | Description | Coverage Status |
91
+ | -------- | ----------- | --------------- |
92
+ | GET /v1/me/following | Get Followed Artists | × |
93
+ | GET /v1/me/following/contains | Check if Current User Follows Artists or Users | × |
94
+ | PUT /v1/me/following | Follow Artists or Users | × |
95
+ | DELETE /v1/me/following | Unfollow Artists or Users | × |
96
+ | GET /v1/users/{user_id}/playlists/{playlist_id}/followers/contains | Check if Users Follow a Playlist | × |
97
+ | PUT /v1/users/{user_id}/playlists/{playlist_id}/followers | Follow a Playlist | × |
98
+ | DELETE /v1/users/{user_id}/playlists/{playlist_id}/followers | Unfollow a Playlist | × |
99
+
100
+ ### Playlists Endpoints
101
+
102
+ | Endpoint | Description | Coverage Status |
103
+ | -------- | ----------- | --------------- |
104
+ | GET /v1/users/{user_id}/playlists | Get a List of a User's Playlists | × |
105
+ | GET /v1/me/playlists | Get a List of Current User's Playlists | × |
106
+ | GET /v1/users/{user_id}/playlists/{playlist_id} | Get a Playlist | × |
107
+ | GET /v1/users/{user_id}/playlists/{playlist_id}/tracks | Get a Playlist's Tracks | × |
108
+ | POST /v1/users/{user_id}/playlists | Create a Playlist | × |
109
+ | POST /v1/users/{user_id}/playlists/{playlist_id}/tracks | Add Tracks to a Playlist | × |
110
+ | DELETE /v1/users/{user_id}/playlists/{playlist_id}/tracks | Remove Tracks from a Playlist | × |
111
+ | PUT /v1/users/{user_id}/playlists/{playlist_id}/tracks | Reorder or replace a Playlist's Tracks | × |
112
+ | PUT /v1/users/{user_id}/playlists/{playlist_id} | Change a Playlist's Details | × |
113
+
114
+ ### History Endpoints
115
+
116
+ | Endpoint | Description | Coverage Status |
117
+ | -------- | ----------- | --------------- |
118
+ | GET /v1/me/top/{type} | Get User's Top Artists and Tracks | × |
119
+ | GET /v1/me/player/recently-played | Get the Current User's Recently Played Tracks | × |
120
+
121
+ ### Connect Endpoints
122
+
123
+ | Endpoint | Description | Coverage Status |
124
+ | -------- | ----------- | --------------- |
125
+ | GET /v1/me/player | Get Information About The User's Current Playback | × |
126
+ | GET /v1/me/player/devices | Get a User's Available Devices | [✔](/lib/spotify/sdk/connect.rb) |
127
+ | PUT /v1/me/player | Transfer a User's Playback | [✔](/lib/spotify/sdk/connect/device.rb) |
128
+
129
+ ### Player Endpoints
130
+
131
+ | Endpoint | Description | Coverage Status |
132
+ | -------- | ----------- | --------------- |
133
+ | GET /v1/me/player/currently-playing | Get the User's Currently Playing Track | × |
134
+ | PUT /v1/me/player/play | Start/Resume a User's Playback | × |
135
+ | PUT /v1/me/player/pause | Pause a User's Playback | × |
136
+ | POST /v1/me/player/next | Skip User's Playback To Next Track | × |
137
+ | POST /v1/me/player/previous | Skip User's Playback To Previous Track | × |
138
+ | PUT /v1/me/player/seek | Seek To Position In Currently Playing Track | × |
139
+ | PUT /v1/me/player/repeat | Set Repeat Mode On User's Playback | × |
140
+ | PUT /v1/me/player/volume | Set Volume For User's Playback | × |
141
+ | PUT /v1/me/player/shuffle | Toggle Shuffle For User's Playback | × |
142
+
data/README.md CHANGED
@@ -5,6 +5,9 @@ A modern, opinionated and *unofficial* Ruby SDK for the [Spotify Web API][spotif
5
5
  This is a work in progress. **Currently in pre-alpha.**
6
6
 
7
7
  [![Build Status](https://travis-ci.org/bih/spotify-ruby.svg?branch=master)](https://travis-ci.org/bih/spotify-ruby)
8
+ [![Coverage Status](https://coveralls.io/repos/github/bih/spotify-ruby/badge.svg)](https://coveralls.io/github/bih/spotify-ruby)
9
+ [![Gem Version](https://badge.fury.io/rb/spotify-ruby.svg)](https://badge.fury.io/rb/spotify-ruby)
10
+ [![Code Triagers Badge](https://www.codetriage.com/bih/spotify-ruby/badges/users.svg)](https://www.codetriage.com/bih/spotify-ruby)
8
11
 
9
12
  ## Installation
10
13
 
@@ -30,23 +33,74 @@ Configure with your client credentials and redirect URL. Get it [for free here][
30
33
  require "spotify"
31
34
 
32
35
  @auth = Spotify::Auth.new({
33
- client_id: ENV["SPOTIFY_CLIENT_ID"],
36
+ client_id: ENV["SPOTIFY_CLIENT_ID"],
34
37
  client_secret: ENV["SPOTIFY_CLIENT_SECRET"],
35
- redirect_uri: ENV["SPOTIFY_REDIRECT_URI"]
38
+ redirect_uri: ENV["SPOTIFY_REDIRECT_URI"]
36
39
  })
37
40
  ```
38
41
 
39
42
  ## Authentication
40
43
 
41
- With our `@auth` instance, we can initiate an authentication URL for `https://accounts.spotify.com`. By default, this will have all the values needed to get a user setup.
44
+ With our `@auth` instance, we have access to multiple forms of authentication. Below are our ones that we make available through the Spotify Platform: Authorization Code, Implicit Grant and Client Credentials. [Learn more here][spotify-authorization-guide].
45
+
46
+ ### Authorization Code
47
+
48
+ **Recommended.** Generate the URL for the user to grant permissions for your application:
49
+
50
+ ```ruby
51
+ @auth.authorize_url(response_type: "code")
52
+ ```
53
+
54
+ Once they return back to your application with a `code`:
55
+
56
+ ```ruby
57
+ @access_token = @auth.auth_code.get_token(params[:code])
58
+ ```
59
+
60
+ You can save their information in the database under `access_token`, `expires_in` and `refresh_token`
61
+ ```ruby
62
+ @sdk = Spotify::SDK.new(@access_token)
63
+ @sdk.to_hash # => { access_token: "...", expires_in: 1234567890, refresh_token: "..." }
64
+ ```
65
+
66
+ And you can also re-instantiate a SDK instance again later:
67
+ ```ruby
68
+ @sdk = Spotify::SDK.new({
69
+ access_token: "[insert access_token]",
70
+ expires_in: "[insert expires_in]",
71
+ refresh_token: "[insert refresh_token]"
72
+ })
73
+ ```
74
+
75
+ And when their access token expires, you can just run `refresh!`:
76
+
77
+ ```ruby
78
+ @access_token = @access_token.refresh!
79
+ ```
80
+
81
+ ### Implicit Grant
82
+
83
+ ```ruby
84
+ @auth.authorize_url(response_type: "token")
85
+ ```
86
+
87
+ Once they return back to your application with a `token`:
88
+
89
+ ```ruby
90
+ @sdk = Spotify::SDK.new(params[:token])
91
+ ```
92
+
93
+ ### Client Credentials
94
+
95
+ Generate an access token based from your client credentials. Note this has limited access.
42
96
 
43
97
  ```ruby
44
- @auth.authorize_url # => https://accounts.spotify.com/oauth/authorize?client_id=...&redirect_uri=...
98
+ @access_token = @auth.client_credentials.get_token
45
99
  ```
46
100
 
47
101
  ## Usage
48
102
 
49
- TODO: Write more detailed usage instructions here
103
+ TODO: Write more detailed usage instructions here. [Spotify API endpoint coverage can be found in COVERAGE.md](COVERAGE.md)
50
104
 
51
105
  ## Development
52
106
 
@@ -67,6 +121,7 @@ The gem is available as open source under the terms of the [MIT License](http://
67
121
  Everyone interacting in the `spotify-ruby` project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/bih/spotify-ruby/blob/master/CODE_OF_CONDUCT.md).
68
122
 
69
123
  [spotify]: https://spotify.com
124
+ [spotify-authorization-guide]: https://developer.spotify.com/web-api/authorization-guide/
70
125
  [spotify-web-api]: https://developer.spotify.com/documentation/web-api/reference/
71
126
  [spotify-developer-dashboard]: https://developer.spotify.com/my-applications/
72
127
  [rubyinfo-docs]: http://www.rubydoc.info/github/bih/spotify-ruby
@@ -1,7 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "httparty"
4
+ require "oauth2"
5
+
6
+ require "active_support"
7
+ require "active_support/core_ext"
8
+
3
9
  require "spotify/version"
4
10
  require "spotify/auth"
11
+ require "spotify/sdk"
5
12
 
6
13
  ##
7
14
  # The declaration for the Spotify namespace.
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "oauth2"
4
-
5
3
  module Spotify
6
4
  ##
7
5
  # Spotify::Auth inherits from OAuth2::Client based on the "oauth-2" gem.
@@ -39,7 +37,6 @@ module Spotify
39
37
  # Initialize the Spotify Auth object.
40
38
  #
41
39
  # @example
42
- #
43
40
  # @auth = Spotify::Auth.new({
44
41
  # client_id: "[client id goes here]",
45
42
  # client_secret: "[client secret goes here]",
@@ -47,25 +44,25 @@ module Spotify
47
44
  # })
48
45
  #
49
46
  # @param [Hash] config OAuth configuration containing the Client ID, secret and redirect URL.
50
- # The redirect URL can be overriden later.
51
47
  #
52
48
  # @see https://developer.spotify.com/my-applications/
53
49
  #
54
50
  def initialize(config)
51
+ validate_initialized_input(config)
52
+ client_id = config.delete(:client_id)
53
+ client_secret = config.delete(:client_secret)
55
54
  opts = {
56
55
  site: "https://api.spotify.com",
57
- authorize_url: "https://accounts.spotify.com/oauth/authorize"
58
- }
59
- validate_initialized_input(config)
60
- @redirect_uri = config[:redirect_uri]
61
- super(config[:client_id], config[:client_secret], opts)
56
+ authorize_url: "https://accounts.spotify.com/oauth/authorize",
57
+ token_url: "https://accounts.spotify.com/api/token"
58
+ }.merge(config)
59
+ super(client_id, client_secret, opts)
62
60
  end
63
61
 
64
62
  ##
65
63
  # Get a HTTP URL to send user for authorizing with Spotify.
66
64
  #
67
65
  # @example
68
- #
69
66
  # @auth = Spotify::Auth.new({
70
67
  # client_id: "[client id goes here]",
71
68
  # client_secret: "[client secret goes here]",
@@ -76,13 +73,13 @@ module Spotify
76
73
  #
77
74
  # @param [Hash] override_params Optional hash containing any overriding values for parameters.
78
75
  # Parameters used are client_id, redirect_uri, response_type and scope.
76
+ # @return [String] A fully qualified Spotify authorization URL to send the user to.
79
77
  #
80
78
  # @see https://developer.spotify.com/web-api/authorization-guide/
81
79
  #
82
80
  def authorize_url(override_params={})
83
81
  super({
84
82
  client_id: id,
85
- redirect_uri: redirect_uri,
86
83
  response_type: "code",
87
84
  scope: SCOPES.join(" ")
88
85
  }.merge(override_params))
@@ -90,21 +87,16 @@ module Spotify
90
87
 
91
88
  private
92
89
 
93
- ##
94
- # OAuth2::Client does not support redirect_uri at initialization, so we store
95
- # it in the instance and call it later. We think it makes things clearer.
96
- #
97
- attr_accessor :redirect_uri
98
-
99
90
  ##
100
91
  # Validate initialization configuration and raise errors.
101
92
  #
102
93
  # @param [Hash] config OAuth configuration containing the Client ID, secret and redirect URL.
94
+ # @return [nil]
103
95
  #
104
96
  def validate_initialized_input(config)
105
97
  raise Errors::AuthClientCredentialsError.new(OAUTH_I18N[:must_be_hash]) unless config.is_a?(Hash)
106
98
 
107
- %i[client_id client_secret redirect_uri].each do |key|
99
+ %i[client_id client_secret].each do |key|
108
100
  raise Errors::AuthClientCredentialsError.new(OAUTH_I18N[:require_attr] % key) unless config.has_key?(key)
109
101
  end
110
102
  end
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spotify/sdk/initialization"
4
+ require "spotify/sdk/initialization/base"
5
+ require "spotify/sdk/initialization/oauth_access_token"
6
+ require "spotify/sdk/initialization/plain_string"
7
+ require "spotify/sdk/initialization/query_hash"
8
+ require "spotify/sdk/initialization/query_string"
9
+ require "spotify/sdk/initialization/url_string"
10
+ require "spotify/sdk/base"
11
+ require "spotify/sdk/model"
12
+ require "spotify/sdk/connect"
13
+ require "spotify/sdk/connect/device"
14
+
15
+ module Spotify
16
+ ##
17
+ # Spotify::SDK contains the complete Ruby DSL to interact with the Spotify Platform.
18
+ #
19
+ class SDK
20
+ ##
21
+ # Initialize the Spotify SDK object.
22
+ #
23
+ # @example
24
+ # # Example 1: Load it in from an access token value.
25
+ # @sdk = Spotify::SDK.new("access_token_here")
26
+ #
27
+ # # Example 2: Load it in with values from your database.
28
+ # @sdk = Spotify::SDK.new({
29
+ # access_token: "access_token_here",
30
+ # expires_in: 3_000_000,
31
+ # refresh_token: "refresh_token_here"
32
+ # })
33
+ #
34
+ # # Example 4: Load it in from an OAuth2::AccessToken object.
35
+ # @sdk = Spotify::SDK.new(@auth.auth_code.get_token("auth code"))
36
+ #
37
+ # # Example 5: Load it from a query string or a fully qualified URL.
38
+ # @sdk = Spotify::SDK.new("https://localhost:8080/#token=...&expires_in=...")
39
+ # @sdk = Spotify::SDK.new("token=...&expires_in=...")
40
+ #
41
+ # @param [String, Hash, OAuth2::AccessToken] obj Any supported object which contains an access token. See examples.
42
+ #
43
+ def initialize(obj)
44
+ @payload = Spotify::SDK::Initialization.detect(obj)
45
+ @access_token = @payload[:access_token]
46
+ @expires_in = @payload[:expires_in]
47
+ @refresh_token = @payload[:refresh_token]
48
+
49
+ mount_sdk_components
50
+ end
51
+
52
+ ##
53
+ # Helper method to a fully qualified OAuth2::AccessToken instance.
54
+ #
55
+ # @example
56
+ # @auth = Spotify::Auth.new({
57
+ # client_id: "[client id goes here]",
58
+ # client_secret: "[client secret goes here]",
59
+ # redirect_uri: "http://localhost"
60
+ # })
61
+ #
62
+ # @sdk = Spotify::SDK.new("access_token_here")
63
+ # @sdk.oauth2_access_token(@auth) # => #<OAuth2::AccessToken:...>
64
+ #
65
+ # @param [Spotify::Auth] client An instance of Spotify::Auth. See example.
66
+ # @return [OAuth2::AccessToken] An fully qualified instance of OAuth2::AccessToken.
67
+ #
68
+ def oauth2_access_token(client)
69
+ OAuth2::AccessToken.new(client, @access_token, expires_in: @expires_in,
70
+ refresh_token: @refresh_token)
71
+ end
72
+
73
+ ##
74
+ # Obtain a hash containing all of the user's authorization details.
75
+ #
76
+ # @example
77
+ # @auth = Spotify::Auth.new({
78
+ # client_id: "[client id goes here]",
79
+ # client_secret: "[client secret goes here]",
80
+ # redirect_uri: "http://localhost"
81
+ # })
82
+ #
83
+ # @sdk = Spotify::SDK.new("access_token_here")
84
+ # @sdk.to_hash # => { access_token: ..., expires_in: ... }
85
+ #
86
+ # @return [Hash] Containing access_token, expires_in and refresh_token
87
+ #
88
+ def to_hash
89
+ @payload.with_indifferent_access.symbolize_keys
90
+ end
91
+
92
+ attr_reader :access_token, :expires_in, :refresh_token
93
+ attr_reader :connect
94
+
95
+ private
96
+
97
+ ##
98
+ # This is where we mount all SDK components to the SDK object.
99
+ # When mounting a new component, you'll need to do the following:
100
+ # - Be sure to add a `attr_reader` for it. Developers can't access it otherwise.
101
+ # - Add a test for it in spec/lib/spotify/sdk_spec.rb (see how we did it for others)
102
+ #
103
+ # @return [nil]
104
+ #
105
+ def mount_sdk_components
106
+ @connect = Spotify::SDK::Connect.new(self)
107
+ end
108
+ end
109
+ end