Class: SptyAPI::Authorization

Inherits:
Object
  • Object
show all
Defined in:
lib/spty_api/authorization.rb

Constant Summary collapse

ACCOUNT_API =
'https://accounts.spotify.com/api/'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_tokenObject

Returns the value of attribute access_token.



5
6
7
# File 'lib/spty_api/authorization.rb', line 5

def access_token
  @access_token
end

#authorizedObject

Returns the value of attribute authorized.



5
6
7
# File 'lib/spty_api/authorization.rb', line 5

def authorized
  @authorized
end

#errorsObject

Returns the value of attribute errors.



5
6
7
# File 'lib/spty_api/authorization.rb', line 5

def errors
  @errors
end

#expires_inObject

Returns the value of attribute expires_in.



5
6
7
# File 'lib/spty_api/authorization.rb', line 5

def expires_in
  @expires_in
end

#scopeObject

Returns the value of attribute scope.



5
6
7
# File 'lib/spty_api/authorization.rb', line 5

def scope
  @scope
end

#token_typeObject

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.
end

Instance Method Details

#authorize(res) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/spty_api/authorization.rb', line 45

def authorize(res)
  self.authorized   = 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

Returns:

  • (Boolean)


41
42
43
# File 'lib/spty_api/authorization.rb', line 41

def client_is_authorized?
  authorized
end

#loginObject



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 
  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'] ? authorize(res) : reject(res)
end

#reject(res) ⇒ Object



53
54
55
56
# File 'lib/spty_api/authorization.rb', line 53

def reject(res)
  self.authorized   = false
  self.errors       = res
end