Class: DhanHQ::WS::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/DhanHQ/ws/decoder.rb

Overview

Translates the binary WebSocket frames into Ruby hashes that can be consumed by client code.

Constant Summary collapse

FEED_KIND =

Mapping of feed response codes to semantic event kinds.

{
  2 => :ticker, 4 => :quote, 5 => :oi, 6 => :prev_close, 8 => :full, 50 => :disconnect, 41 => :depth_bid, 51 => :depth_ask
}.freeze

Class Method Summary collapse

Class Method Details

.decode(binary) ⇒ Hash?

Parses a binary packet and returns a normalized hash representation.

Parameters:

  • binary (String)

    Raw WebSocket frame payload.

Returns:

  • (Hash, nil)

    Normalized tick data or nil when the packet should be ignored.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/DhanHQ/ws/decoder.rb', line 21

def self.decode(binary)
  pkt = WebsocketPacketParser.new(binary).parse
  return nil if pkt.nil? || pkt.empty?

  kind   = FEED_KIND[pkt[:feed_response_code]] || :unknown
  segstr = Segments.from_code(pkt[:exchange_segment])
  sid    = pkt[:security_id].to_s

  dispatch(pkt, kind, segstr, sid)
rescue StandardError => e
  DhanHQ.logger&.debug("[DhanHQ::WS::Decoder] #{e.class}: #{e.message}")
  nil
end