Skip to content
Merged
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
8 changes: 3 additions & 5 deletions lib/for_education_code_generator.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# frozen_string_literal: true

require 'securerandom'

class ForEducationCodeGenerator
MAX_CODE = 1_000_000

cattr_accessor :random

self.random ||= Random.new

def self.generate
number = random.rand(MAX_CODE)
number = SecureRandom.random_number(MAX_CODE)
code = format('%06d', number)

code.match(/(\d\d)(\d\d)(\d\d)/) do |m|
Expand Down
8 changes: 2 additions & 6 deletions spec/lib/for_education_code_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@

RSpec.describe ForEducationCodeGenerator do
describe '.generate' do
it 'uses Random#rand to generate a random number up to the maximum' do
allow(described_class.random).to receive(:rand).with(ForEducationCodeGenerator::MAX_CODE).and_return(123)
it 'uses SecureRandom to generate a random number up to the maximum' do
allow(SecureRandom).to receive(:random_number).with(ForEducationCodeGenerator::MAX_CODE).and_return(123)
Comment thread
zetter-rpf marked this conversation as resolved.

expect(described_class.generate).to eq('00-01-23')
end

it 'generates a string containing 3 pairs of digits' do
expect(described_class.generate).to match(/\d\d-\d\d-\d\d/)
end

it 'generates a different code each time' do
expect(described_class.generate).not_to eq(described_class.generate)
end
end
end
8 changes: 0 additions & 8 deletions spec/support/for_education_code_generator.rb

This file was deleted.

Loading