# frozen_string_literal: true module EodServices class Contract class <[A-Z]+)(?[0-9]{6})(?[CP]{1})[0-9]{8}$/.freeze # rubocop:enable Metrics/LineLength def contract_symbol(underlying:, expiry:, type:, strike:) return underlying if type == EodModels::OptionType::STOCK contract = underlying + expiration(expiry) + type + padded_strike(strike) unless EodServices::InstrumentType.option?(contract) raise ArgumentError, "Contract #{contract} is not a valid options contract" end contract end def underlying_symbol(contract_symbol) match = contract_symbol.match(CONTRACT_REGEX) return match[:symbol] if match contract_symbol end def contract_expiry(contract_symbol) match = contract_symbol.match(CONTRACT_REGEX) Date.parse(match[:expiry]) if match end def contract_type(contract_symbol) match = contract_symbol.match(CONTRACT_REGEX) return match[:option_type] if match EodModels::OptionType::STOCK end private def padded_strike(strike) (strike * 1000).to_i.to_s.rjust(8, '0') end def expiration(expiry) expiry.strftime('%y%m%d') end end end end