Created
July 4, 2014 08:32
-
-
Save marjinal1st/dee76ae32e1e38e5afa9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'luhn' | |
require 'json' | |
require 'uri' | |
require 'net/http' | |
notes = ["Note: spaces and dashes aren't allowed. If you are going to write\n", | |
"4444 4444 4444 4448 or 4444-4444-4444-4448, write:\n", | |
"4444444444444448.\n", | |
"Note 2:If your Diners Club card begins with 5 and is 16 digits,\n", | |
'it will be treated as a Mastercard.' | |
] | |
inod = ['Visa (invalid number of digits)', | |
'Mastercard (invalid number of digits)', | |
'American Express (invalid number of digits)', | |
'Diners Club (invalid number of digits)', | |
'Discover (invalid number of digits)', | |
'JCB (invalid number of digits)' | |
] | |
puts notes.join | |
puts 'Credit card number:' | |
num = gets.chomp.to_s | |
case num | |
when /^4\d{12}(?:\d{3})?$/ then puts 'Visa' | |
when /^5[1-5]\d{14}$/ then puts 'Mastercard' | |
when /^3[47]\d{13}$/ then puts 'American Express' | |
when /^3(?:0[0-5]|[68]\d)\d{11}$/ then puts 'Diners Club' | |
when /^6(?:011|5\d{2})\d{12}$/ then puts 'Discover' | |
when /^(?:2131|1800|35\d{3})\d{11}$/ then puts 'JCB' | |
when /^4(\d{0,11}|\d{11}(?=\d{1,2})|\d{13,14}|\d{16,})$/ then puts inod[0] | |
when /^5[1-5](\d{0,13}|\d{15,})$/ then puts inod[1] | |
when /^3[47](\d{0,12}|\d{14,})$/ then puts inod[2] | |
when /^3(?:0[0-5]|[68])(\d{0,10}|\d{12,})$/ then puts inod[3] | |
when /^(6(?:011|5\d{2})(\d{0,11}|\d{13,})|65\d{0,2})$/ then puts inod[4] | |
when /^((?:2131|1800|35\d{3})(\d{0,10}|\d{12,})|35\d{0,2})$/ then puts inod[5] | |
else | |
puts 'Unknown' | |
end | |
puts 'Invalid credit card!' unless num.valid_luhn? | |
uri = URI.parse("http://www.binlist.net/json/#{num.split('').first(6).join}") | |
http = Net::HTTP.new(uri.host, uri.port) | |
request = Net::HTTP::Get.new(uri.request_uri) | |
response = http.request(request) | |
info = JSON.parse(response.body) | |
puts 'Bank is:' + info['bank'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment