Sha256: 1885ef2ad5c0aebde6a5afb24e7b88fd9a5d3cfc1fb5ba99cbdd6ccace02a71d

Contents?: true

Size: 1.94 KB

Versions: 5

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

require 'oj'

module EodFacade
  class Options < ::EodFacade::Base
    class << self
      def call(symbol)
        # unless EodServices::InstrumentType.option?(symbol)
        #   raise ArgumentError, "Option contract #{symbol} not found or is invalid"
        # end

        underlying = EodServices::Contract.underlying_symbol(symbol)
        expiry = EodServices::Contract.contract_expiry(symbol)
        option_type = contract_type(symbol)

        unless expiry
          raise ArgumentError, "Invalid expiration date for option #{symbol}"
        end

        response = make_request(url_path(underlying), params(expiry))

        unless response.success?
          raise ArgumentError, "Error fetching options data for #{symbol}"
        end

        contracts = response.parsed_response

        contract_hash(
          symbol: symbol,
          contracts: contracts,
          option_type: option_type
        )
      end

      private

      def params(expiry)
        {
          from: expiry.to_s,
          to: expiry.to_s
        }
      end

      def url_path(underlying)
        "/options/#{underlying}"
      end

      def contract_hash(symbol:, contracts:, option_type:)
        by_expiration = contracts['data'].first
        expiration_date = by_expiration && by_expiration['expirationDate']

        by_option_type = by_expiration && by_expiration['options'][option_type]
        contract = by_option_type&.find do |c|
          c['contractName'] == symbol
        end

        raise ArgumentError, "Contract #{symbol} not found" unless contract

        contract.merge('expirationDate' => expiration_date)
      end

      def contract_type(symbol)
        option_type = EodServices::Contract.contract_type(symbol)
        case option_type
        when EodModels::OptionType::CALL
          'CALL'
        when EodModels::OptionType::PUT
          'PUT'
        else
          'STOCK'
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sensei_eod_utils-0.0.31 lib/eod_facade/options.rb
sensei_eod_utils-0.0.30 lib/eod_facade/options.rb
sensei_eod_utils-0.0.29 lib/eod_facade/options.rb
sensei_eod_utils-0.0.28 lib/eod_facade/options.rb
sensei_eod_utils-0.0.27 lib/eod_facade/options.rb