Class: DhanHQ::Configuration

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

Overview

The Configuration class manages API credentials and settings.

Use this class to set the required access_token and client_id, as well as optional settings such as the base URL and CSV URLs.

Constant Summary collapse

BASE_URL =

Default REST API host used when the base URL is not overridden.

Returns:

  • (String)
Constants::Urls::REST_API_BASE
SANDBOX_URL =

Default Sandbox API host.

Returns:

  • (String)
Constants::Urls::SANDBOX_API_BASE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Initializes a new configuration instance with default values.

Examples:

config = DhanHQ::Configuration.new
config.client_id = "your_client_id"
config.access_token = "your_access_token"


133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/DhanHQ/configuration.rb', line 133

def initialize
  @client_id = ENV.fetch("DHAN_CLIENT_ID", nil)
  @access_token = ENV.fetch("DHAN_ACCESS_TOKEN", nil)
  @sandbox        = ENV.fetch("DHAN_SANDBOX", "false").to_s.casecmp("true").zero?
  @base_url       = ENV.fetch("DHAN_BASE_URL", nil)
  @ws_version     = ENV.fetch("DHAN_WS_VERSION", 2).to_i
  @ws_order_url = ENV.fetch("DHAN_WS_ORDER_URL", nil)
  @ws_market_feed_url = ENV.fetch("DHAN_WS_MARKET_FEED_URL", nil)
  @ws_market_depth_url = ENV.fetch("DHAN_WS_MARKET_DEPTH_URL", nil)
  @market_depth_level = ENV.fetch("DHAN_MARKET_DEPTH_LEVEL", "20").to_i
  @ws_user_type   = ENV.fetch("DHAN_WS_USER_TYPE", "SELF")
  @partner_id     = ENV.fetch("DHAN_PARTNER_ID", nil)
  @partner_secret = ENV.fetch("DHAN_PARTNER_SECRET", nil)
end

Instance Attribute Details

#access_tokenString?

The access token for API authentication.

Returns:

  • (String, nil)

    The access token or nil if not set.



24
25
26
# File 'lib/DhanHQ/configuration.rb', line 24

def access_token
  @access_token
end

#access_token_providerProc?

Optional callable (Proc/lambda) that returns the access token at request time. When set, #resolved_access_token calls this instead of using #access_token.

Returns:

  • (Proc, nil)


29
30
31
# File 'lib/DhanHQ/configuration.rb', line 29

def access_token_provider
  @access_token_provider
end

#base_urlString

Returns the base URL to use. If #sandbox is true and #base_url is nil or the default production URL, returns SANDBOX_URL.

Returns:

  • (String)


115
116
117
118
119
120
121
# File 'lib/DhanHQ/configuration.rb', line 115

def base_url
  if sandbox? && (@base_url.nil? || @base_url == BASE_URL)
    SANDBOX_URL
  else
    @base_url || BASE_URL
  end
end

#client_idString?

The client ID for API authentication.

Returns:

  • (String, nil)

    The client ID or nil if not set.



20
21
22
# File 'lib/DhanHQ/configuration.rb', line 20

def client_id
  @client_id
end

#compact_csv_urlString

URL for the compact CSV format of instruments.

Returns:

  • (String)

    URL for compact CSV.



46
47
48
# File 'lib/DhanHQ/configuration.rb', line 46

def compact_csv_url
  @compact_csv_url
end

#detailed_csv_urlString

URL for the detailed CSV format of instruments.

Returns:

  • (String)

    URL for detailed CSV.



50
51
52
# File 'lib/DhanHQ/configuration.rb', line 50

def detailed_csv_url
  @detailed_csv_url
end

#market_depth_levelInteger

Market depth level (20 or 200).

Returns:

  • (Integer)


79
80
81
# File 'lib/DhanHQ/configuration.rb', line 79

def market_depth_level
  @market_depth_level
end

#on_token_expiredProc?

Optional callable invoked when the API returns 401/token-expired and the client is about to retry (when #access_token_provider is set). Use for logging or refreshing token in your store before the retry fetches a new token.

Returns:

  • (Proc, nil)


35
36
37
# File 'lib/DhanHQ/configuration.rb', line 35

def on_token_expired
  @on_token_expired
end

#partner_idString?

Partner ID for order updates when ws_user_type is "PARTNER".

Returns:

  • (String, nil)


90
91
92
# File 'lib/DhanHQ/configuration.rb', line 90

def partner_id
  @partner_id
end

#partner_secretString?

Partner secret for order updates when ws_user_type is "PARTNER".

Returns:

  • (String, nil)


94
95
96
# File 'lib/DhanHQ/configuration.rb', line 94

def partner_secret
  @partner_secret
end

#sandboxObject

Whether to use the sandbox environment.



42
43
44
# File 'lib/DhanHQ/configuration.rb', line 42

def sandbox
  @sandbox
end

#ws_market_depth_urlString

Websocket market depth endpoint. Sandbox does not support WebSocket; always returns production URL unless overridden.

Returns:

  • (String)


73
74
75
# File 'lib/DhanHQ/configuration.rb', line 73

def ws_market_depth_url
  @ws_market_depth_url || Constants::Urls::WS_DEPTH_20
end

#ws_market_feed_urlString

Websocket market feed endpoint. Sandbox does not support WebSocket; always returns production URL unless overridden.

Returns:

  • (String)


66
67
68
# File 'lib/DhanHQ/configuration.rb', line 66

def ws_market_feed_url
  @ws_market_feed_url || Constants::Urls::WS_MARKET_FEED
end

#ws_order_urlString

Websocket order updates endpoint. Sandbox does not support WebSocket; always returns production URL unless overridden.

Returns:

  • (String)


59
60
61
# File 'lib/DhanHQ/configuration.rb', line 59

def ws_order_url
  @ws_order_url || Constants::Urls::WS_ORDER_UPDATE
end

#ws_user_typeString

Websocket user type for order updates.

Returns:

  • (String)

    "SELF" or "PARTNER".



86
87
88
# File 'lib/DhanHQ/configuration.rb', line 86

def ws_user_type
  @ws_user_type
end

#ws_versionInteger

Websocket API version.

Returns:

  • (Integer)


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

def ws_version
  @ws_version
end

Instance Method Details

#resolved_access_tokenString

Returns the access token to use for this request. If #access_token_provider is set, calls it (no memoization; token per request). Otherwise returns #access_token.

Returns:

  • (String)

Raises:



101
102
103
104
105
106
107
108
109
110
# File 'lib/DhanHQ/configuration.rb', line 101

def resolved_access_token
  if access_token_provider
    token = access_token_provider.call
    raise DhanHQ::AuthenticationError, "access_token_provider returned nil or empty" if token.nil? || token.to_s.empty?

    token.to_s
  else
    access_token
  end
end

#sandbox?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/DhanHQ/configuration.rb', line 123

def sandbox?
  @sandbox == true
end