Class: DhanHQ::Models::Profile

Inherits:
BaseModel show all
Defined in:
lib/DhanHQ/models/profile.rb

Overview

Model for retrieving authenticated user profile and account information.

The User Profile API can be used to check the validity of access token and account setup. It is a simple GET request and serves as a great test API to start integration with DhanHQ. The profile contains account-level metadata including token validity, active segments, DDPI status, MTF consent, and Data API subscription status.

Examples:

Fetch user profile to verify token validity

profile = DhanHQ::Models::Profile.fetch
if profile
  puts "Client ID: #{profile.dhan_client_id}"
  puts "Token Valid Until: #{profile.token_validity}"
  puts "Active Segments: #{profile.active_segment}"
  puts "DDPI: #{profile.ddpi}"
  puts "MTF: #{profile.mtf}"
  puts "Data Plan: #{profile.data_plan}"
end

Check account status

profile = DhanHQ::Models::Profile.fetch
if profile
  if profile.ddpi == "Active" && profile.mtf == "Active"
    puts "Account is fully active"
  else
    puts "Some features may be inactive"
  end
end

Verify data API subscription

profile = DhanHQ::Models::Profile.fetch
if profile && profile.data_plan == "Active"
  puts "Data API subscription is active until: #{profile.data_validity}"
else
  puts "Data API subscription is not active"
end

Constant Summary collapse

HTTP_PATH =

Base path for profile retrieval.

"/v2/profile"

Constants included from ResponseHelper

ResponseHelper::STATUS_ERROR_FALLBACK

Instance Attribute Summary

Attributes inherited from BaseModel

#attributes, #errors

Class Method Summary collapse

Methods inherited from BaseModel

all, api, api_type, #assign_attributes, attributes, create, #delete, #destroy, find, #id, #initialize, #new_record?, #optionchain_api?, parse_collection_response, #persisted?, resource_path, #save, #save!, #to_request_params, #update, #valid?, validate_attributes, validation_contract, #validation_contract, where

Methods included from APIHelper

#handle_response

Methods included from AttributeHelper

#camelize_keys, #inspect, #normalize_keys, #snake_case, #titleize_keys

Methods included from ValidationHelper

#valid?, #validate!, #validate_params!

Methods included from RequestHelper

#build_from_response

Constructor Details

This class inherits a constructor from DhanHQ::BaseModel

Class Method Details

.fetchProfile?

Fetches the authenticated user’s profile details and account information.

Retrieves account-level metadata including token validity, active segments, DDPI status, MTF consent status, and Data API subscription status. This is a simple GET request that serves as a great test API to verify your access token and account setup before starting integration.

Examples:

Fetch profile to verify integration

profile = DhanHQ::Models::Profile.fetch
if profile
  puts "✓ Access token is valid"
  puts "✓ Account setup complete"
  puts "Client ID: #{profile.dhan_client_id}"
else
  puts "✗ Failed to fetch profile - check access token"
end

Check account features

profile = DhanHQ::Models::Profile.fetch
if profile
  puts "Token valid until: #{profile.token_validity}"
  puts "Active segments: #{profile.active_segment}"
  puts "DDPI: #{profile.ddpi}"
  puts "MTF: #{profile.mtf}"
  puts "Data Plan: #{profile.data_plan}"
  if profile.data_plan == "Active"
    puts "Data API valid until: #{profile.data_validity}"
  end
end

Verify account is ready for trading

profile = DhanHQ::Models::Profile.fetch
if profile
  ready = profile.ddpi == "Active" && profile.mtf == "Active"
  if ready
    puts "Account is ready for trading"
  else
    puts "Account may have restrictions:"
    puts "  DDPI: #{profile.ddpi}"
    puts "  MTF: #{profile.mtf}"
  end
end

Returns:

  • (Profile, nil)

    Profile object with account details if fetch succeeds, nil otherwise. Response structure (keys normalized to snake_case):

    • :dhan_client_id [String] User-specific identification generated by Dhan

    • :token_validity [String] Validity date and time for access token. Format: “DD/MM/YYYY HH:MM” (e.g., “30/03/2025 15:37”)

    • :active_segment [String] All active segments in user account. Comma-separated list (e.g., “Equity, Derivative, Currency, Commodity”)

    • :ddpi [String] DDPI (Demat Debit Power of Attorney) status of the user. Valid values: “Active”, “Deactive”

    • :mtf [String] MTF (Margin Trading Facility) consent status of the user. Valid values: “Active”, “Deactive”

    • :data_plan [String] Data API subscription status. Valid values: “Active”, “Deactive”

    • :data_validity [String] Validity date and time for Data API Subscription. Format: “YYYY-MM-DD HH:MM:SS.S” (e.g., “2024-12-05 09:37:52.0”)



118
119
120
121
122
123
# File 'lib/DhanHQ/models/profile.rb', line 118

def fetch
  response = resource.fetch
  return nil unless response.is_a?(Hash)

  new(response, skip_validation: true)
end

.resourceDhanHQ::Resources::Profile

Provides a shared instance of the Profile resource.

Returns:



54
55
56
# File 'lib/DhanHQ/models/profile.rb', line 54

def resource
  @resource ||= DhanHQ::Resources::Profile.new
end