Skip to content

Commit

Permalink
now validates that the input is a lower that 1e6
Browse files Browse the repository at this point in the history
  • Loading branch information
aamyot authored and aamyot committed Jul 3, 2015
1 parent cb7d260 commit ebb04f3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/controller/primes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def call(env)
def decompose(input)
if !(Integer(input) rescue false)
PrimeResults::NotANumber.new(input)
elsif input.to_i > 1_000_000
PrimeResults::NumberIsTooBig.new(input)
else
PrimeResults::Valid.new(input, PrimeFactors.of(input.to_i))
end
Expand Down
14 changes: 12 additions & 2 deletions app/primes/results.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@ def to_json

class NotANumber
def initialize(input)
@number = input
@input = input
end

def to_json
{:number => @number, :error => "not a number"}.to_json
{:number => @input, :error => "not a number"}.to_json
end
end

class NumberIsTooBig
def initialize(input)
@number = input.to_i
end

def to_json
{:number => @number, :error => "too big number (>1e6)"}.to_json
end
end

Expand Down
12 changes: 12 additions & 0 deletions spec/challenges/prime_factors_challenge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,16 @@ def app
'}')
end

it "decomposes a positive integer into prime factors" do
get "http://localhost:9292/primeFactors/primeFactors?number=1000001"

expect(last_response.status).to eq(200)
expect(last_response.content_type).to eq("application/json")
expect(last_response.body).to eq(
'{' \
'"number":1000001,' \
'"error":"too big number (>1e6)"' \
'}')
end

end

0 comments on commit ebb04f3

Please sign in to comment.