Centralize sdk_object_to_hash logic in DataHelpers module and update all references for improved reusability and consistency. Add error handling for partial and failed SnapTrade account linking. (#741)

Co-authored-by: luckyPipewrench <luckypipewrench@proton.me>
This commit is contained in:
LPW
2026-01-22 16:19:44 -05:00
committed by GitHub
parent bf76d6b88d
commit 9858b36dc7
6 changed files with 41 additions and 52 deletions

View File

@@ -3,6 +3,23 @@ module SnaptradeAccount::DataHelpers
private
# Convert SnapTrade SDK objects to hashes
# SDK objects don't have proper to_h but do have to_json
# Uses JSON round-trip to ensure all nested objects become hashes
def sdk_object_to_hash(obj)
return obj if obj.is_a?(Hash)
if obj.respond_to?(:to_json)
JSON.parse(obj.to_json)
elsif obj.respond_to?(:to_h)
obj.to_h
else
obj
end
rescue JSON::ParserError, TypeError
obj.respond_to?(:to_h) ? obj.to_h : {}
end
def parse_decimal(value)
return nil if value.nil?