Class: PlanValidation

Inherits:
Object
  • Object
show all
Defined in:
lib/util/validation/plan.rb

Class Method Summary collapse

Class Method Details

.create(data) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/util/validation/plan.rb', line 8

def self.create(data)
  # Validate amount
  amount = data[:amount]

  if amount.is_a?(String)
    begin
      amount = Integer(amount)
      print amount
    rescue ArgumentError
      raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
    end
  end

  unless amount.is_a?(Integer)
    raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
  end

  # Validate interval
  allowed_values = ['dias', 'semanas', 'meses', 'aƱos']
  HelperValidation.validate_value(data[:interval], allowed_values)

  # Validate currency
  HelperValidation.validate_currency_code(data[:currency_code])
end

.list(data) ⇒ Object



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
# File 'lib/util/validation/plan.rb', line 33

def self.list(data)
  # Validate amount
  if data.key?('amount')
    amount = data[:amount]

    if amount.is_a?(String)
      begin
        amount = Integer(amount)
      rescue ArgumentError
        raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
      end
    end

    unless amount.is_a?(Integer)
      raise CustomException.new("Invalid 'amount'. It should be an integer or a string representing an integer.")
    end
  end

  # Validate min_amount
  if data.key?('min_amount')
    unless data[:min_amount].is_a?(Integer)
      raise CustomException.new('Invalid min amount.')
    end
  end

  # Validate max_amount
  if data.key?('max_amount')
    unless data[:max_amount].is_a?(Integer)
      raise CustomException.new('Invalid max amount.')
    end
  end

  if data.key?('creation_date_from') && data.key?('creation_date_to')
    HelperValidation.validate_date_filter(data[:creation_date_from], data[:creation_date_to])
  end
end