Module: Eodhd::Validations
- Included in:
- Client
- Defined in:
- lib/Eodhd/Validations.rb
Instance Method Summary collapse
- #validate_arguments(exchange_code: nil, exchange_id: nil, symbol: nil, period: nil, interval: nil, from: nil, to: nil, date: nil) ⇒ Object
- #validate_date(date, param_name = 'date') ⇒ Object
- #validate_date_range(from, to) ⇒ Object
- #validate_exchange_code(exchange_code) ⇒ Object
- #validate_interval(interval) ⇒ Object
- #validate_period(period) ⇒ Object
- #validate_symbol(symbol) ⇒ Object
Instance Method Details
#validate_arguments(exchange_code: nil, exchange_id: nil, symbol: nil, period: nil, interval: nil, from: nil, to: nil, date: nil) ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/Eodhd/Validations.rb', line 6 def validate_arguments(exchange_code: nil, exchange_id: nil, symbol: nil, period: nil, interval: nil, from: nil, to: nil, date: nil) exchange_code ||= exchange_id validate_exchange_code(exchange_code) validate_symbol(symbol) validate_period(period) validate_interval(interval) validate_date(from, 'from') validate_date(to, 'to') validate_date_range(from, to) end |
#validate_date(date, param_name = 'date') ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/Eodhd/Validations.rb', line 50 def validate_date(date, param_name = 'date') return unless date case date when Date return when String unless date.match?(/\A\d{4}-\d{2}-\d{2}\z/) raise ArgumentError, "Invalid #{param_name} '#{date}'. Must be in format 'yyyy-mm-dd'" end begin Date.parse(date) rescue Date::Error raise ArgumentError, "Invalid #{param_name} '#{date}'. Not a valid date" end else raise ArgumentError, "Invalid #{param_name}. Must be String in 'yyyy-mm-dd' format or Date object" end end |
#validate_date_range(from, to) ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/Eodhd/Validations.rb', line 69 def validate_date_range(from, to) return unless from && to from_date = from.is_a?(Date) ? from : Date.parse(from.to_s) to_date = to.is_a?(Date) ? to : Date.parse(to.to_s) if from_date > to_date raise ArgumentError, "from date (#{from}) cannot be after to date (#{to})" end end |
#validate_exchange_code(exchange_code) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/Eodhd/Validations.rb', line 17 def validate_exchange_code(exchange_code) return unless exchange_code unless exchange_code.match?(/\A[A-Z]{2,6}\z/) raise ArgumentError, "Invalid exchange_code '#{exchange_code}'. Must be 2-6 uppercase letters" end end |
#validate_interval(interval) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/Eodhd/Validations.rb', line 42 def validate_interval(interval) return unless interval valid_intervals = %w[1m 5m 15m 30m 1h 4h 1d 1w 1mo] unless valid_intervals.include?(interval) raise ArgumentError, "Invalid interval: #{interval}. Must be one of: #{valid_intervals.join(', ')}" end end |
#validate_period(period) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/Eodhd/Validations.rb', line 34 def validate_period(period) return unless period valid_periods = %w[d w m] unless valid_periods.include?(period) raise ArgumentError, "Invalid period '#{period}'. Must be one of: #{valid_periods.join(', ')}" end end |
#validate_symbol(symbol) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/Eodhd/Validations.rb', line 24 def validate_symbol(symbol) return unless symbol if symbol.strip.empty? raise ArgumentError, "Symbol cannot be empty" end unless symbol.match?(/\A[A-Za-z0-9.-]{1,12}\z/) raise ArgumentError, "Invalid symbol '#{symbol}'. Must be 1-12 characters, letters/numbers/dots/hyphens only" end end |