Class: DhanHQ::Models::TokenResponse
- Inherits:
-
Object
- Object
- DhanHQ::Models::TokenResponse
- 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.
Instance Attribute Summary collapse
-
#access_token ⇒ String
readonly
The authentication token.
-
#client_id ⇒ String
readonly
Dhan client ID.
-
#client_name ⇒ String
readonly
Dhan client name.
-
#expiry_time ⇒ Time
readonly
Token expiration timestamp.
-
#power_of_attorney ⇒ Boolean
readonly
POA status.
-
#ucc ⇒ String
readonly
Unique client code.
Instance Method Summary collapse
- #expired? ⇒ Boolean
- #expires_in ⇒ Object
-
#initialize(data) ⇒ TokenResponse
constructor
A new instance of TokenResponse.
- #needs_refresh?(buffer_seconds: 300) ⇒ Boolean
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_token ⇒ String (readonly)
The authentication token
36 37 38 |
# File 'lib/DhanHQ/models/token_response.rb', line 36 def access_token @access_token end |
#client_id ⇒ String (readonly)
Dhan client ID
36 37 38 |
# File 'lib/DhanHQ/models/token_response.rb', line 36 def client_id @client_id end |
#client_name ⇒ String (readonly)
Dhan client name
36 37 38 |
# File 'lib/DhanHQ/models/token_response.rb', line 36 def client_name @client_name end |
#expiry_time ⇒ Time (readonly)
Token expiration timestamp
36 37 38 |
# File 'lib/DhanHQ/models/token_response.rb', line 36 def expiry_time @expiry_time end |
#power_of_attorney ⇒ Boolean (readonly)
POA status
36 37 38 |
# File 'lib/DhanHQ/models/token_response.rb', line 36 def power_of_attorney @power_of_attorney end |
#ucc ⇒ String (readonly)
Unique client code
36 37 38 |
# File 'lib/DhanHQ/models/token_response.rb', line 36 def ucc @ucc end |
Instance Method Details
#expired? ⇒ 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_in ⇒ Object
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
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 |