Class: Spotted::Auth
- Inherits:
-
Object
- Object
- Spotted::Auth
- Defined in:
- lib/spotted/auth.rb
Overview
Handles OAuth 2.0 authorization flows for Spotify API
Constant Summary collapse
- AUTHORIZE_URL =
"https://accounts.spotify.com/authorize"- TOKEN_URL =
"https://accounts.spotify.com/api/token"
Instance Attribute Summary collapse
- #client_id ⇒ String readonly
- #client_secret ⇒ String readonly
Instance Method Summary collapse
-
#authorization_url(redirect_uri:, scope: nil, state: nil, show_dialog: false) ⇒ String
Generates the Spotify authorization URL for OAuth2 authorization code flow.
-
#exchange_authorization_code(code:, redirect_uri:) ⇒ Hash
Exchanges an authorization code for an access token.
-
#initialize(client_id:, client_secret:) ⇒ Auth
constructor
Creates a new Auth instance for handling OAuth flows.
-
#refresh_access_token(refresh_token:) ⇒ Hash
Refreshes an access token using a refresh token.
Constructor Details
#initialize(client_id:, client_secret:) ⇒ Auth
Creates a new Auth instance for handling OAuth flows
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/spotted/auth.rb', line 21 def initialize(client_id:, client_secret:) if client_id.nil? || client_id.empty? raise ArgumentError, "client_id is required" end if client_secret.nil? || client_secret.empty? raise ArgumentError, "client_secret is required" end @client_id = client_id @client_secret = client_secret end |
Instance Attribute Details
#client_id ⇒ String (readonly)
10 11 12 |
# File 'lib/spotted/auth.rb', line 10 def client_id @client_id end |
#client_secret ⇒ String (readonly)
13 14 15 |
# File 'lib/spotted/auth.rb', line 13 def client_secret @client_secret end |
Instance Method Details
#authorization_url(redirect_uri:, scope: nil, state: nil, show_dialog: false) ⇒ String
Generates the Spotify authorization URL for OAuth2 authorization code flow
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/spotted/auth.rb', line 53 def (redirect_uri:, scope: nil, state: nil, show_dialog: false) params = { client_id: @client_id, response_type: "code", redirect_uri: redirect_uri } params[:scope] = scope.is_a?(Array) ? scope.join(" ") : scope if scope params[:state] = state if state params[:show_dialog] = "true" if show_dialog query_string = URI.encode_www_form(params) "#{AUTHORIZE_URL}?#{query_string}" end |
#exchange_authorization_code(code:, redirect_uri:) ⇒ Hash
Exchanges an authorization code for an access token
89 90 91 92 93 94 95 96 97 |
# File 'lib/spotted/auth.rb', line 89 def (code:, redirect_uri:) body = URI.encode_www_form( grant_type: "authorization_code", code: code, redirect_uri: redirect_uri ) make_token_request(body: body) end |
#refresh_access_token(refresh_token:) ⇒ Hash
Refreshes an access token using a refresh token
115 116 117 118 119 120 121 122 |
# File 'lib/spotted/auth.rb', line 115 def refresh_access_token(refresh_token:) body = URI.encode_www_form( grant_type: "refresh_token", refresh_token: refresh_token ) make_token_request(body: body) end |