Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
3.0 KB
Raw Blame
module Trading212Account::DataHelpers
  extend ActiveSupport::Concern

  private

    def instruments_map
      @trading212_account&.instruments_map || {}
    end

    def parse_decimal(value)
      return nil if value.nil?

      normalized = value.is_a?(String) ? value.strip : value.to_s
      return nil if normalized.blank?

      BigDecimal(normalized)
    rescue ArgumentError
      nil
    end

    def parse_date(value)
      return nil if value.blank?

      case value
      when DateTime, Time, ActiveSupport::TimeWithZone
        value.to_date
      when Date
        value
      else
        Time.zone.parse(value.to_s)&.to_date || Date.parse(value.to_s)
      end
    rescue ArgumentError, TypeError
      nil
    end

    # T212 quotes London listings in pence (GBX). Prices and amounts are
    # stored in the major unit, as the price providers do.
    MINOR_UNIT_CURRENCIES = {
      "GBX" => { currency: "GBP", multiplier: BigDecimal("0.01") }
    }.freeze

    def normalize_minor_units(currency_code, *amounts)
      conversion = MINOR_UNIT_CURRENCIES[currency_code.to_s.upcase]
      return [ currency_code, *amounts ] unless conversion

      [ conversion[:currency], *amounts.map { |amount| amount && amount * conversion[:multiplier] } ]
    end

    # T212 tickers look like "AAPL_US_EQ". Derive a standard ticker by taking
    # the first segment, which is the instrument symbol on its primary exchange.
    def standard_ticker(t212_ticker)
      t212_ticker.to_s.split("_").first&.upcase || ""
    end

    def resolve_security_for_ticker(t212_ticker)
      instrument = instruments_map[t212_ticker]

      if instrument
        resolve_security_from_instrument(instrument, t212_ticker)
      else
        ticker = standard_ticker(t212_ticker)
        Security.find_by(ticker: ticker) || Security.create!(ticker: ticker, name: ticker)
      end
    rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
      ticker = standard_ticker(t212_ticker)
      Security.find_by(ticker: ticker)
    end

    def resolve_security_from_instrument(instrument, t212_ticker)
      ticker = standard_ticker(t212_ticker)
      name = instrument["shortName"].presence || ticker

      Security.find_by(ticker: ticker) ||
        Security.create!(ticker: ticker, name: name)
    rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
      Security.find_by(ticker: ticker)
    end

    # Resolve (or create) a Security when we already have isin/ticker/name in hand,
    # e.g. from the nested instrument object in positions and orders responses.
    # Note: the securities table has no isin column; isin is accepted but unused.
    def resolve_security_direct(isin, ticker, name)
      Security.find_by(ticker: ticker) ||
        Security.create!(ticker: ticker, name: name)
    rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
      Security.find_by(ticker: ticker)
    end

    def instrument_currency(t212_ticker)
      instrument = instruments_map[t212_ticker]
      instrument&.dig("currencyCode").presence || currency
    end
end