Class: DhanHQ::Configuration
- Inherits:
-
Object
- Object
- DhanHQ::Configuration
- 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.
Constants::Urls::REST_API_BASE
- SANDBOX_URL =
Default Sandbox API host.
Constants::Urls::SANDBOX_API_BASE
Instance Attribute Summary collapse
-
#access_token ⇒ String?
The access token for API authentication.
-
#access_token_provider ⇒ Proc?
Optional callable (Proc/lambda) that returns the access token at request time.
-
#base_url ⇒ String
Returns the base URL to use.
-
#client_id ⇒ String?
The client ID for API authentication.
-
#compact_csv_url ⇒ String
URL for the compact CSV format of instruments.
-
#detailed_csv_url ⇒ String
URL for the detailed CSV format of instruments.
-
#market_depth_level ⇒ Integer
Market depth level (20 or 200).
-
#on_token_expired ⇒ Proc?
Optional callable invoked when the API returns 401/token-expired and the client is about to retry (when #access_token_provider is set).
-
#partner_id ⇒ String?
Partner ID for order updates when
ws_user_typeis "PARTNER". -
#partner_secret ⇒ String?
Partner secret for order updates when
ws_user_typeis "PARTNER". -
#sandbox ⇒ Object
Whether to use the sandbox environment.
-
#ws_market_depth_url ⇒ String
Websocket market depth endpoint.
-
#ws_market_feed_url ⇒ String
Websocket market feed endpoint.
-
#ws_order_url ⇒ String
Websocket order updates endpoint.
-
#ws_user_type ⇒ String
Websocket user type for order updates.
-
#ws_version ⇒ Integer
Websocket API version.
Instance Method Summary collapse
-
#initialize ⇒ Configuration
constructor
Initializes a new configuration instance with default values.
-
#resolved_access_token ⇒ String
Returns the access token to use for this request.
- #sandbox? ⇒ Boolean
Constructor Details
#initialize ⇒ Configuration
Initializes a new configuration instance with default values.
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_token ⇒ String?
The access token for API authentication.
24 25 26 |
# File 'lib/DhanHQ/configuration.rb', line 24 def access_token @access_token end |
#access_token_provider ⇒ Proc?
Optional callable (Proc/lambda) that returns the access token at request time. When set, #resolved_access_token calls this instead of using #access_token.
29 30 31 |
# File 'lib/DhanHQ/configuration.rb', line 29 def access_token_provider @access_token_provider end |
#base_url ⇒ String
Returns the base URL to use. If #sandbox is true and #base_url is nil or the default production URL, returns SANDBOX_URL.
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_id ⇒ String?
The client ID for API authentication.
20 21 22 |
# File 'lib/DhanHQ/configuration.rb', line 20 def client_id @client_id end |
#compact_csv_url ⇒ String
URL for the compact CSV format of instruments.
46 47 48 |
# File 'lib/DhanHQ/configuration.rb', line 46 def compact_csv_url @compact_csv_url end |
#detailed_csv_url ⇒ String
URL for the detailed CSV format of instruments.
50 51 52 |
# File 'lib/DhanHQ/configuration.rb', line 50 def detailed_csv_url @detailed_csv_url end |
#market_depth_level ⇒ Integer
Market depth level (20 or 200).
79 80 81 |
# File 'lib/DhanHQ/configuration.rb', line 79 def market_depth_level @market_depth_level end |
#on_token_expired ⇒ Proc?
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.
35 36 37 |
# File 'lib/DhanHQ/configuration.rb', line 35 def on_token_expired @on_token_expired end |
#partner_id ⇒ String?
Partner ID for order updates when ws_user_type is "PARTNER".
90 91 92 |
# File 'lib/DhanHQ/configuration.rb', line 90 def partner_id @partner_id end |
#partner_secret ⇒ String?
Partner secret for order updates when ws_user_type is "PARTNER".
94 95 96 |
# File 'lib/DhanHQ/configuration.rb', line 94 def partner_secret @partner_secret end |
#sandbox ⇒ Object
Whether to use the sandbox environment.
42 43 44 |
# File 'lib/DhanHQ/configuration.rb', line 42 def sandbox @sandbox end |
#ws_market_depth_url ⇒ String
Websocket market depth endpoint. Sandbox does not support WebSocket; always returns production URL unless overridden.
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_url ⇒ String
Websocket market feed endpoint. Sandbox does not support WebSocket; always returns production URL unless overridden.
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_url ⇒ String
Websocket order updates endpoint. Sandbox does not support WebSocket; always returns production URL unless overridden.
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_type ⇒ String
Websocket user type for order updates.
86 87 88 |
# File 'lib/DhanHQ/configuration.rb', line 86 def ws_user_type @ws_user_type end |
#ws_version ⇒ Integer
Websocket API version.
54 55 56 |
# File 'lib/DhanHQ/configuration.rb', line 54 def ws_version @ws_version end |
Instance Method Details
#resolved_access_token ⇒ String
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.
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
123 124 125 |
# File 'lib/DhanHQ/configuration.rb', line 123 def sandbox? @sandbox == true end |