From 7edf1974811c75ad5c67be9255c42b9bf81a3776 Mon Sep 17 00:00:00 2001 From: Indika Kodagoda Date: Sun, 3 Jun 2012 12:56:36 +0530 Subject: [PATCH] CLI completed --- Gemfile.lock | 33 +++++++++++++++++ bin/ruby_luhn | 7 ++++ bin/ruby_luhn~ | 6 ++++ bin/ruby_lunh~ | 7 ++++ features/calculate.feature | 34 ++++++++++++++++++ features/calculate.feature~ | 24 +++++++++++++ features/support/env.rb | 2 ++ features/support/env.rb~ | 0 features/validate.feature | 29 +++++++++++++++ features/validate.feature~ | 29 +++++++++++++++ lib/luhn/cli.rb | 70 +++++++++++++++++++++++++++++++++++++ lib/luhn/cli.rb~ | 70 +++++++++++++++++++++++++++++++++++++ luhn.gemspec | 7 +++- luhn.gemspec~ | 16 +++++++++ 14 files changed, 333 insertions(+), 1 deletion(-) create mode 100755 bin/ruby_luhn create mode 100755 bin/ruby_luhn~ create mode 100644 bin/ruby_lunh~ create mode 100644 features/calculate.feature create mode 100644 features/calculate.feature~ create mode 100644 features/support/env.rb create mode 100644 features/support/env.rb~ create mode 100644 features/validate.feature create mode 100644 features/validate.feature~ create mode 100644 lib/luhn/cli.rb create mode 100644 lib/luhn/cli.rb~ create mode 100644 luhn.gemspec~ diff --git a/Gemfile.lock b/Gemfile.lock index 32559e1..04e7eed 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,13 +2,43 @@ PATH remote: . specs: luhn (0.1.1) + thor (~> 0.14.6) GEM remote: http://rubygems.org/ specs: ansi (1.3.0) + aruba (0.4.11) + childprocess (>= 0.2.3) + cucumber (>= 1.1.1) + ffi (>= 1.0.11) + rspec (>= 2.7.0) + builder (3.0.0) + childprocess (0.3.2) + ffi (~> 1.0.6) + cucumber (1.1.9) + builder (>= 2.1.2) + diff-lcs (>= 1.1.2) + gherkin (~> 2.9.0) + json (>= 1.4.6) + term-ansicolor (>= 1.0.6) + diff-lcs (1.1.3) + ffi (1.0.11) + gherkin (2.9.3) + json (>= 1.4.6) + json (1.7.3) minitest (2.6.0) rake (0.9.2) + rspec (2.8.0) + rspec-core (~> 2.8.0) + rspec-expectations (~> 2.8.0) + rspec-mocks (~> 2.8.0) + rspec-core (2.8.0) + rspec-expectations (2.8.0) + diff-lcs (~> 1.1.2) + rspec-mocks (2.8.0) + term-ansicolor (1.0.7) + thor (0.14.6) turn (0.8.2) ansi (>= 1.2.2) @@ -16,7 +46,10 @@ PLATFORMS ruby DEPENDENCIES + aruba (~> 0.4.11) + cucumber (~> 1.1.4) luhn! minitest (~> 2.6.0) rake + rspec (~> 2.8.0) turn diff --git a/bin/ruby_luhn b/bin/ruby_luhn new file mode 100755 index 0000000..b1114ac --- /dev/null +++ b/bin/ruby_luhn @@ -0,0 +1,7 @@ +#!/usr/bin/env ruby +require 'luhn' +require 'luhn/cli' + +CLI.start + + diff --git a/bin/ruby_luhn~ b/bin/ruby_luhn~ new file mode 100755 index 0000000..96da377 --- /dev/null +++ b/bin/ruby_luhn~ @@ -0,0 +1,6 @@ +#!/usr/bin/env ruby +require 'luhn' +require 'luhn/cli' +CLI.start + + diff --git a/bin/ruby_lunh~ b/bin/ruby_lunh~ new file mode 100644 index 0000000..229f076 --- /dev/null +++ b/bin/ruby_lunh~ @@ -0,0 +1,7 @@ +#!/usr/bin/env ruby +# -*- mode: ruby -*- + +require 'luhn/cli' +CLI.start + + diff --git a/features/calculate.feature b/features/calculate.feature new file mode 100644 index 0000000..1ade4d5 --- /dev/null +++ b/features/calculate.feature @@ -0,0 +1,34 @@ +Feature: calculate luhn check digit + + In order to get the valid IMEI numbers + As a user + I want to calculate the luhn check digit + + Scenario: calculate check digit for one number + When I run `ruby_luhn calculate 35431604559642` + Then the output should contain "> 354316045596428" + + Scenario: calculate check digit for multiple numbers + When I run `ruby_luhn calculate` interactively + And I type "35431604559642" + And I type "35431604559639" + And I type "35431604559620" + And I type "exit" + Then the output should contain "> 354316045596428" + Then the output should contain "> 354316045596394" + Then the output should contain "> 354316045596204" + + Scenario: reject invalid input + When I run `ruby_luhn calculate 3543160d559642` + Then the output should contain "> 3543160d559642 is not a valid number" + + Scenario: reject invalid input for multiple numbers + When I run `ruby_luhn calculate` interactively + And I type "35431604559642" + And I type "3543160d59e639" + And I type "35431604559620" + And I type "exit" + Then the output should contain "> 354316045596428" + Then the output should contain "> 3543160d59e639 is not a valid number" + Then the output should contain "> 354316045596204" + diff --git a/features/calculate.feature~ b/features/calculate.feature~ new file mode 100644 index 0000000..274b348 --- /dev/null +++ b/features/calculate.feature~ @@ -0,0 +1,24 @@ +Feature: calculate luhn check digit + + In order to get the valid IMEI numbers + As a user + I want to calculate the luhn check digit + + Scenario: calculate check digit for one number + When I run `ruby_luhn calculate 35431604559642` + Then the output should contain "> 354316045596428" + + Scenario: calculate check digit for multiple numbers + When I run `ruby_luhn calculate` interactively + And I type "35431604559642" + And I type "35431604559639" + And I type "35431604559620" + And I type "exit" + Then the output should contain "> 354316045596428" + Then the output should contain "> 354316045596394" + Then the output should contain "> 354316045596204" + + Scenario: reject invalid input + When I run `ruby_luhn calculate 3543160d559642` + Then the output should contain "> 3543160d559642 is not a valid number" + diff --git a/features/support/env.rb b/features/support/env.rb new file mode 100644 index 0000000..3f48099 --- /dev/null +++ b/features/support/env.rb @@ -0,0 +1,2 @@ +require 'aruba/api' +require 'aruba/cucumber' diff --git a/features/support/env.rb~ b/features/support/env.rb~ new file mode 100644 index 0000000..e69de29 diff --git a/features/validate.feature b/features/validate.feature new file mode 100644 index 0000000..c7a7aea --- /dev/null +++ b/features/validate.feature @@ -0,0 +1,29 @@ +Feature: validate luhn check digit + + In order to verify IMEI numbers + As a user + I want to verify the luhn check digit + + Scenario: validate check digit for one number + When I run `ruby_luhn validate 354316045596428` + Then the output should contain "> 354316045596428 correct" + + Scenario: reject invalid input + When I run `ruby_luhn validate 354316035596428` + Then the output should contain "> 354316035596428 incorrect" + + + Scenario: reject invalid input + When I run `ruby_luhn validate 3543160d559642` + Then the output should contain "> 3543160d559642 invalid number" + + Scenario: validate check digit for multiple numbers + When I run `ruby_luhn validate` interactively + And I type "354316045596428" + And I type "354316045596393" + And I type "354316045596203" + And I type "exit" + Then the output should contain "> 354316045596428 correct" + Then the output should contain "> 354316045596393 incorrect" + Then the output should contain "> 354316045596203 incorrect" + diff --git a/features/validate.feature~ b/features/validate.feature~ new file mode 100644 index 0000000..9b53624 --- /dev/null +++ b/features/validate.feature~ @@ -0,0 +1,29 @@ +Feature: validate luhn check digit + + In order to verify IMEI numbers + As a user + I want to verify the luhn check digit + + Scenario: validate check digit for one number + When I run `ruby_luhn validate 354316045596428` + Then the output should contain "> 354316045596428 correct" + + Scenario: reject invalid input + When I run `ruby_luhn validate 354316035596428` + Then the output should contain "> 354316035596428 incorrect" + + + Scenario: reject invalid input + When I run `ruby_luhn validate 3543160d559642` + Then the output should contain "> 3543160d559642 invalid number" + + Scenario: validate check digit for multiple numbers + When I run `ruby_luhn validate` interactively + And I type "354316045596428" + And I type "354316045596393" + And I type "354316045596203" + And I type "exit" + Then the output should contain "> 354316045596428 correct" + Then the output should contain "> 3543160455963943 incorrect" + Then the output should contain "> 3543160455962043 incorrect" + diff --git a/lib/luhn/cli.rb b/lib/luhn/cli.rb new file mode 100644 index 0000000..70f80e9 --- /dev/null +++ b/lib/luhn/cli.rb @@ -0,0 +1,70 @@ +require "thor" + +class CLI < Thor + desc "calculate NUMBER", "calculate the luhn check digit of the number. In the interactive mode type exit to terminate" + def calculate(number=nil) + if number.nil? + continue = true + while continue + number = ask("number? ") + if number.eql? "exit" + continue = false + end + print_number number + end + else + print_number number + end + end + + desc "validate NUMBER", "validate the luhn check digit of the number. In the interactive mode type exit to terminate" + def validate(number=nil) + get_input_number(number) do |input_number| + print_validity input_number + end + end + + private + def get_input_number(number) + if number.nil? + continue = true + while continue + number = ask("number? ") + if number.eql? "exit" + continue = false + end + yield number + end + else + yield number + end + end + + def print_validity(number) + if is_number?(number) + if Luhn.valid?(number) + say "> " + number + " correct" + else + say "> " + number + " incorrect" + end + else + say "> " + number + " invalid number" + end + end + + def print_number(number) + if is_number? number + say "> " + number + Luhn.control_digit(number).to_s + else + say "> " + number + " is not a valid number" + end + end + + def is_number?(number) + if number.match(/\D/) + false + else + true + end + end +end diff --git a/lib/luhn/cli.rb~ b/lib/luhn/cli.rb~ new file mode 100644 index 0000000..d812be4 --- /dev/null +++ b/lib/luhn/cli.rb~ @@ -0,0 +1,70 @@ +require "thor" + +class CLI < Thor + desc "calculate NUMBER", "calculate the luhn check digit of the number. In the interactive mode type exit to terminate" + def calculate(number=nil) + if number.nil? + continue = true + while continue + number = ask("number? ") + if number.eql? "exit" + continue = false + end + print_number number + end + else + print_number number + end + end + + desc "validate NUMBER", "validate the luhn check digit of the number. In the interactive mode type exit to terminate" + def validate(number) + get_input_number(number) do |input_number| + print_validity input_number + end + end + + private + def get_input_number(number) + if number.nil? + continue = true + while continue + number = ask("number? ") + if number.eql? "exit" + continue = false + end + yield number + end + else + yield number + end + end + + def print_validity(number) + if is_number?(number) + if Luhn.valid?(number) + say "> " + number + " correct" + else + say "> " + number + " incorrect" + end + else + say "> " + number + " invalid number" + end + end + + def print_number(number) + if is_number? number + say "> " + number + Luhn.control_digit(number).to_s + else + say "> " + number + " is not a valid number" + end + end + + def is_number?(number) + if number.match(/\D/) + false + else + true + end + end +end diff --git a/luhn.gemspec b/luhn.gemspec index 1b10181..3e66409 100644 --- a/luhn.gemspec +++ b/luhn.gemspec @@ -12,5 +12,10 @@ Gem::Specification.new do |s| s.platform = Gem::Platform::RUBY s.files = Dir.glob("{lib,spec}/**/*") + %w[LICENSE README.rdoc] + s.add_dependency 'thor', '~> 0.14.6' + s.add_development_dependency 'minitest', '~> 2.6.0' -end \ No newline at end of file + s.add_development_dependency 'cucumber', '~> 1.1.4' + s.add_development_dependency 'aruba', '~> 0.4.11' + s.add_development_dependency 'rspec', '~> 2.8.0' +end diff --git a/luhn.gemspec~ b/luhn.gemspec~ new file mode 100644 index 0000000..1b10181 --- /dev/null +++ b/luhn.gemspec~ @@ -0,0 +1,16 @@ +# encoding: UTF-8 +require File.expand_path('../lib/luhn/version', __FILE__) + +Gem::Specification.new do |s| + s.name = 'luhn' + s.homepage = 'http://github.com/joeljunstrom/ruby_luhn' + s.summary = 'A small implementation of the Luhn algorithm.' + s.require_path = 'lib' + s.authors = ['Joel Junström'] + s.email = ['joel.junstrom@gmail.com'] + s.version = Luhn::Version + s.platform = Gem::Platform::RUBY + s.files = Dir.glob("{lib,spec}/**/*") + %w[LICENSE README.rdoc] + + s.add_development_dependency 'minitest', '~> 2.6.0' +end \ No newline at end of file