Class: K2AccessToken

Inherits:
Object
  • Object
show all
Defined in:
lib/k2-connect-ruby/k2_entity/k2_token.rb

Overview

Class for Gaining Access Token

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret) ⇒ K2AccessToken

Returns a new instance of K2AccessToken.



5
6
7
8
9
# File 'lib/k2-connect-ruby/k2_entity/k2_token.rb', line 5

def initialize(client_id, client_secret)
  validate_client_credentials(client_id, client_secret)
  @client_id = client_id
  @client_secret = client_secret
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



3
4
5
# File 'lib/k2-connect-ruby/k2_entity/k2_token.rb', line 3

def access_token
  @access_token
end

#token_responseObject (readonly)

Returns the value of attribute token_response.



3
4
5
# File 'lib/k2-connect-ruby/k2_entity/k2_token.rb', line 3

def token_response
  @token_response
end

Class Method Details

.make_hash(path_url, request, class_type, body) ⇒ Object

Create a Hash containing important details accessible for K2Connect



54
55
56
57
58
59
60
61
# File 'lib/k2-connect-ruby/k2_entity/k2_token.rb', line 54

def make_hash(path_url, request, class_type, body)
  {
      path_url: path_url,
      request_type: request,
      class_type: class_type,
      params: body
  }.with_indifferent_access
end

Instance Method Details

#introspect_token(access_token) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/k2-connect-ruby/k2_entity/k2_token.rb', line 31

def introspect_token(access_token)
  token_params = {
    client_id: @client_id,
    client_secret: @client_secret,
    token: access_token
  }
  token_hash = K2AccessToken.make_hash(K2Config.path_url('introspect_token'), 'post', 'Access Token', token_params)
  @token_response = K2Connect.make_request(token_hash)
end

#request_tokenObject



11
12
13
14
15
16
17
18
19
# File 'lib/k2-connect-ruby/k2_entity/k2_token.rb', line 11

def request_token
  token_params = {
      client_id: @client_id,
      client_secret: @client_secret,
      grant_type: 'client_credentials'
  }
  token_hash = K2AccessToken.make_hash(K2Config.path_url('oauth_token'), 'post', 'Access Token', token_params)
  @access_token = K2Connect.make_request(token_hash)
end

#revoke_token(access_token) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/k2-connect-ruby/k2_entity/k2_token.rb', line 21

def revoke_token(access_token)
  token_params = {
    client_id: @client_id,
    client_secret: @client_secret,
    token: access_token
  }
  token_hash = K2AccessToken.make_hash(K2Config.path_url('revoke_token'), 'post', 'Access Token', token_params)
  @token_response = K2Connect.make_request(token_hash)
end

#token_info(access_token) ⇒ Object



41
42
43
44
45
46
# File 'lib/k2-connect-ruby/k2_entity/k2_token.rb', line 41

def token_info(access_token)
  token_hash = K2AccessToken.make_hash(K2Config.path_url('token_info'), 'get', 'Access Token', nil)

  token_hash[:access_token] = access_token
  @token_response = K2Connect.make_request(token_hash)
end

#validate_client_credentials(client_id, client_secret) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
# File 'lib/k2-connect-ruby/k2_entity/k2_token.rb', line 48

def validate_client_credentials(client_id, client_secret)
  raise ArgumentError, 'Nil or Empty Client Id or Secret!' if client_id.blank? || client_secret.blank?
end