|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spotify/sdk/initialization" |
| 4 | + |
| 5 | +module Spotify |
| 6 | + ## |
| 7 | + # Spotify::SDK contains the complete Ruby DSL to interact with the Spotify Platform. |
| 8 | + # |
| 9 | + class SDK |
| 10 | + ## |
| 11 | + # Initialize the Spotify SDK object. |
| 12 | + # |
| 13 | + # @example |
| 14 | + # # Example 1: Load it in from an access token value. |
| 15 | + # @sdk = Spotify::SDK.new("access_token_here") |
| 16 | + # |
| 17 | + # # Example 2: Load it in with values from your database. |
| 18 | + # @sdk = Spotify::SDK.new({ |
| 19 | + # access_token: "access_token_here", |
| 20 | + # expires_at: 3_000_000, |
| 21 | + # refresh_token: "refresh_token_here" |
| 22 | + # }) |
| 23 | + # |
| 24 | + # # Example 4: Load it in from an OAuth2::AccessToken object. |
| 25 | + # @sdk = Spotify::SDK.new(@auth.auth_code.get_token("auth code")) |
| 26 | + # |
| 27 | + # # Example 5: Load it from a query string or a fully qualified URL. |
| 28 | + # @sdk = Spotify::SDK.new("https://localhost:8080/#token=...&expires_at=...") |
| 29 | + # @sdk = Spotify::SDK.new("token=...&expires_at=...") |
| 30 | + # |
| 31 | + # @param [String, Hash, OAuth2::AccessToken] obj Any supported object which contains an access token. See examples. |
| 32 | + # |
| 33 | + def initialize(obj) |
| 34 | + @payload = Spotify::SDK::Initialization.detect(obj) |
| 35 | + @access_token = @payload[:access_token] |
| 36 | + @expires_at = @payload[:expires_at] |
| 37 | + @refresh_token = @payload[:refresh_token] |
| 38 | + end |
| 39 | + |
| 40 | + ## |
| 41 | + # Helper method to a fully qualified OAuth2::AccessToken instance. |
| 42 | + # |
| 43 | + # @example |
| 44 | + # @auth = Spotify::Auth.new({ |
| 45 | + # client_id: "[client id goes here]", |
| 46 | + # client_secret: "[client secret goes here]", |
| 47 | + # redirect_uri: "http://localhost" |
| 48 | + # }) |
| 49 | + # |
| 50 | + # @sdk = Spotify::SDK.new("access_token_here") |
| 51 | + # @sdk.oauth2_access_token(@auth) # => #<OAuth2::AccessToken:...> |
| 52 | + # |
| 53 | + # @param [Spotify::Auth] client An instance of Spotify::Auth. See example. |
| 54 | + # @return [OAuth2::AccessToken] An fully qualified instance of OAuth2::AccessToken. |
| 55 | + # |
| 56 | + def oauth2_access_token(client) |
| 57 | + OAuth2::AccessToken.new(client, @access_token, expires_at: @expires_at, |
| 58 | + refresh_token: @refresh_token) |
| 59 | + end |
| 60 | + |
| 61 | + ## |
| 62 | + # Obtain a hash containing all of the user's authorization details. |
| 63 | + # |
| 64 | + # @example |
| 65 | + # @auth = Spotify::Auth.new({ |
| 66 | + # client_id: "[client id goes here]", |
| 67 | + # client_secret: "[client secret goes here]", |
| 68 | + # redirect_uri: "http://localhost" |
| 69 | + # }) |
| 70 | + # |
| 71 | + # @sdk = Spotify::SDK.new("access_token_here") |
| 72 | + # @sdk.to_hash # => { access_token: ..., expires_at: ... } |
| 73 | + # |
| 74 | + # @return [Hash] Containing access_token, expires_at and refresh_token |
| 75 | + # |
| 76 | + def to_hash |
| 77 | + @payload.with_indifferent_access.symbolize_keys |
| 78 | + end |
| 79 | + |
| 80 | + attr_reader :access_token, :expires_at, :refresh_token |
| 81 | + end |
| 82 | +end |
0 commit comments