Class: DhanHQ::Models::Postback

Inherits:
BaseModel show all
Defined in:
lib/DhanHQ/models/postback.rb

Overview

Note:

Postback URL is configured in the Dhan web console when generating an access token. It will NOT work with localhost or 127.0.0.1.

Utility model for parsing Dhan postback (webhook) payloads.

Postback is a webhook mechanism where Dhan pushes order status updates to your configured URL. This model provides a convenient way to parse the incoming JSON payload into a typed, attribute-accessible object.

Examples:

Parse postback payload in a Rails controller

class DhanWebhooksController < ApplicationController
  skip_before_action :verify_authenticity_token

  def create
    postback = DhanHQ::Models::Postback.parse(request.body.read)
    case postback.order_status
    when "TRADED"
      handle_fill(postback)
    when "REJECTED"
      handle_rejection(postback)
    end
    head :ok
  end
end

Parse postback payload from a hash

postback = DhanHQ::Models::Postback.parse(params)
puts "Order #{postback.order_id} is now #{postback.order_status}"
puts "Filled: #{postback.filled_qty}/#{postback.quantity}"

Constant Summary collapse

HTTP_PATH =

No API endpoint — postback is pushed to the user

nil

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

all, api, api_type, #assign_attributes, attributes, create, #delete, #destroy, find, #id, #initialize, #new_record?, #optionchain_api?, parse_collection_response, #persisted?, resource, resource_path, #save, #save!, #to_request_params, #update, #valid?, validate_attributes, validation_contract, #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

.parse(payload) ⇒ Postback

Parse a postback webhook payload into a Postback model instance.

Accepts either a JSON string (from request body) or a Hash (from parsed params). Keys are normalized to snake_case automatically.

Examples:

From raw JSON string

postback = DhanHQ::Models::Postback.parse('{"orderId":"123","orderStatus":"TRADED"}')
puts postback.order_status # => "TRADED"

From a hash

postback = DhanHQ::Models::Postback.parse(order_id: "123", order_status: "TRADED")
puts postback.order_id # => "123"

Parameters:

  • payload (String, Hash)

    Raw JSON string or Hash from the webhook

Returns:

  • (Postback)

    Parsed Postback object with typed attributes



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

def parse(payload)
  data = case payload
         when String
           JSON.parse(payload)
         when Hash
           payload
         else
           raise ArgumentError, "Expected String or Hash, got #{payload.class}"
         end

  new(data, skip_validation: true)
end

Instance Method Details

#cancelled?Boolean

Whether the order was cancelled.

Returns:

  • (Boolean)


109
110
111
# File 'lib/DhanHQ/models/postback.rb', line 109

def cancelled?
  order_status == DhanHQ::Constants::OrderStatus::CANCELLED
end

#pending?Boolean

Whether the order is still pending.

Returns:

  • (Boolean)


101
102
103
# File 'lib/DhanHQ/models/postback.rb', line 101

def pending?
  order_status == DhanHQ::Constants::OrderStatus::PENDING
end

#rejected?Boolean

Whether the order was rejected.

Returns:

  • (Boolean)


93
94
95
# File 'lib/DhanHQ/models/postback.rb', line 93

def rejected?
  order_status == DhanHQ::Constants::OrderStatus::REJECTED
end

#traded?Boolean

Whether the order has been fully traded.

Returns:

  • (Boolean)


85
86
87
# File 'lib/DhanHQ/models/postback.rb', line 85

def traded?
  order_status == DhanHQ::Constants::OrderStatus::TRADED
end