Module: K2Validation
- Included in:
- K2Entity, K2Notification, K2Polling, K2Subscribe
- Defined in:
- lib/k2-connect-ruby/k2_utilities/k2_validation.rb
Overview
Module for Validating Input TODO: Correct validation of url so that it only accepts https
Constant Summary collapse
- ALL_EVENT_TYPES =
%[buygoods_transaction_received b2b_transaction_received buygoods_transaction_reversed customer_created settlement_transfer_completed m2m_transaction_received]
- TILL_SCOPE_EVENT_TYPES =
%[buygoods_transaction_received b2b_transaction_received buygoods_transaction_reversed]
Instance Method Summary collapse
- #determine_scope_details(params) ⇒ Object
-
#incorrect_keys(the_input, invalid_hash = [], the_array) ⇒ Object
Return Incorrect Key Symbols for Hashes.
-
#nil_values(the_input, nil_keys_array = []) ⇒ Object
Return Key Symbols with Blank Values.
-
#to_indifferent_access(params) ⇒ Object
Converts Hash Objects to HashWithIndifferentAccess Objects.
-
#validate_email(email) ⇒ Object
Validate Email Format.
-
#validate_hash(the_input, the_array) ⇒ Object
Validate the Hash Input Parameters.
-
#validate_input(the_input, the_array) ⇒ Object
Validating Method.
-
#validate_network(network) ⇒ Object
Validate the Network Operator.
-
#validate_phone(phone) ⇒ Object
Validate Phone Number.
- #validate_settlement_method(settlement_method) ⇒ Object
- #validate_till_number_prefix(till_number) ⇒ Object
- #validate_url(url) ⇒ Object
- #validate_webhook(params) ⇒ Object
- #validate_webhook_input(params) ⇒ Object
Instance Method Details
#determine_scope_details(params) ⇒ Object
121 122 123 124 125 |
# File 'lib/k2-connect-ruby/k2_utilities/k2_validation.rb', line 121 def determine_scope_details(params) if params[:scope].eql?('till') raise ArgumentError, "Invalid scope till for the event type" unless params[:event_type].in?(TILL_SCOPE_EVENT_TYPES) end end |
#incorrect_keys(the_input, invalid_hash = [], the_array) ⇒ Object
Return Incorrect Key Symbols for Hashes
44 45 46 47 48 49 50 51 |
# File 'lib/k2-connect-ruby/k2_utilities/k2_validation.rb', line 44 def incorrect_keys(the_input, invalid_hash = [], the_array) the_input.each_key do |key| validate_network(the_input[:network]) if key.eql?("network") validate_settlement_method(the_input[:settlement_method]) if key.eql?("settlement_method") invalid_hash << key unless the_array.include?(key.to_s) end raise K2IncorrectParams, invalid_hash if invalid_hash.present? end |
#nil_values(the_input, nil_keys_array = []) ⇒ Object
Return Key Symbols with Blank Values
54 55 56 57 58 59 |
# File 'lib/k2-connect-ruby/k2_utilities/k2_validation.rb', line 54 def nil_values(the_input, nil_keys_array = []) the_input.select { |_, v| v.blank? }.each_key do |key| nil_keys_array << key.to_s end raise K2EmptyParams, nil_keys_array unless nil_keys_array.blank? end |
#to_indifferent_access(params) ⇒ Object
Converts Hash Objects to HashWithIndifferentAccess Objects
93 94 95 |
# File 'lib/k2-connect-ruby/k2_utilities/k2_validation.rb', line 93 def to_indifferent_access(params) params.with_indifferent_access end |
#validate_email(email) ⇒ Object
Validate Email Format
76 77 78 79 80 81 |
# File 'lib/k2-connect-ruby/k2_utilities/k2_validation.rb', line 76 def validate_email(email) unless email.blank? raise ArgumentError, 'Invalid Email Address.' unless email.match(URI::MailTo::EMAIL_REGEXP).present? end email end |
#validate_hash(the_input, the_array) ⇒ Object
Validate the Hash Input Parameters
38 39 40 41 |
# File 'lib/k2-connect-ruby/k2_utilities/k2_validation.rb', line 38 def validate_hash(the_input, the_array) nil_values(the_input) incorrect_keys(the_input, the_array) end |
#validate_input(the_input, the_array) ⇒ Object
Validating Method
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 |
# File 'lib/k2-connect-ruby/k2_utilities/k2_validation.rb', line 8 def validate_input(the_input, the_array) if the_input.blank? raise ArgumentError, "Empty or Nil Input!\n No Input Content has been given." else unless !!the_input == the_input if the_input.is_a?(Hash) validate_hash(the_input.with_indifferent_access, the_array) else begin if the_input.is_a?(String) raise ArgumentError, "Wrong Input Format.\n The Input is a String." elsif the_input.is_a?(Integer) raise ArgumentError, "Wrong Input Format.\n The Input is an Integer." elsif the_input.key?(:authenticity_token) nil_values(the_input.permit(the_array).to_hash.with_indifferent_access) else raise ArgumentError, "Undefined Input Format.\n The Input is Neither a Hash nor an ActionController::Parameter Object." end rescue NoMethodError => nme if nme..include?('has_key?') raise ArgumentError, "Undefined Input Format.\n The Input is Neither a Hash nor an ActionController::Parameter Object." end end end end end to_indifferent_access(the_input) end |
#validate_network(network) ⇒ Object
Validate the Network Operator
84 85 86 |
# File 'lib/k2-connect-ruby/k2_utilities/k2_validation.rb', line 84 def validate_network(network) raise ArgumentError, "Invalid Network Operator." unless K2Config.network_operators.include?(network.to_s.upcase) end |
#validate_phone(phone) ⇒ Object
Validate Phone Number
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/k2-connect-ruby/k2_utilities/k2_validation.rb', line 62 def validate_phone(phone) raise ArgumentError, "No Phone Number given." if phone.blank? # Kenyan Phone Numbers unless phone.blank? if phone[-(number = phone.to_i.to_s.size).to_i, 3].eql?(254.to_s) raise ArgumentError, 'Invalid Kenyan Phone Number.' unless phone[-9, 9][0].eql?(7.to_s) else raise ArgumentError, 'Invalid Phone Number.' unless number.eql?(9) end phone.tr('+', '') end end |
#validate_settlement_method(settlement_method) ⇒ Object
88 89 90 |
# File 'lib/k2-connect-ruby/k2_utilities/k2_validation.rb', line 88 def validate_settlement_method(settlement_method) raise ArgumentError, "Wrong Settlement Method" unless settlement_method.eql?("EFT") || settlement_method.eql?("RTS") end |
#validate_till_number_prefix(till_number) ⇒ Object
102 103 104 105 106 |
# File 'lib/k2-connect-ruby/k2_utilities/k2_validation.rb', line 102 def validate_till_number_prefix(till_number) raise ArgumentError, "No Till Number given." if till_number.blank? raise ArgumentError, "Invalid Till Number format." unless till_number[0, 1].eql?('K') till_number end |
#validate_url(url) ⇒ Object
97 98 99 100 |
# File 'lib/k2-connect-ruby/k2_utilities/k2_validation.rb', line 97 def validate_url(url) raise ArgumentError, 'Invalid URL Format.' unless url =~ /\A#{URI.regexp(%w[https http])}\z/ url end |
#validate_webhook(params) ⇒ Object
108 109 110 111 |
# File 'lib/k2-connect-ruby/k2_utilities/k2_validation.rb', line 108 def validate_webhook(params) raise ArgumentError, 'Subscription Service does not Exist!' unless params[:event_type].in?(ALL_EVENT_TYPES) determine_scope_details(params) end |
#validate_webhook_input(params) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/k2-connect-ruby/k2_utilities/k2_validation.rb', line 113 def validate_webhook_input(params) if params[:event_type].in?(TILL_SCOPE_EVENT_TYPES) validate_input(params, %w[event_type scope scope_reference url]) else validate_input(params.except(:scope_reference), %w[event_type scope url]) end end |