Class: Faker::NationalHealthService

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/default/nhs.rb

Constant Summary

Constants inherited from Base

Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, translate, unique, with_locale

Class Method Details

.british_numberString

Produces a random British NHS number.

Examples:

Faker::NationalHealthService.british_number #=> "403 958 5577"

Returns:

  • (String)

Available since:

  • 1.9.2


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/faker/default/nhs.rb', line 15

def british_number
  base_number = rand(400_000_001...499_999_999)
  # If the check digit is equivalent to 10, the number is invalid.
  # See https://en.wikipedia.org/wiki/NHS_number
  base_number -= 1 if check_digit(number: base_number) == 10
  "#{base_number}#{check_digit(number: base_number)}".to_s
                                                     .chars
                                                     .insert(3, ' ')
                                                     .insert(7, ' ')
                                                     .join('')
end

.check_digit(legacy_number = NOT_GIVEN, number: 0) ⇒ Integer

Produces a random British NHS number's check digit.

Examples:

Faker::NationalHealthService.check_digit(number: 400_012_114) #=> 6

Parameters:

  • number (Integer) (defaults to: 0)

    Specifies the NHS number the check digit belongs to.

Returns:

  • (Integer)

Available since:

  • 1.9.2


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/faker/default/nhs.rb', line 37

def check_digit(legacy_number = NOT_GIVEN, number: 0)
  warn_for_deprecated_arguments do |keywords|
    keywords << :number if legacy_number != NOT_GIVEN
  end

  sum = 0
  number.to_s.chars.each_with_index do |digit, idx|
    position = idx + 1
    sum += (digit.to_i * (11 - position))
  end
  result = 11 - (sum % 11)

  # A value of 11 is considered the same as 0
  # See https://en.wikipedia.org/wiki/NHS_number
  return 0 if result == 11

  result
end