module Alphavantage::Validations

Constants

VALID_DATATYPES
VALID_INDICATOR_INTERVALS
VALID_INTERVALS
VALID_OUTPUTSIZES
VALID_SERIES_TYPES
VALID_SLICES

Private Instance Methods

is_integer?(str) click to toggle source
# File lib/alphavantage/validations.rb, line 51
def is_integer?(str)
  Integer(str) rescue false
end
validate_datatype(value) click to toggle source
# File lib/alphavantage/validations.rb, line 37
def validate_datatype(value)
  validate_from_collection(value: value, collection: VALID_DATATYPES, type: 'data type')
end
validate_from_collection(value:, collection:, type:) click to toggle source
# File lib/alphavantage/validations.rb, line 57
def validate_from_collection(value:, collection:, type:)
  return value if collection.include?(value.to_sym)

  message = "Invalid #{type} given. Given #{value}, allowed: #{collection.map{|c| "'#{c}'"}.join(', ')}"
  raise Alphavantage::Error, message
end
validate_indicator_interval(value) click to toggle source
# File lib/alphavantage/validations.rb, line 29
def validate_indicator_interval(value)
  validate_from_collection(value: value, collection: VALID_INDICATOR_INTERVALS, type: 'interval')
end
validate_integer(label:,value:) click to toggle source
# File lib/alphavantage/validations.rb, line 41
def validate_integer(label:,value:)
  raise Alphavantage::Error, "Invalid #{label} given. Must be integer." unless is_integer?(value)
  value
end
validate_interval(value) click to toggle source
# File lib/alphavantage/validations.rb, line 21
def validate_interval(value)
  validate_from_collection(value: value, collection: VALID_INTERVALS, type: 'interval')
end
validate_mat(moving_average_type) click to toggle source
# File lib/alphavantage/validations.rb, line 46
def validate_mat(moving_average_type)
  raise Alphavantage::Error, "Invalid moving average type given." if !(0..8).include?(moving_average_type)
  moving_average_type
end
validate_outputsize(value) click to toggle source
# File lib/alphavantage/validations.rb, line 25
def validate_outputsize(value)
  validate_from_collection(value: value, collection: VALID_OUTPUTSIZES, type: 'outputsize')
end
validate_series_type(value) click to toggle source
# File lib/alphavantage/validations.rb, line 33
def validate_series_type(value)
  validate_from_collection(value: value, collection: VALID_SERIES_TYPES, type: 'series type')
end
validate_slice(value) click to toggle source
# File lib/alphavantage/validations.rb, line 17
def validate_slice(value)
  validate_from_collection(value: value, collection: VALID_SLICES, type: 'slice')
end