Class: ChargeValidation

Inherits:
Object
  • Object
show all
Defined in:
lib/util/validation/charge.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
32
33
34
35
36
37
38
39
40
# File 'lib/util/validation/charge.rb', line 8

def self.create(data)
  # Validate email
  raise 'Invalid email.' unless HelperValidation.is_valid_email(data[:email])

  # 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

  HelperValidation.validate_currency_code(data[:currency_code])
  source_id = data[:source_id]

  if source_id.start_with?("tkn")
    HelperValidation.validate_string_start(source_id, "tkn")
  elsif source_id.start_with?("ype")
    HelperValidation.validate_string_start(source_id, "ype")
  elsif source_id.start_with?("crd")
    HelperValidation.validate_string_start(source_id, "crd")
  else
    raise CustomException.new("Incorrect format. The format must start with tkn, ype, or crd")
  end
end

.list(data) ⇒ Object



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/util/validation/charge.rb', line 42

def self.list(data)
  # Validate email
  if data.key?('email')
    unless Helpers.is_valid_email(data[:email])
      raise CustomException.new('Invalid email.')
    end
  end

  # 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

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

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

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

  # Validate currency_code
  if data.key?('currency_code')
    Helpers.validate_currency_code(data[:currency_code])
  end

  # Validate card_brand
  if data.key?('card_brand')
    allowed_brand_values = ['Visa', 'Mastercard', 'Amex', 'Diners']
    Helpers.validate_value(data[:card_brand], allowed_brand_values)
  end

  # Validate card_type
  if data.key?('card_type')
    allowed_card_type_values = ['credito', 'debito', 'internacional']
    Helpers.validate_value(data[:card_type], allowed_card_type_values)
  end

  # Validate date filter
  if data.key?('creation_date_from') && data.key?('creation_date_to')
    Helpers.validate_date_filter(data[:creation_date_from], data[:creation_date_to])
  end

  # Validate country_code
  if data.key?('country_code')
    Helpers.validate_value(data[:country_code], get_country_codes)
  end
end