class TwitchOAuth2::Client
Client
for making requests
Constants
- CONNECTION
Public Class Methods
new( client_id:, client_secret:, redirect_uri: 'http://localhost', scopes: nil )
click to toggle source
# File lib/twitch_oauth2/client.rb, line 22 def initialize( client_id:, client_secret:, redirect_uri: 'http://localhost', scopes: nil ) @client_id = client_id @client_secret = client_secret @redirect_uri = redirect_uri @scopes = scopes end
Public Instance Methods
check_tokens(access_token: nil, refresh_token: nil, token_type: :user)
click to toggle source
# File lib/twitch_oauth2/client.rb, line 31 def check_tokens(access_token: nil, refresh_token: nil, token_type: :user) if access_token validate_result = validate access_token: access_token if validate_result[:status] == 401 return refreshed_tokens(refresh_token: refresh_token) if token_type == :user elsif validate_result[:expires_in].positive? return { access_token: access_token, refresh_token: refresh_token } end end flow token_type: token_type end
refreshed_tokens(refresh_token:)
click to toggle source
# File lib/twitch_oauth2/client.rb, line 45 def refreshed_tokens(refresh_token:) refresh(refresh_token: refresh_token).slice(:access_token, :refresh_token) end
token(token_type:, code: nil)
click to toggle source
# File lib/twitch_oauth2/client.rb, line 49 def token(token_type:, code: nil) response = CONNECTION.post( 'token', client_id: @client_id, client_secret: @client_secret, code: code, grant_type: grant_type_by_token_type(token_type), redirect_uri: @redirect_uri ) return response.body if response.success? raise Error, response.body[:message] end
Private Instance Methods
flow(token_type:)
click to toggle source
# File lib/twitch_oauth2/client.rb, line 66 def flow(token_type:) if token_type == :user raise Error.new('Use `error.metadata[:link]` for getting new tokens', link: authorize) end token(token_type: token_type).slice(:access_token, :refresh_token) end
grant_type_by_token_type(token_type)
click to toggle source
# File lib/twitch_oauth2/client.rb, line 89 def grant_type_by_token_type(token_type) case token_type when :user then :authorization_code when :application then :client_credentials else raise Error, 'unsupported token type' end end
refresh(refresh_token:)
click to toggle source
# File lib/twitch_oauth2/client.rb, line 105 def refresh(refresh_token:) response = CONNECTION.post( 'token', client_id: @client_id, client_secret: @client_secret, grant_type: :refresh_token, refresh_token: refresh_token ) return response.body if response.success? raise Error, response.body[:message] end
validate(access_token:)
click to toggle source
# File lib/twitch_oauth2/client.rb, line 97 def validate(access_token:) response = CONNECTION.get( 'validate', {}, { 'Authorization' => "OAuth #{access_token}" } ) response.body end