Class: Kiq::CLI::Backer

Inherits:
Object
  • Object
show all
Defined in:
lib/kiq/backer.rb

Overview

Hosts logic for the backer object

Constant Summary collapse

@@instance_collector =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, credit_card, amount) ⇒ Backer

Initalizes a new Backer object

Parameters:

  • name (String)
  • amount (String)


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

#amountObject

Allows access to name, credit_card, and amount



7
8
9
# File 'lib/kiq/backer.rb', line 7

def amount
  @amount
end

#credit_cardObject

Allows access to name, credit_card, and amount



7
8
9
# File 'lib/kiq/backer.rb', line 7

def credit_card
  @credit_card
end

#nameObject

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

Parameters:

  • amount (String)

Returns:

  • (Boolean)

    warning if false



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

Parameters:

  • credit_card (String)

Returns:

  • (Boolean)

    warning if false



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

Parameters:

  • credit_card (String)

Returns:

  • (Boolean)

    warning if false



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

Parameters:

  • credit_card (String)

Returns:

  • (Boolean)

    warning if false



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

Parameters:

  • project (Project)
  • credit_card (String)

Returns:

  • (Boolean)

    warning if false



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

Parameters:

  • name (String)

Returns:

  • (Boolean)

    warning if false



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

Parameters:

  • name (String)

Returns:

  • (Boolean)

    warning if false



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

Parameters:

  • credit_card (String)

Returns:

  • (Boolean)


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

Parameters:

  • backer_name (String)

Returns:

  • (Boolean)

    warning if false



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

Parameters:

Returns:

  • (Boolean)

    warning if false



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

Parameters:

Returns:

  • (Boolean)

    warning if false



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