Class: DhanHQ::Models::AlertOrder
- 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
Class Method Summary collapse
- .all ⇒ Object
- .api_type ⇒ Object
- .create(params) ⇒ Object
- .find(alert_id) ⇒ Object
-
.modify(alert_id, params) ⇒ AlertOrder?
Modify an existing conditional trigger/alert order.
- .resource ⇒ Object
- .validation_contract ⇒ Object
Instance Method Summary collapse
-
#destroy ⇒ Object
(also: #delete)
rubocop:disable Naming/PredicateMethod.
- #id ⇒ Object
- #save ⇒ Object
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
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
Constructor Details
This class inherits a constructor from DhanHQ::BaseModel
Class Method Details
.all ⇒ Object
30 31 32 33 |
# File 'lib/DhanHQ/models/alert_order.rb', line 30 def all response = resource.all parse_collection_response(response) end |
.api_type ⇒ Object
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.
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 |
.resource ⇒ Object
22 23 24 |
# File 'lib/DhanHQ/models/alert_order.rb', line 22 def resource @resource ||= DhanHQ::Resources::AlertOrders.new end |
.validation_contract ⇒ Object
26 27 28 |
# File 'lib/DhanHQ/models/alert_order.rb', line 26 def validation_contract Contracts::AlertOrderContract end |
Instance Method Details
#destroy ⇒ Object 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 |
#id ⇒ Object
79 80 81 |
# File 'lib/DhanHQ/models/alert_order.rb', line 79 def id alert_id&.to_s end |
#save ⇒ Object
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 |