Class: TwitchOAuth2::Tokens

Inherits:
Object
  • Object
show all
Defined in:
lib/twitch_oauth2/tokens.rb

Overview

Class for tokens and their refreshing, using provided client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, access_token: nil, refresh_token: nil, token_type: :application, scopes: nil, on_update: nil) ⇒ Tokens

I don’t know how to make it shorter rubocop:disable Metrics/ParameterLists



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/twitch_oauth2/tokens.rb', line 17

def initialize(
	client:, access_token: nil, refresh_token: nil, token_type: :application, scopes: nil,
	on_update: nil
)
	# rubocop:enable Metrics/ParameterLists
	@client = client.is_a?(Hash) ? Client.new(**client) : client
	@access_token = access_token
	@refresh_token = refresh_token
	@token_type = token_type
	@scopes = scopes
	@on_update = on_update

	@expires_at = nil
end

Instance Attribute Details

#clientObject (readonly)

‘refresh_token` for `on_update`, but it can be better to make getter with logic like for `access_token`, but there can be troubles:

  • right now ‘refresh_token` is kind of constant, but it can have TTL in the future;

  • right now there is no ‘refresh_token` for `:application` tokens, but it can appears in the future.



13
14
15
# File 'lib/twitch_oauth2/tokens.rb', line 13

def client
  @client
end

#refresh_tokenObject (readonly)

‘refresh_token` for `on_update`, but it can be better to make getter with logic like for `access_token`, but there can be troubles:

  • right now ‘refresh_token` is kind of constant, but it can have TTL in the future;

  • right now there is no ‘refresh_token` for `:application` tokens, but it can appears in the future.



13
14
15
# File 'lib/twitch_oauth2/tokens.rb', line 13

def refresh_token
  @refresh_token
end

#token_typeObject (readonly)

‘refresh_token` for `on_update`, but it can be better to make getter with logic like for `access_token`, but there can be troubles:

  • right now ‘refresh_token` is kind of constant, but it can have TTL in the future;

  • right now there is no ‘refresh_token` for `:application` tokens, but it can appears in the future.



13
14
15
# File 'lib/twitch_oauth2/tokens.rb', line 13

def token_type
  @token_type
end

Instance Method Details

#access_tokenObject

Raises:



48
49
50
51
52
# File 'lib/twitch_oauth2/tokens.rb', line 48

def access_token
	raise AuthorizeError, authorize_link if !valid? && @token_type == :user

	@access_token
end


44
45
46
# File 'lib/twitch_oauth2/tokens.rb', line 44

def authorize_link
	@client.authorize(scopes: @scopes)
end

#code=(value) ⇒ Object



54
55
56
# File 'lib/twitch_oauth2/tokens.rb', line 54

def code=(value)
	assign_tokens @client.token(token_type: @token_type, code: value)
end

#valid?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/twitch_oauth2/tokens.rb', line 32

def valid?
	if @access_token
		validate_access_token if @expires_at.nil? || Time.now >= @expires_at
	else
		return false if @token_type == :user

		request_new_tokens
	end

	true
end