forked from faker-ruby/faker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Faker::NHS - Support for the British National Health Service (fak…
…er-ruby#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
- Loading branch information
1 parent
18bf47a
commit 56d06d2
Showing
5 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |