Class: CreditCardProcessor::CCProcessor

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

Instance Method Summary collapse

Constructor Details

#initialize(argv = ARGV) ⇒ CCProcessor

Returns a new instance of CCProcessor.



10
11
12
13
14
15
16
# File 'lib/credit_card_processor.rb', line 10

def initialize(argv = ARGV)
  @parser = Trollop::Parser.new
  Trollop.with_standard_exception_handling(@parser) do
    define_options(argv)
    @options = @parser.parse(argv.clone)
  end
end

Instance Method Details

#add_account(name, cc_number, credit_available) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/credit_card_processor.rb', line 40

def (name, cc_number, credit_available)
  if cc_valid?(cc_number)
    DB[:accounts].insert(name: name, cc_number: cc_number, credit_available: credit_available, balance: 0)
  else
    DB[:accounts].insert(name: name, cc_number: 'error', credit_available: credit_available, balance: 0)
  end
end

#cc_valid?(cc_number) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/credit_card_processor.rb', line 36

def cc_valid?(cc_number)
  Luhn.valid? cc_number
end

#charge(name, charge) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/credit_card_processor.rb', line 52

def charge(name, charge)
  credit_available = DB[:accounts].select(:credit_available).where(name: name).first
  if charge.to_i <= credit_available[:credit_available].to_i && name_exists?(name)
    DB[:accounts].where(name: name).update(balance: Sequel.expr(:balance) + charge.to_i)
    DB[:accounts].where(name: name).update(credit_available: Sequel.expr(:credit_available) - charge.to_i)
  end
end

#create_tableObject



26
27
28
29
30
31
32
33
34
# File 'lib/credit_card_processor.rb', line 26

def create_table
  DB.create_table? :accounts do
    primary_key :id
    String :name
    String :cc_number
    Integer :credit_available
    Integer :balance
  end
end

#credit(name, credit) ⇒ Object



60
61
62
63
64
65
# File 'lib/credit_card_processor.rb', line 60

def credit(name, credit)
  if name_exists?(name)
    DB[:accounts].where(name: name).update(balance: Sequel.expr(:balance) - credit.to_i)
    DB[:accounts].where(name: name).update(credit_available: Sequel.expr(:credit_available) + credit.to_i)
  end
end

#define_options(_argv) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/credit_card_processor.rb', line 18

def define_options(_argv)
  @parser.banner <<-EOS.gsub(/^ {8}/, '')
    Processes credit card info from an input file
    Options:
  EOS
  @parser.opt :file, 'Full path to input file', type: :string, short: 'f', required: true
end

#mainObject



94
95
96
97
98
99
# File 'lib/credit_card_processor.rb', line 94

def main
  input_file = @options[:file]
  create_table
  parse(input_file)
  output
end

#name_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/credit_card_processor.rb', line 48

def name_exists?(name)
  DB[:accounts].where(name: name)
end

#outputObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/credit_card_processor.rb', line 83

def output
  output_hash = DB[:accounts].select(:name, :cc_number, :balance).all
  output_hash.sort_by { |output_hash| output_hash[:name] }.each do |output_hash|
    if output_hash[:cc_number] == 'error'
      puts "#{output_hash[:name]}: error"
    else
      puts "#{output_hash[:name]}: $#{output_hash[:balance]}"
    end
  end
end

#parse(input_file) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/credit_card_processor.rb', line 67

def parse(input_file)
  File.open(input_file).each do |line|
    line = line.split(' ')
    function = line[0]
    if function == 'Add'
      (line[1], line[2], line[3].split('$').last.to_i)
    elsif function == 'Charge'
      charge(line[1], line[2].split('$').last.to_i)
    elsif function == 'Credit'
      credit(line[1], line[2].split('$').last.to_i)
    else
      puts 'Function is not valid'
    end
  end
end