module Quandl::Data::Cleaning

Protected Instance Methods

clean(data) click to toggle source
# File lib/quandl/data/cleaning.rb, line 9
def clean(data)
  data = data.dup if data.is_a?(Array) &&data.respond_to?(:dup)
  # check if data is dirty
  requires_cleaning = ensure_data_requires_cleaning(data)
  # short ciruit unless data is dirty
  return requires_cleaning unless requires_cleaning == true
  # ensure we're dealing with an array
  data = ensure_data_is_an_array(data)
  # clean with either format or babelfish
  known_format?( data ) ? clean_with_format(data) : clean_with_babelfish(data)
end
clean_with_babelfish(data) click to toggle source
# File lib/quandl/data/cleaning.rb, line 48
def clean_with_babelfish(data)
  data, self.headers = Quandl::Babelfish.clean(data)
  cleaned!
  data
end
clean_with_format(data) click to toggle source
# File lib/quandl/data/cleaning.rb, line 42
def clean_with_format(data)
  data = Format.parse( data )
  cleaned!
  data
end
cleaned!() click to toggle source
# File lib/quandl/data/cleaning.rb, line 54
def cleaned!
  self.cleaned = true
end
ensure_data_is_an_array(data) click to toggle source
# File lib/quandl/data/cleaning.rb, line 30
def ensure_data_is_an_array(data)
  # Hash needs conversion to array
  data = Quandl::Data::Format.hash_to_array( data )
  # String needs conversion to array
  data = Quandl::Data::Format.csv_to_array( data )
  data
end
ensure_data_requires_cleaning(data) click to toggle source
# File lib/quandl/data/cleaning.rb, line 21
def ensure_data_requires_cleaning(data)
  # skip cleaning if already clean
  return data if data.kind_of?(Array) && cleaned?
  # Return empty array if given empty string, nil, etc.
  return [] if data.blank?
  # data requires cleaning
  true
end
known_format?( data ) click to toggle source
# File lib/quandl/data/cleaning.rb, line 38
def known_format?( data )
  Format.recognized_date?( data[0][0] )
end