Class: Kiq::CLI::Backer
- Inherits:
-
Object
- Object
- Kiq::CLI::Backer
- Defined in:
- lib/kiq/backer.rb
Overview
Hosts logic for the backer object
Constant Summary collapse
- @@instance_collector =
[]
Instance Attribute Summary collapse
-
#amount ⇒ Object
Allows access to name, credit_card, and amount.
-
#credit_card ⇒ Object
Allows access to name, credit_card, and amount.
-
#name ⇒ Object
Allows access to name, credit_card, and amount.
Class Method Summary collapse
-
.check_amount_dollar_sign(amount) ⇒ Boolean
Checks if the amount includes dollar signs.
-
.check_card_length(credit_card) ⇒ Boolean
Checks if the credit card is under 19 numbers long.
-
.check_card_luhn_10(credit_card) ⇒ Boolean
Checks if the credit card passes the Luhn-10 algorithm.
-
.check_card_numeric(credit_card) ⇒ Boolean
Checks if the credit card only contains numbers.
-
.check_card_uniqueness(project, credit_card) ⇒ Boolean
Checks if the credit card is unique, no other Backer exists with that card.
-
.check_name_characters(name) ⇒ Boolean
Checks if the name has non-alphanumeric characters besides underscores and dashes.
-
.check_name_length(name) ⇒ Boolean
Checks if the string is between 4 and 20 characters.
-
.luhn_valid?(credit_card) ⇒ Boolean
Computes Luhn-10 algorithm.
-
.validate_backer_name(backer_name) ⇒ Boolean
Checks all validation methods for a Backer’s name.
-
.validate_card(project, credit_card) ⇒ Boolean
Checks all validation methods for a credit card.
-
.validate_project_exists(project) ⇒ Boolean
Check if project exists.
Instance Method Summary collapse
-
#initialize(name, credit_card, amount) ⇒ Backer
constructor
Initalizes a new Backer object.
Constructor Details
#initialize(name, credit_card, amount) ⇒ Backer
Initalizes a new Backer object
13 14 15 16 17 |
# File 'lib/kiq/backer.rb', line 13 def initialize(name, credit_card, amount) @name = name @amount = amount @credit_card = credit_card end |
Instance Attribute Details
#amount ⇒ Object
Allows access to name, credit_card, and amount
7 8 9 |
# File 'lib/kiq/backer.rb', line 7 def amount @amount end |
#credit_card ⇒ Object
Allows access to name, credit_card, and amount
7 8 9 |
# File 'lib/kiq/backer.rb', line 7 def credit_card @credit_card end |
#name ⇒ Object
Allows access to name, credit_card, and amount
7 8 9 |
# File 'lib/kiq/backer.rb', line 7 def name @name end |
Class Method Details
.check_amount_dollar_sign(amount) ⇒ Boolean
Checks if the amount includes dollar signs
48 49 50 51 52 53 54 55 |
# File 'lib/kiq/backer.rb', line 48 def self.check_amount_dollar_sign(amount) if !amount.include?('$') return true else puts "ERROR: Backing amount must not contain the '$' character or any other alphanumeric characters. Numbers only." return false end end |
.check_card_length(credit_card) ⇒ Boolean
Checks if the credit card is under 19 numbers long
109 110 111 112 113 114 115 116 |
# File 'lib/kiq/backer.rb', line 109 def self.check_card_length(credit_card) if credit_card.length <= 19 return true else puts "ERROR: That card isn't less than or equal to 19 numbers!" return false end end |
.check_card_luhn_10(credit_card) ⇒ Boolean
Checks if the credit card passes the Luhn-10 algorithm
97 98 99 100 101 102 103 104 |
# File 'lib/kiq/backer.rb', line 97 def self.check_card_luhn_10(credit_card) if self.luhn_valid?(credit_card) return true else puts "ERROR: That card fails Luhn-10!" return false end end |
.check_card_numeric(credit_card) ⇒ Boolean
Checks if the credit card only contains numbers
121 122 123 124 125 126 127 128 |
# File 'lib/kiq/backer.rb', line 121 def self.check_card_numeric(credit_card) if credit_card.match(/^\d+$/) return true else puts "ERROR: That card isn't purely numeric!" return false end end |
.check_card_uniqueness(project, credit_card) ⇒ Boolean
Checks if the credit card is unique, no other Backer exists with that card
85 86 87 88 89 90 91 92 |
# File 'lib/kiq/backer.rb', line 85 def self.check_card_uniqueness(project, credit_card) if project.backers[credit_card].nil? return true else puts "ERROR: That card has already been added by another user!" return false end end |
.check_name_characters(name) ⇒ Boolean
Checks if the name has non-alphanumeric characters besides underscores and dashes
60 61 62 63 64 65 66 67 |
# File 'lib/kiq/backer.rb', line 60 def self.check_name_characters(name) if name.scan(/[^\w-]/).empty? return true else puts "ERROR: Backer name may only use alphanumeric characters, underscores, and dashes." return false end end |
.check_name_length(name) ⇒ Boolean
Checks if the string is between 4 and 20 characters
72 73 74 75 76 77 78 79 |
# File 'lib/kiq/backer.rb', line 72 def self.check_name_length(name) if name.length >= 4 && name.length <= 20 return true else puts "ERROR: Backer name must be between 4 and 20 characters." return false end end |
.luhn_valid?(credit_card) ⇒ Boolean
Computes Luhn-10 algorithm
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/kiq/backer.rb', line 133 def self.luhn_valid?(credit_card) number = credit_card.reverse sum = 0 count = 0 number.each_char do |char| n = char.to_i if count.odd? n *= 2 end if n >= 10 n = 1 + (n - 10) end sum += n count += 1 end (sum % 10) == 0 end |
.validate_backer_name(backer_name) ⇒ Boolean
Checks all validation methods for a Backer’s name
41 42 43 |
# File 'lib/kiq/backer.rb', line 41 def self.validate_backer_name(backer_name) return self.check_name_characters(backer_name) && self.check_name_length(backer_name) end |
.validate_card(project, credit_card) ⇒ Boolean
Checks all validation methods for a credit card
34 35 36 |
# File 'lib/kiq/backer.rb', line 34 def self.validate_card(project, credit_card) return self.check_card_uniqueness(project, credit_card) && self.check_card_luhn_10(credit_card) && self.check_card_length(credit_card) && self.check_card_numeric(credit_card) && self.check_name_length(name) end |
.validate_project_exists(project) ⇒ Boolean
Check if project exists
22 23 24 25 26 27 28 29 |
# File 'lib/kiq/backer.rb', line 22 def self.validate_project_exists(project) if !project.nil? return true else puts "ERROR: Project doesn't exist." return false end end |