#!/usr/bin/env ruby require 'thor' require 'credy' module Credy class CLI < Thor desc "generate", "Generate a credit card number" method_option :details, aliases: '-d', desc: "Show card details" method_option :country, aliases: '-c', type: :string, default: 'all', desc: "Card's issuing country (au, fr, us, ca, ...)" method_option :type, aliases: '-t', type: :string, default: 'all', desc: "Card's type (visa, mastercard, americanexpress)" method_option :number, aliases: '-n', type: :numeric, default: 1, desc: "Number of cards" def generate details = options[:details] args = {} args[:country] = options[:country] unless options[:country] == 'all' args[:type] = options[:type] unless options[:type] == 'all' options[:number].times do number = CreditCard.generate args unless number puts 'No rule found for those criteria.' break end if details d = [number[:type], number[:country]].compact puts "#{number[:number]} (#{d.join(', ')})" else puts number[:number] end end end map 'g' => :generate desc 'infos NUMBER', 'Show information about a credit card number' def infos(number) infos = CreditCard.infos number if infos puts "Type: #{infos[:type]}" puts "Country: #{infos[:country]}" if infos[:country] if CreditCard.validate(number)[:valid] puts "Valid".green else puts "Not valid".red end else puts 'No information available for this number.' end end map 'i' => :infos desc 'validate NUMBER', 'Check if a credit card number is valid or not' def validate(number) validity = CreditCard.validate number if validity[:valid] puts 'This number is valid.'.green else puts 'This number is not valid.'.red end details = [] validity[:details].each do |criteria, value| value = value ? 'v'.green : 'x'.red details << "#{criteria}: #{value}" end puts "(#{details.join(', ')})" end end end Credy::CLI.start