Sha256: aebdce94212d982395c954ff22301b770d0f4f4b8c7e76cd8d923073ac26acfa

Contents?: true

Size: 579 Bytes

Versions: 2

Compression:

Stored size: 579 Bytes

Contents

module Alphavantage
  class NormalizeKey
    def initialize(key:)
      @key = key
    end

    def call
      return @key if is_date?(@key)
      underscore_key(sanitize_key(@key))
    end

    private

    def underscore_key(key)
      key.to_s.gsub(/::/, '/').
      gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
      gsub(/([a-z\d])([A-Z])/,'\1_\2').
      tr("-", "_").
      downcase.to_sym
    end

    def sanitize_key(key)
      key.tr('.()','').gsub(/^\d+.?\s/, "").tr(' ','_')
    end

    def is_date?(key)
      !/(\d{4}-\d{2}-\d{2})/.match(key.to_s).nil?
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alphavantage-1.0.2 lib/alphavantage/normalize_key.rb
alphavantage-1.0.1 lib/alphavantage/normalize_key.rb