From 56d06d2001907997295c7e7e6711c1c92b57c216 Mon Sep 17 00:00:00 2001 From: Max Woolf Date: Wed, 11 Jul 2018 21:19:59 +0100 Subject: [PATCH] Add Faker::NHS - Support for the British National Health Service (#801) * Adds support for the British National Health Service fake data. Every person in the United Kingdom has a unique code which identifies them to the National Health Service (NHS). The code is in XXX XXX XXXX format where the final digit is a check digit calculated from the previous ones. More information on NHS numbers can be found here: https://en.wikipedia.org/wiki/NHS_number This commit adds the Faker::NHS class along with the Faker::NHS.nhs_number method to generate a valid NHS number. * Add docs * Update changelog.md * Naming fixes --- CHANGELOG.md | 1 + README.md | 1 + doc/national_health_service.md | 9 +++++++++ lib/faker/nhs.rb | 23 ++++++++++++++++++++++ test/test_faker_national_health_service.rb | 15 ++++++++++++++ 5 files changed, 49 insertions(+) create mode 100644 doc/national_health_service.md create mode 100644 lib/faker/nhs.rb create mode 100644 test/test_faker_national_health_service.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 1169f41285..736af31ab1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ [Full Changelog](https://github.com/stympy/faker/compare/v1.8.7...v1.9.1) ### Feature Request +- [PR #801](https://github.com/stympy/faker/pull/801) Add Faker::NHS - Support for the British National Health Service [@substrakt-health](https://github.com/substrakt-health) - [PR #1308](https://github.com/stympy/faker/pull/1308) Add Faker::BojackHorseman [@saurabhudaniya200](https://github.com/saurabhudaniya200) - [PR #1292](https://github.com/stympy/faker/pull/1292) Add Faker::Bank - account_number and routing_number [@vbrazo](https://github.com/vbrazo) - [PR #1300](https://github.com/stympy/faker/pull/1300) Add Faker::GreekPhilosophers [@15ngburton](https://github.com/15ngburton) diff --git a/README.md b/README.md index 9653a030bb..ae2cc5f4e4 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,7 @@ Contents - [Faker::Nation](doc/nation.md) - [Faker::NatoPhoneticAlphabet](doc/nato_phonetic_alphabet.md) - [Faker::NewGirl](doc/new_girl.md) + - [Faker::NationalHealthService](doc/national_health_service.md) - [Faker::Number](doc/number.md) - [Faker::Omniauth](doc/omniauth.md) - [Faker::OnePiece](doc/one_piece.md) diff --git a/doc/national_health_service.md b/doc/national_health_service.md new file mode 100644 index 0000000000..edced2e8a9 --- /dev/null +++ b/doc/national_health_service.md @@ -0,0 +1,9 @@ +# Faker::NationalHealthService + +It might be available in the next version + +```ruby +Faker::NationalHealthService.british_number #=> "403 958 5577" + +Faker::NationalHealthService.check_digit(400_012_114) #=> 6 +``` diff --git a/lib/faker/nhs.rb b/lib/faker/nhs.rb new file mode 100644 index 0000000000..9f154f8a75 --- /dev/null +++ b/lib/faker/nhs.rb @@ -0,0 +1,23 @@ +module Faker + class NationalHealthService < Base + class << self + def british_number + base_number = rand(400_000_000...499_999_999) + "#{base_number}#{check_digit(base_number)}".to_s + .chars + .insert(3, ' ') + .insert(7, ' ') + .join('') + end + + def check_digit(number = 0) + sum = 0 + number.to_s.chars.each_with_index do |digit, idx| + position = idx + 1 + sum += (digit.to_i * (11 - position)) + end + 11 - (sum % 11) + end + end + end +end diff --git a/test/test_faker_national_health_service.rb b/test/test_faker_national_health_service.rb new file mode 100644 index 0000000000..5e5ca97c1f --- /dev/null +++ b/test/test_faker_national_health_service.rb @@ -0,0 +1,15 @@ +require File.expand_path(File.dirname(__FILE__) + '/test_helper.rb') + +class TestFakerNationalHealthService < Test::Unit::TestCase + def setup + @tester = Faker::NationalHealthService + end + + def test_nhs_british_number + assert_match(/\A\d{3}\s\d{3}\s\d{4}\z/, @tester.british_number) + end + + def test_nhs_check_digit + assert_equal 6, @tester.check_digit(400_012_114) + end +end