Class: SptyAPI::Authorization
- Inherits:
-
Object
- Object
- SptyAPI::Authorization
- Defined in:
- lib/spty_api/authorization.rb
Constant Summary collapse
- ACCOUNT_API =
'https://accounts.spotify.com/api/'
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#authorized ⇒ Object
Returns the value of attribute authorized.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#expires_in ⇒ Object
Returns the value of attribute expires_in.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#token_type ⇒ Object
Returns the value of attribute token_type.
Class Method Summary collapse
Instance Method Summary collapse
- #authorize(res) ⇒ Object
- #client_is_authorized? ⇒ Boolean
-
#initialize(type:) ⇒ Authorization
constructor
A new instance of Authorization.
- #login ⇒ Object
- #reject(res) ⇒ Object
Constructor Details
#initialize(type:) ⇒ Authorization
Returns a new instance of Authorization.
10 11 12 13 14 |
# File 'lib/spty_api/authorization.rb', line 10 def initialize(type:) if type == 'client_credentials' self.class.via_client_credentials(self) end end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
5 6 7 |
# File 'lib/spty_api/authorization.rb', line 5 def access_token @access_token end |
#authorized ⇒ Object
Returns the value of attribute authorized.
5 6 7 |
# File 'lib/spty_api/authorization.rb', line 5 def @authorized end |
#errors ⇒ Object
Returns the value of attribute errors.
5 6 7 |
# File 'lib/spty_api/authorization.rb', line 5 def errors @errors end |
#expires_in ⇒ Object
Returns the value of attribute expires_in.
5 6 7 |
# File 'lib/spty_api/authorization.rb', line 5 def expires_in @expires_in end |
#scope ⇒ Object
Returns the value of attribute scope.
5 6 7 |
# File 'lib/spty_api/authorization.rb', line 5 def scope @scope end |
#token_type ⇒ Object
Returns the value of attribute token_type.
5 6 7 |
# File 'lib/spty_api/authorization.rb', line 5 def token_type @token_type end |
Class Method Details
.via_client_credentials(auth) ⇒ Object
16 17 18 |
# File 'lib/spty_api/authorization.rb', line 16 def self.via_client_credentials(auth) auth.login end |
Instance Method Details
#authorize(res) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/spty_api/authorization.rb', line 45 def (res) self. = true self.access_token = res['access_token'] self.token_type = res['token_type'] self.expires_in = Time.now + res['expires_in'] self.scope = res['scope'] end |
#client_is_authorized? ⇒ Boolean
41 42 43 |
# File 'lib/spty_api/authorization.rb', line 41 def end |
#login ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/spty_api/authorization.rb', line 20 def login client_id = SptyAPI.configuration.client_id secret_key = SptyAPI.configuration.secret_key auth_cred = Base64.strict_encode64("#{client_id}:#{secret_key}") http = SptyAPI::HTTP::Client.new( http_method: 'post', token_type: 'Basic', access_token: auth_cred, endpoint: 'token' ) http.api_overwrite = ACCOUNT_API http.content_type_overwrite = 'application/x-www-form-urlencoded' http.body_parameter = 'grant_type=client_credentials' res = JSON.parse(http.request.read_body) res['access_token'] ? (res) : reject(res) end |
#reject(res) ⇒ Object
53 54 55 56 |
# File 'lib/spty_api/authorization.rb', line 53 def reject(res) self. = false self.errors = res end |