Class: DhanHQ::Models::TokenResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/DhanHQ/models/token_response.rb

Overview

Represents a Dhan API token response with expiry tracking and validation.

TokenResponse wraps the response from Dhan's token generation and renewal endpoints, providing convenient methods for checking token validity and determining when refresh is needed.

Examples:

From token generation

response = Auth.generate_access_token(
  dhan_client_id: "123",
  pin: "1234",
  totp: "654321"
)
token = TokenResponse.new(response)
token.expired?       # => false
token.expires_in     # => 86400 (seconds)
token.needs_refresh? # => false

Checking token status

if token.needs_refresh?(buffer_seconds: 600)
  # Refresh token 10 minutes before expiry
  new_token = Auth.renew_token(...)
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ TokenResponse

Returns a new instance of TokenResponse.



44
45
46
47
48
49
50
51
52
53
# File 'lib/DhanHQ/models/token_response.rb', line 44

def initialize(data)
  data = normalize_keys(data)

  @client_id = data["dhanClientId"]
  @client_name = data["dhanClientName"]
  @ucc = data["dhanClientUcc"]
  @power_of_attorney = data["givenPowerOfAttorney"]
  @access_token = data["accessToken"]
  @expiry_time = parse_time(data["expiryTime"])
end

Instance Attribute Details

#access_tokenString (readonly)

The authentication token

Returns:

  • (String)

    the current value of access_token



36
37
38
# File 'lib/DhanHQ/models/token_response.rb', line 36

def access_token
  @access_token
end

#client_idString (readonly)

Dhan client ID

Returns:

  • (String)

    the current value of client_id



36
37
38
# File 'lib/DhanHQ/models/token_response.rb', line 36

def client_id
  @client_id
end

#client_nameString (readonly)

Dhan client name

Returns:

  • (String)

    the current value of client_name



36
37
38
# File 'lib/DhanHQ/models/token_response.rb', line 36

def client_name
  @client_name
end

#expiry_timeTime (readonly)

Token expiration timestamp

Returns:

  • (Time)

    the current value of expiry_time



36
37
38
# File 'lib/DhanHQ/models/token_response.rb', line 36

def expiry_time
  @expiry_time
end

#power_of_attorneyBoolean (readonly)

POA status

Returns:

  • (Boolean)

    the current value of power_of_attorney



36
37
38
# File 'lib/DhanHQ/models/token_response.rb', line 36

def power_of_attorney
  @power_of_attorney
end

#uccString (readonly)

Unique client code

Returns:

  • (String)

    the current value of ucc



36
37
38
# File 'lib/DhanHQ/models/token_response.rb', line 36

def ucc
  @ucc
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/DhanHQ/models/token_response.rb', line 55

def expired?
  return true unless expiry_time

  Time.now >= expiry_time
end

#expires_inObject



61
62
63
64
65
# File 'lib/DhanHQ/models/token_response.rb', line 61

def expires_in
  return 0 unless expiry_time

  expiry_time - Time.now
end

#needs_refresh?(buffer_seconds: 300) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/DhanHQ/models/token_response.rb', line 67

def needs_refresh?(buffer_seconds: 300)
  return true unless expiry_time

  Time.now >= (expiry_time - buffer_seconds)
end