Skip to content

CLI #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,54 @@ 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)

PLATFORMS
ruby

DEPENDENCIES
aruba (~> 0.4.11)
cucumber (~> 1.1.4)
luhn!
minitest (~> 2.6.0)
rake
rspec (~> 2.8.0)
turn
7 changes: 7 additions & 0 deletions bin/ruby_luhn
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env ruby
require 'luhn'
require 'luhn/cli'

CLI.start


6 changes: 6 additions & 0 deletions bin/ruby_luhn~
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env ruby
require 'luhn'
require 'luhn/cli'
CLI.start


7 changes: 7 additions & 0 deletions bin/ruby_lunh~
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env ruby
# -*- mode: ruby -*-

require 'luhn/cli'
CLI.start


34 changes: 34 additions & 0 deletions features/calculate.feature
Original file line number Diff line number Diff line change
@@ -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"

24 changes: 24 additions & 0 deletions features/calculate.feature~
Original file line number Diff line number Diff line change
@@ -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"

2 changes: 2 additions & 0 deletions features/support/env.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'aruba/api'
require 'aruba/cucumber'
Empty file added features/support/env.rb~
Empty file.
29 changes: 29 additions & 0 deletions features/validate.feature
Original file line number Diff line number Diff line change
@@ -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"

29 changes: 29 additions & 0 deletions features/validate.feature~
Original file line number Diff line number Diff line change
@@ -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"

70 changes: 70 additions & 0 deletions lib/luhn/cli.rb
Original file line number Diff line number Diff line change
@@ -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
70 changes: 70 additions & 0 deletions lib/luhn/cli.rb~
Original file line number Diff line number Diff line change
@@ -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
7 changes: 6 additions & 1 deletion luhn.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
16 changes: 16 additions & 0 deletions luhn.gemspec~
Original file line number Diff line number Diff line change
@@ -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 = ['[email protected]']
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