class EodFacade::Options

Public Class Methods

call(symbol) click to toggle source
# File lib/eod_facade/options.rb, line 9
def call(symbol)
  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

  unless Cache::RedisCache.get(redis_key(underlying, expiry))
    response = make_request(url_path(underlying))
    
    unless response.success?
      raise ArgumentError, "Error fetching options data for #{underlying}"
    end

    Cache::RedisCache.set(
      redis_key(underlying, expiry),
      response.parsed_response.to_json
    )
  end

  contracts = Oj.load(Cache::RedisCache.get(redis_key(underlying, expiry)))

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

Private Class Methods

contract_hash(symbol:, contracts:, option_type:) click to toggle source
# File lib/eod_facade/options.rb, line 53
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
contract_type(symbol) click to toggle source
# File lib/eod_facade/options.rb, line 67
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
params(expiry) click to toggle source
# File lib/eod_facade/options.rb, line 42
def params(expiry)
  {
    from: expiry.to_s,
    to: expiry.to_s
  }
end
redis_key(underlying, expiry) click to toggle source
# File lib/eod_facade/options.rb, line 79
def redis_key(underlying, expiry)
  "#{underlying}_#{expiry.to_s}_options"
end
url_path(underlying) click to toggle source
# File lib/eod_facade/options.rb, line 49
def url_path(underlying)
  "/options/#{underlying}"
end