Class: Spotify::Accounts::Session
- Inherits:
-
Object
- Object
- Spotify::Accounts::Session
- Defined in:
- lib/spotify/accounts/session.rb
Overview
A class representing an access token, with the ability to refresh.
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#accounts ⇒ Object
readonly
Returns the value of attribute accounts.
-
#expires_in ⇒ Object
readonly
Returns the value of attribute expires_in.
-
#refresh_token ⇒ Object
readonly
Returns the value of attribute refresh_token.
Class Method Summary collapse
-
.from_authorization_code(accounts, code) ⇒ Spotify::Accounts::Session
Parse the response we collect from the authorization code.
-
.from_refresh_token(accounts, refresh_token) ⇒ Spotify::Accounts::Session
Set up an instance of Access Token with just a refresh_token.
Instance Method Summary collapse
-
#contains_scope?(scope) ⇒ TrueClass, FalseClass
Checks if a specific scope has been granted by the user.
-
#expired? ⇒ TrueClass, ...
Check if the access token has expired.
-
#expires_at ⇒ Time
When will the access token expire? Returns nil if no expires_in is defined.
-
#initialize(accounts, access_token, expires_in, refresh_token, scopes) ⇒ Session
constructor
A new instance of Session.
-
#inspect ⇒ Object
:nodoc:.
-
#refresh! ⇒ TrueClass, FalseClass
Refresh the access token.
-
#scopes ⇒ Array
Converts the space-delimited scope list to a symbolized array.
-
#to_json(*_args) ⇒ String
Export to JSON.
Constructor Details
#initialize(accounts, access_token, expires_in, refresh_token, scopes) ⇒ Session
Returns a new instance of Session.
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/spotify/accounts/session.rb', line 52 def initialize(accounts, access_token, expires_in, refresh_token, scopes) unless accounts.instance_of?(Spotify::Accounts) raise "You need a valid Spotify::Accounts instance in order to use Spotify authentication." end @accounts = accounts @access_token = access_token @expires_in = expires_in @expires_at = expires_in + Time.now.to_i unless expires_in.nil? @refresh_token = refresh_token @scopes = scopes end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
65 66 67 |
# File 'lib/spotify/accounts/session.rb', line 65 def access_token @access_token end |
#accounts ⇒ Object (readonly)
Returns the value of attribute accounts.
65 66 67 |
# File 'lib/spotify/accounts/session.rb', line 65 def accounts @accounts end |
#expires_in ⇒ Object (readonly)
Returns the value of attribute expires_in.
65 66 67 |
# File 'lib/spotify/accounts/session.rb', line 65 def expires_in @expires_in end |
#refresh_token ⇒ Object (readonly)
Returns the value of attribute refresh_token.
65 66 67 |
# File 'lib/spotify/accounts/session.rb', line 65 def refresh_token @refresh_token end |
Class Method Details
.from_authorization_code(accounts, code) ⇒ Spotify::Accounts::Session
Parse the response we collect from the authorization code.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/spotify/accounts/session.rb', line 21 def (accounts, code) params = { client_id: @accounts.instance_variable_get(:@client_id), client_secret: @accounts.instance_variable_get(:@client_secret), redirect_uri: @accounts.instance_variable_get(:@redirect_uri), grant_type: "authorization_code", code: code } request = HTTParty.post("https://accounts.spotify.com/api/token", body: params) response = request.parsed_response.with_indifferent_access raise response[:error_description] if response[:error] new(accounts, response[:access_token], response[:expires_in], response[:refresh_token], response[:scope]) end |
.from_refresh_token(accounts, refresh_token) ⇒ Spotify::Accounts::Session
Set up an instance of Access Token with just a refresh_token.
47 48 49 |
# File 'lib/spotify/accounts/session.rb', line 47 def from_refresh_token(accounts, refresh_token) new(accounts, nil, nil, refresh_token, nil) end |
Instance Method Details
#contains_scope?(scope) ⇒ TrueClass, FalseClass
Checks if a specific scope has been granted by the user.
91 92 93 |
# File 'lib/spotify/accounts/session.rb', line 91 def contains_scope?(scope) scopes.include?(scope.downcase.to_sym) end |
#expired? ⇒ TrueClass, ...
Check if the access token has expired. Returns nil if no expires_in is defined.
117 118 119 120 121 |
# File 'lib/spotify/accounts/session.rb', line 117 def expired? return nil if expires_at.nil? Time.now > expires_at end |
#expires_at ⇒ Time
When will the access token expire? Returns nil if no expires_in is defined.
103 104 105 106 107 |
# File 'lib/spotify/accounts/session.rb', line 103 def expires_at return nil if @expires_in.nil? Time.at(@expires_at) end |
#inspect ⇒ Object
:nodoc:
172 173 174 |
# File 'lib/spotify/accounts/session.rb', line 172 def inspect # :nodoc: "#<%s:0x00%x>" % [self.class.name, (object_id << 1)] end |
#refresh! ⇒ TrueClass, FalseClass
Refresh the access token.
rubocop:disable AbcSize
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/spotify/accounts/session.rb', line 132 def refresh! raise "You cannot refresh without a valid refresh_token." if @refresh_token.nil? params = { client_id: @accounts.instance_variable_get(:@client_id), client_secret: @accounts.instance_variable_get(:@client_secret), grant_type: "refresh_token", refresh_token: @refresh_token } request = HTTParty.post("https://accounts.spotify.com/api/token", body: params) response = request.parsed_response.with_indifferent_access @access_token = response[:access_token] @expires_in = response[:expires_in] @expires_at = response[:expires_in] + Time.now.to_i @scopes = response[:scope] true rescue HTTParty::Error false end |
#scopes ⇒ Array
Converts the space-delimited scope list to a symbolized array.
75 76 77 78 79 |
# File 'lib/spotify/accounts/session.rb', line 75 def scopes return [] if @scopes.nil? @scopes.split(" ").map(&:to_sym) end |
#to_json(*_args) ⇒ String
Export to JSON. Designed mostly for iOS, Android, or external use cases.
163 164 165 166 167 168 169 170 |
# File 'lib/spotify/accounts/session.rb', line 163 def to_json(*_args) { access_token: @access_token.presence, expires_at: @expires_at.presence, refresh_token: @refresh_token.presence, scopes: scopes }.to_json end |