Skip to content

Commit

Permalink
Add Faker::NHS - Support for the British National Health Service (fak…
Browse files Browse the repository at this point in the history
…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
maxehmookau authored and vbrazo committed Jul 11, 2018
1 parent 18bf47a commit 56d06d2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions doc/national_health_service.md
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
```
23 changes: 23 additions & 0 deletions lib/faker/nhs.rb
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
15 changes: 15 additions & 0 deletions test/test_faker_national_health_service.rb
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

0 comments on commit 56d06d2

Please sign in to comment.