Class: CF::InputValidator

Inherits:
Object
  • Object
show all
Includes:
ValidatorHelpers
Defined in:
lib/cf/input_validator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules) ⇒ InputValidator

Returns a new instance of InputValidator.



13
14
15
16
17
# File 'lib/cf/input_validator.rb', line 13

def initialize(rules)
  @errors = []
  @rules = rules
  @invalid_units, @temp_units, @valid_units = [], [], []
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



11
12
13
# File 'lib/cf/input_validator.rb', line 11

def errors
  @errors
end

#rulesObject

Returns the value of attribute rules.



11
12
13
# File 'lib/cf/input_validator.rb', line 11

def rules
  @rules
end

Class Method Details

.check_extension(file_location) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/cf/input_validator.rb', line 19

def self.check_extension(file_location)
  @error = []
  ext = File.extname(file_location).sub(/./,"")  
  formats = %w[csv xls xlsx ods]
  check = formats.include?(ext)

  @error << "Invalid file! The specified format is not supported." unless check
  {:check => check, :error => @error}
end

Instance Method Details

#parse_and_validate(inputs) ⇒ Object

returns a hash of arrays with two keys :valid_units and :invalid_units => @valid_units, :invalid_units => @invalid_units



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cf/input_validator.rb', line 31

def parse_and_validate(inputs)
  csv_contents = FasterCSV.parse(inputs)
  csv_header = csv_contents[0].map{|a| a.strip unless a.nil? }

  # Initialize rule_labels array and store labels of RULE_HEADERS into it
  rule_labels = Array.new
  @rules.each do |rule|
    label = rule[:label] || rule["label"]
    rule_labels << label.strip
  end
  
  if rule_labels == csv_header
    csv_contents[1..-1].each do |item|
      # Validation.
      rules.each_with_index do |header, index|
        next unless item # Escape nil items. This may result while reading csv file with blank line

        unless (item = item.select{|x| !x.blank? }).empty?
          item[index].strip! if !item[index].nil?
          option_header_required = header[:required] || header["required"]
          option_validation_format = header[:validation_format] || header["validation_format"]
          if required(option_header_required, item[index]) and valid(option_validation_format, item[index])
            # temporarily store valid units into temp_units array
            @temp_units << item unless @temp_units.include?(item)
          else
            # Note:: While validating input data, collect invalid row(data) and process the others.
            # Display invalid data to user later
            @invalid_units << item unless @invalid_units.include?(item)
          end
        end
      end

    end
  else
    @errors << "Headers doesnot match the column counts: Rule => #{rule_labels} against CSV => #{csv_header}"
  end

  # calculate valid_units
  @valid_units = @temp_units - @invalid_units

  {:valid_units => @valid_units, :invalid_units => @invalid_units}
end