Class: SimpleSpotify::Authorization
- Inherits:
-
Object
- Object
- SimpleSpotify::Authorization
- Defined in:
- lib/simplespotify/authorization.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
Class Method Summary collapse
- .from_code(code, client: {}, redirect: nil) ⇒ Object
- .login_uri(redirect, client, state: nil, scope: nil, dialog: false) ⇒ Object
Instance Method Summary collapse
- #headers ⇒ Object
-
#initialize(access_token: nil, refresh_token: nil, client: {}, token_type: nil, expires_in: nil, scope: nil) ⇒ Authorization
constructor
A new instance of Authorization.
- #on_refresh(&block) ⇒ Object
- #refresh! ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(access_token: nil, refresh_token: nil, client: {}, token_type: nil, expires_in: nil, scope: nil) ⇒ Authorization
Returns a new instance of Authorization.
46 47 48 49 50 51 |
# File 'lib/simplespotify/authorization.rb', line 46 def initialize access_token: nil, refresh_token:nil, client: {}, token_type: nil, expires_in: nil, scope: nil @access_token = access_token @refresh_token = refresh_token @client = client.to_h @client_id = @client[:client_id] end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
4 5 6 |
# File 'lib/simplespotify/authorization.rb', line 4 def access_token @access_token end |
#client_id ⇒ Object (readonly)
Returns the value of attribute client_id.
5 6 7 |
# File 'lib/simplespotify/authorization.rb', line 5 def client_id @client_id end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
4 5 6 |
# File 'lib/simplespotify/authorization.rb', line 4 def refresh_token @refresh_token end |
Class Method Details
.from_code(code, client: {}, redirect: nil) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/simplespotify/authorization.rb', line 28 def self.from_code code, client: {}, redirect: nil params = { grant_type: 'authorization_code', redirect_uri: redirect, code: code, }.merge(client.to_h) response = HTTParty.post SimpleSpotify::TOKEN_URL, query: params body = JSON.parse(response.body, symbolize_names: true) if response.code != 200 raise SimpleSpotify::Error::AuthorizationError.new(body) end return Authorization.new(body) end |
.login_uri(redirect, client, state: nil, scope: nil, dialog: false) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/simplespotify/authorization.rb', line 7 def self.login_uri redirect, client, state: nil, scope: nil, dialog: false params = { client_id: client.to_h[:client_id], response_type: 'code', redirect_uri: redirect, } params[:state] = state unless state.nil? if scope scope = scope.join(' ') if scope.is_a? Array params[:scope] = scope end params[:show_dialog] = dialog.to_s params_qs = params.map {|k,v| "#{k}=#{URI::encode(v)}" }.join('&') return SimpleSpotify::AUTHORIZATION_URL + '?' + params_qs end |
Instance Method Details
#headers ⇒ Object
54 55 56 |
# File 'lib/simplespotify/authorization.rb', line 54 def headers {'Authorization' => "Bearer #{@access_token}"} end |
#on_refresh(&block) ⇒ Object
77 78 79 |
# File 'lib/simplespotify/authorization.rb', line 77 def on_refresh &block @callback = block end |
#refresh! ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/simplespotify/authorization.rb', line 59 def refresh! params = { grant_type: 'refresh_token', refresh_token: @refresh_token }.merge(@client) response = HTTParty.post SimpleSpotify::TOKEN_URL, query: params body = JSON.parse(response.body, symbolize_names: true) if response.code != 200 raise SimpleSpotify::Error::AuthorizationError.new(body) end @access_token = body[:access_token] @callback.call(self) if @callback end |
#to_h ⇒ Object
82 83 84 85 86 87 |
# File 'lib/simplespotify/authorization.rb', line 82 def to_h { access_token: access_token, refresh_token: refresh_token } end |