Class: OrderValidation

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

Class Method Summary collapse

Class Method Details

.create(data) ⇒ Object

Raises:



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

def self.create(data)
  # Validate 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

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

  # Validate firstname, lastname, and phone
  client_details = data[:client_details] || {}
  raise CustomException.new('first name is empty.') if client_details[:first_name].nil? || client_details[:first_name].empty?
  raise CustomException.new('last name is empty.') if client_details[:last_name].nil? || client_details[:last_name].empty?
  raise CustomException.new('phone_number is empty.') if client_details[:phone_number].nil? || client_details[:phone_number].empty?

  # Validate email
  raise CustomException.new('Invalid email.') unless HelperValidation.is_valid_email(client_details[:email])

  # Validate expiration date
  raise CustomException.new('expiration_date must be a future date.') unless HelperValidation.is_future_date(data[:expiration_date])
end

.list(data) ⇒ Object



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
73
74
75
# File 'lib/util/validation/order.rb', line 40

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