credit_card_validator gem

I recently published a gem that provides credit card validation. It is basically a ruby port of the javascript credit card validator by Thomas Fuchs (madrobby).

Usage:

  CreditCardValidator::Validator.valid?('1111 2222 3333 4444')
 
# allow test numbers to be valid (for development) 
CreditCardValidator::Validator.options[:test_numbers_are_valid] = true
CreditCardValidator::Validator.valid?('1111 2222 3333 4444')
 
# limit the card types you allow
CreditCardValidator::Validator.options[:allowed_card_types] = [:visa, :mastercard]
CreditCardValidator::Validator.valid?('1111 2222 3333 4444')

Supported card types:

  :amex, :discover, :diners_club, :master_card, :visa

Whitespace is stripped from the number automatically.

The following things are tested:

1. does the luhn validation code add up? (see http://en.wikipedia.org/wiki/Luhn_algorithm)

2. does the number range and length seem right? (see http://en.wikipedia.org/wiki/Bank_card_number)

3. is it one of several well-known test numbers?

Note: this only validates that the number is of a valid format, it does not check if it is an actual credit card number. You will need to talk to your payment gateway to learn that.

You can also use the validator to learn about the type of the card:

  # gives the type back as a string (visa, master_card, etc)
CreditCardValidator::Validator.card_type(number)
 
CreditCardValidator::Validator.is_visa?(number)
CreditCardValidator::Validator.is_master_card?(number)
# etc. - works for all of the supported card types
 
CreditCardValidator::Validator.is_allowed_card_type?(number)

To Install:

gem install tobias-credit_card_validator --source http://gems.github.com

The source is available on github

script_finder gem

I recently published a ruby gem on github that simplifies running scripts from a script/ dir from anywhere in your project (rails or otherwise).

script_finder provides a script (called s@) in your path that searches in and up from the current dir for a folder (default: @script/) containing an executable file uniquely identified by the a prefix given as the first argument. It then calls that executable, passing the rest of the arguments to the called executable. If the given prefix is ambiguous, the script suggests unique prefixes.

Examples (in a rails app):

~/rails_app/app/views$ s c
--> calling '/Users/tobias/rails_app/script/console'
Loading development environment (Rails 2.1.0)
RowsLogger plugin enables mysql
>> exit
~/rails_app/app/views$ s r 'some ruby'
's r' was too ambiguous. Try:
's ru' for 'script/runner'
's re' for 'script/remote'
~/rails_app/app/views$ s ru 'some ruby'
--> calling '/Users/tobias/rails_app/script/runner some ruby'
...

The gem is not rails specific – out of the box it will work with any project that has a script/ directory. If you want to make your own version of the s@ script that looks for executables in a different dir (I would save this one as @c):

  #!/usr/bin/env ruby
 
require 'script_finder'
 
# looks for executables in a commands/ dir instead of script/.
ScriptFinder.find_and_execute(ARGV, 'commands')

Let me know if you have any problems/questions.