Module: Quandl::Data::Cleaning

Extended by:
ActiveSupport::Concern
Included in:
Quandl::Data
Defined in:
lib/quandl/data/cleaning.rb

Instance Method Summary collapse

Instance Method Details

#clean(data) ⇒ Object (protected)



23
24
25
26
27
28
29
30
31
32
# File 'lib/quandl/data/cleaning.rb', line 23

def clean(data)
  # 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) ⇒ Object (protected)



63
64
65
66
67
# File 'lib/quandl/data/cleaning.rb', line 63

def clean_with_babelfish(data)
  data, self.headers = Quandl::Babelfish.clean(data)
  cleaned!
  data
end

#clean_with_format(data) ⇒ Object (protected)



57
58
59
60
61
# File 'lib/quandl/data/cleaning.rb', line 57

def clean_with_format(data)
  data = Format.parse( data )
  cleaned!
  data
end

#cleanedObject



7
8
9
# File 'lib/quandl/data/cleaning.rb', line 7

def cleaned
  @cleaned
end

#cleaned!Object (protected)



69
70
71
# File 'lib/quandl/data/cleaning.rb', line 69

def cleaned!
  self.cleaned = true
end

#cleaned=(value) ⇒ Object



10
11
12
# File 'lib/quandl/data/cleaning.rb', line 10

def cleaned=(value)
  @cleaned = (value == true)
end

#cleaned?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/quandl/data/cleaning.rb', line 13

def cleaned?
  cleaned == true
end

#ensure_data_is_an_array(data) ⇒ Object (protected)



45
46
47
48
49
50
51
# File 'lib/quandl/data/cleaning.rb', line 45

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_is_cleanedObject



17
18
19
# File 'lib/quandl/data/cleaning.rb', line 17

def ensure_data_is_cleaned
  data_array unless cleaned?
end

#ensure_data_requires_cleaning(data) ⇒ Object (protected)



34
35
36
37
38
39
40
41
42
43
# File 'lib/quandl/data/cleaning.rb', line 34

def ensure_data_requires_cleaning(data)
  # skip cleaning if already clean
  return data if data.kind_of?(Array) && cleaned?
  # Quandl::Data is already clean, but to avoid errors extract internal array
  return data.to_date.to_a if data.kind_of?(Quandl::Data)
  # Return empty array if given empty string, nil, etc.
  return [] if data.blank?
  # data requires cleaning
  true
end

#known_format?(data) ⇒ Boolean (protected)

Returns:

  • (Boolean)


53
54
55
# File 'lib/quandl/data/cleaning.rb', line 53

def known_format?( data )
  Format.recognized_date?( data[0][0] )
end