Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions alphavantage.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Gem::Specification.new do |spec|

spec.add_dependency "faraday", "~> 1.4"
spec.add_dependency "hashie", "~> 4.1"
spec.add_dependency "csv"

spec.add_development_dependency "bundler", "~> 2.2"
spec.add_development_dependency "rake", ">= 12.3.3"
Expand Down
3 changes: 2 additions & 1 deletion lib/alphavantage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
require "alphavantage/fundamental"
require "alphavantage/forex"
require "alphavantage/crypto"
require "alphavantage/indicator"
require "alphavantage/indicator"
require "alphavantage/options"
29 changes: 29 additions & 0 deletions lib/alphavantage/options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Alphavantage
class Options
include Validations

FUNCTIONS = {
realtime_options: 'REALTIME_OPTIONS',
historical_options: 'HISTORICAL_OPTIONS'
}

def initialize(symbol:)
@symbol = symbol
end

def realtime_options(require_greeks: false, contract: nil)
Client.get(params: { function: FUNCTIONS[__method__],
symbol: @symbol,
require_greeks: require_greeks,
contract: validate_options_contract(contract)
})
end

def historical_options(date: nil)
Client.get(params: { function: FUNCTIONS[__method__],
symbol: @symbol,
date: validate_options_date(date)
})
end
end
end
30 changes: 30 additions & 0 deletions lib/alphavantage/validations.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# frozen_string_literal: true

require 'date'

module Alphavantage
module Validations
VALID_SLICES = (1..2).map do |year|
Expand All @@ -11,6 +15,7 @@ module Validations
VALID_OUTPUTSIZES = %i{ compact full }
VALID_SERIES_TYPES = %i{ close open high low }
VALID_DATATYPES = %i{ json csv }
VALID_MINIMUM_OPTIONS_DATE = Date.parse('2008-01-01')

private

Expand Down Expand Up @@ -48,6 +53,31 @@ def validate_mat(moving_average_type)
moving_average_type
end

# the validate options contract format is IBM250725C00135000
def validate_options_contract(value)
return value if value.nil?
raise Alphavantage::Error, "Invalid options contract given. Must be in format IBM250725C00135000." \
unless !value.empty? && value =~ /^[A-Z]{1,5}\d{6}[CP]\d{8}$/
value
end

# the validate options date format is YYYY-MM-DD
# and must be greater than 2008-01-01
def validate_options_date(value)
return value if value.nil?
raise Alphavantage::Error, "Invalid date given. Must be in format YYYY-MM-DD." unless value =~ /^\d{4}-\d{2}-\d{2}$/

begin
parsed_date = Date.parse(value)
raise Alphavantage::Error, "Invalid date given. Must be greater than #{VALID_MINIMUM_OPTIONS_DATE.strftime("%Y-%m-%d")}." \
unless parsed_date >= VALID_MINIMUM_OPTIONS_DATE
rescue Date::Error
raise Alphavantage::Error, "Invalid date given. Must be in format YYYY-MM-DD."
end

value
end

def is_integer?(str)
Integer(str) rescue false
end
Expand Down
2 changes: 1 addition & 1 deletion spec/alphavantage/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@

expect(key1.class).to eq(Time)
expect(key2.class).to eq(Time)
expect(key1 < Key2).to be(true)
expect(key1 < key2).to be(true)
end
end
Loading