Class: DhanHQ::Agent::Policy
- Inherits:
-
Object
- Object
- DhanHQ::Agent::Policy
- Defined in:
- lib/DhanHQ/agent/policy.rb
Overview
Permission policy for MCP and agent-skill integrations.
Constant Summary collapse
- READ_SCOPES =
%w[portfolio:read market:read orders:read].freeze
- WRITE_SCOPES =
%w[orders:write orders:cancel alerts:write risk:write].freeze
- ALL_SCOPES =
(READ_SCOPES + WRITE_SCOPES).freeze
Instance Attribute Summary collapse
-
#scopes ⇒ Object
readonly
Returns the value of attribute scopes.
Class Method Summary collapse
Instance Method Summary collapse
- #allow?(scope) ⇒ Boolean
-
#initialize(scopes: []) ⇒ Policy
constructor
A new instance of Policy.
- #require!(scope) ⇒ Object
- #require_write!(scope) ⇒ Object
- #writes_enabled? ⇒ Boolean
Constructor Details
#initialize(scopes: []) ⇒ Policy
Returns a new instance of Policy.
13 14 15 16 17 |
# File 'lib/DhanHQ/agent/policy.rb', line 13 def initialize(scopes: []) @scopes = Array(scopes).map(&:to_s).uniq.freeze unknown = @scopes - ALL_SCOPES raise ArgumentError, "Unknown agent scopes: #{unknown.join(", ")}" if unknown.any? end |
Instance Attribute Details
#scopes ⇒ Object (readonly)
Returns the value of attribute scopes.
11 12 13 |
# File 'lib/DhanHQ/agent/policy.rb', line 11 def scopes @scopes end |
Class Method Details
.from_env ⇒ Object
23 24 25 26 |
# File 'lib/DhanHQ/agent/policy.rb', line 23 def self.from_env raw = ENV.fetch("DHANHQ_AGENT_SCOPES", READ_SCOPES.join(",")) new(scopes: raw.split(/[\s,]+/).reject(&:empty?)) end |
.read_only ⇒ Object
19 20 21 |
# File 'lib/DhanHQ/agent/policy.rb', line 19 def self.read_only new(scopes: READ_SCOPES) end |
Instance Method Details
#allow?(scope) ⇒ Boolean
28 29 30 |
# File 'lib/DhanHQ/agent/policy.rb', line 28 def allow?(scope) @scopes.include?(scope.to_s) end |
#require!(scope) ⇒ Object
32 33 34 35 36 |
# File 'lib/DhanHQ/agent/policy.rb', line 32 def require!(scope) return true if allow?(scope) raise DhanHQ::Error, "Agent scope required: #{scope}" end |
#require_write!(scope) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/DhanHQ/agent/policy.rb', line 42 def require_write!(scope) require!(scope) return true if writes_enabled? raise DhanHQ::LiveTradingDisabledError, "Agent write tools require DHANHQ_MCP_ENABLE_WRITES=true and LIVE_TRADING=true" end |
#writes_enabled? ⇒ Boolean
38 39 40 |
# File 'lib/DhanHQ/agent/policy.rb', line 38 def writes_enabled? ENV["DHANHQ_MCP_ENABLE_WRITES"] == "true" && ENV["LIVE_TRADING"] == "true" end |