Class: DhanHQ::Models::AlertOrder

Inherits:
BaseModel
  • Object
show all
Includes:
Concerns::ApiResponseHandler
Defined in:
lib/DhanHQ/models/alert_order.rb

Overview

Model for alert/conditional orders. CRUD via AlertOrders resource; validated by AlertOrderContract.

Constant Summary collapse

HTTP_PATH =
"/v2/alerts/orders"

Constants included from ResponseHelper

ResponseHelper::STATUS_ERROR_FALLBACK

Instance Attribute Summary

Attributes inherited from BaseModel

#attributes, #errors

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseModel

api, #assign_attributes, attributes, #initialize, #new_record?, #optionchain_api?, parse_collection_response, #persisted?, resource_path, #save!, #to_request_params, #update, #valid?, validate_attributes, #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

.allObject



30
31
32
33
# File 'lib/DhanHQ/models/alert_order.rb', line 30

def all
  response = resource.all
  parse_collection_response(response)
end

.api_typeObject



18
19
20
# File 'lib/DhanHQ/models/alert_order.rb', line 18

def api_type
  :order_api
end

.create(params) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/DhanHQ/models/alert_order.rb', line 45

def create(params)
  normalized = snake_case(params)
  validate_params!(normalized, DhanHQ::Contracts::AlertOrderContract)
  response = resource.create(camelize_keys(normalized))
  return nil unless response.is_a?(Hash) && response["alertId"]

  find(response["alertId"])
end

.find(alert_id) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/DhanHQ/models/alert_order.rb', line 35

def find(alert_id)
  response = resource.find(alert_id)
  return nil unless response.is_a?(Hash) || (response.is_a?(Array) && response.any?)

  payload = response.is_a?(Array) ? response.first : response
  return nil if payload.is_a?(Hash) && payload.empty?

  new(payload, skip_validation: true)
end

.modify(alert_id, params) ⇒ AlertOrder?

Modify an existing conditional trigger/alert order.

Examples:

Modify an alert order's condition

updated = DhanHQ::Models::AlertOrder.modify("12345",
  condition: { comparing_value: 300 },
  orders: [{ quantity: 20 }]
)

Parameters:

  • alert_id (String)

    The alert ID to modify

  • params (Hash)

    Updated parameters (condition, orders, etc.)

Returns:

  • (AlertOrder, nil)

    Updated AlertOrder instance, or nil on failure



68
69
70
71
72
73
74
75
76
# File 'lib/DhanHQ/models/alert_order.rb', line 68

def modify(alert_id, params)
  normalized = snake_case(params)
  validate_params!(normalized, DhanHQ::Contracts::AlertOrderContract)
  payload = normalized.merge(alert_id: alert_id)
  response = resource.update(alert_id, camelize_keys(payload))
  return nil unless success_response?(response)

  find(alert_id)
end

.resourceObject



22
23
24
# File 'lib/DhanHQ/models/alert_order.rb', line 22

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

.validation_contractObject



26
27
28
# File 'lib/DhanHQ/models/alert_order.rb', line 26

def validation_contract
  Contracts::AlertOrderContract
end

Instance Method Details

#destroyObject Also known as: delete

rubocop:disable Naming/PredicateMethod



96
97
98
99
100
101
# File 'lib/DhanHQ/models/alert_order.rb', line 96

def destroy # rubocop:disable Naming/PredicateMethod
  return false if new_record?

  response = self.class.resource.delete(id)
  success_response?(response)
end

#idObject



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

def id
  alert_id&.to_s
end

#saveObject



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/DhanHQ/models/alert_order.rb', line 83

def save
  return false unless valid?

  payload = to_request_params
  response = if new_record?
               self.class.resource.create(payload)
             else
               self.class.resource.update(id, payload)
             end

  handle_api_response(response, success_key: new_record? ? "alertId" : nil)
end