faker 1.8.7 → 1.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (177) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +146 -6
  3. data/README.md +44 -4
  4. data/lib/extensions/array.rb +10 -10
  5. data/lib/extensions/symbol.rb +0 -1
  6. data/lib/faker.rb +42 -35
  7. data/lib/faker/address.rb +39 -13
  8. data/lib/faker/app.rb +1 -3
  9. data/lib/faker/appliance.rb +13 -0
  10. data/lib/faker/avatar.rb +2 -2
  11. data/lib/faker/bank.rb +72 -15
  12. data/lib/faker/beer.rb +0 -1
  13. data/lib/faker/bitcoin.rb +4 -5
  14. data/lib/faker/bojack_horseman.rb +17 -0
  15. data/lib/faker/breaking_bad.rb +0 -3
  16. data/lib/faker/business.rb +1 -2
  17. data/lib/faker/cannabis.rb +42 -0
  18. data/lib/faker/chuck_norris.rb +3 -2
  19. data/lib/faker/code.rb +59 -20
  20. data/lib/faker/coffee.rb +4 -0
  21. data/lib/faker/color.rb +2 -2
  22. data/lib/faker/commerce.rb +3 -4
  23. data/lib/faker/community.rb +13 -0
  24. data/lib/faker/company.rb +80 -24
  25. data/lib/faker/currency.rb +17 -0
  26. data/lib/faker/date.rb +13 -27
  27. data/lib/faker/demographic.rb +2 -2
  28. data/lib/faker/device.rb +29 -0
  29. data/lib/faker/dota.rb +25 -0
  30. data/lib/faker/dr_who.rb +7 -8
  31. data/lib/faker/dumb_and_dumber.rb +1 -1
  32. data/lib/faker/dune.rb +4 -4
  33. data/lib/faker/elder_scrolls.rb +16 -0
  34. data/lib/faker/electrical_components.rb +19 -0
  35. data/lib/faker/ethereum.rb +12 -0
  36. data/lib/faker/famous_last_words.rb +11 -0
  37. data/lib/faker/file.rb +2 -5
  38. data/lib/faker/fillmurray.rb +3 -4
  39. data/lib/faker/finance.rb +3 -4
  40. data/lib/faker/food.rb +21 -1
  41. data/lib/faker/football.rb +21 -0
  42. data/lib/faker/gender.rb +13 -0
  43. data/lib/faker/greek_philosophers.rb +13 -0
  44. data/lib/faker/hacker.rb +9 -11
  45. data/lib/faker/harry_potter.rb +4 -0
  46. data/lib/faker/hipster.rb +12 -4
  47. data/lib/faker/id_number.rb +24 -9
  48. data/lib/faker/internet.rb +69 -57
  49. data/lib/faker/invoice.rb +94 -0
  50. data/lib/faker/job.rb +10 -3
  51. data/lib/faker/lebowski.rb +2 -2
  52. data/lib/faker/lord_of_the_rings.rb +5 -1
  53. data/lib/faker/lorem.rb +32 -8
  54. data/lib/faker/lorem_flickr.rb +43 -0
  55. data/lib/faker/lorem_pixel.rb +20 -5
  56. data/lib/faker/lovecraft.rb +12 -5
  57. data/lib/faker/markdown.rb +12 -4
  58. data/lib/faker/measurement.rb +35 -80
  59. data/lib/faker/michael_scott.rb +9 -0
  60. data/lib/faker/military.rb +25 -0
  61. data/lib/faker/music.rb +13 -1
  62. data/lib/faker/myst.rb +25 -0
  63. data/lib/faker/name.rb +22 -8
  64. data/lib/faker/nation.rb +24 -0
  65. data/lib/faker/nato_phonetic_alphabet.rb +9 -0
  66. data/lib/faker/new_girl.rb +13 -0
  67. data/lib/faker/number.rb +12 -12
  68. data/lib/faker/omniauth.rb +110 -122
  69. data/lib/faker/parks_and_rec.rb +13 -0
  70. data/lib/faker/phone_number.rb +11 -19
  71. data/lib/faker/placeholdit.rb +11 -2
  72. data/lib/faker/princess_bride.rb +13 -0
  73. data/lib/faker/rick_and_morty.rb +1 -1
  74. data/lib/faker/science.rb +4 -2
  75. data/lib/faker/seinfeld.rb +1 -1
  76. data/lib/faker/shakespeare.rb +0 -3
  77. data/lib/faker/silicon_valley.rb +4 -0
  78. data/lib/faker/simpsons.rb +3 -8
  79. data/lib/faker/singular_siegler.rb +9 -0
  80. data/lib/faker/slack_emoji.rb +0 -2
  81. data/lib/faker/source.rb +18 -0
  82. data/lib/faker/space.rb +4 -0
  83. data/lib/faker/star_trek.rb +2 -2
  84. data/lib/faker/star_wars.rb +5 -6
  85. data/lib/faker/stranger_thing.rb +13 -0
  86. data/lib/faker/string.rb +45 -0
  87. data/lib/faker/stripe.rb +63 -0
  88. data/lib/faker/sword_art_online.rb +21 -0
  89. data/lib/faker/team.rb +4 -1
  90. data/lib/faker/the_it_crowd.rb +21 -0
  91. data/lib/faker/time.rb +10 -10
  92. data/lib/faker/twin_peaks.rb +2 -2
  93. data/lib/faker/twitter.rb +1 -1
  94. data/lib/faker/types.rb +36 -40
  95. data/lib/faker/university.rb +3 -3
  96. data/lib/faker/vehicle.rb +72 -10
  97. data/lib/faker/verb.rb +25 -0
  98. data/lib/faker/version.rb +1 -1
  99. data/lib/faker/world_cup.rb +25 -0
  100. data/lib/helpers/char.rb +19 -20
  101. data/lib/helpers/unique_generator.rb +7 -1
  102. data/lib/locales/ca-CAT.yml +1 -1
  103. data/lib/locales/da-DK.yml +1 -0
  104. data/lib/locales/de-AT.yml +3 -2
  105. data/lib/locales/de.yml +6 -5
  106. data/lib/locales/ee.yml +59 -0
  107. data/lib/locales/en-CA.yml +23 -1
  108. data/lib/locales/en-GB.yml +1 -0
  109. data/lib/locales/en-NZ.yml +4 -3
  110. data/lib/locales/en-US.yml +2 -0
  111. data/lib/locales/en.yml +2 -2
  112. data/lib/locales/en/appliance.yml +5 -0
  113. data/lib/locales/en/bank.yml +274 -59
  114. data/lib/locales/en/bojack_horseman.yml +6 -0
  115. data/lib/locales/en/cannabis.yml +12 -0
  116. data/lib/locales/en/community.yml +33 -0
  117. data/lib/locales/en/currency.yml +6 -0
  118. data/lib/locales/en/dessert.yml +3 -3
  119. data/lib/locales/en/device.yml +7 -0
  120. data/lib/locales/en/dota.yml +102 -0
  121. data/lib/locales/en/dr_who.yml +5 -1
  122. data/lib/locales/en/dumb_and_dumber.yml +1 -1
  123. data/lib/locales/en/elder_scrolls.yml +8 -5
  124. data/lib/locales/en/electrical_components.yml +6 -0
  125. data/lib/locales/en/famous_last_words.yml +4 -0
  126. data/lib/locales/en/food.yml +6 -3
  127. data/lib/locales/en/football.yml +7 -0
  128. data/lib/locales/en/gender.yml +5 -0
  129. data/lib/locales/en/greek_philosophers.yml +26 -0
  130. data/lib/locales/en/harry_potter.yml +2 -1
  131. data/lib/locales/en/invoice.yml +6 -0
  132. data/lib/locales/en/job.yml +2 -2
  133. data/lib/locales/en/lord_of_the_rings.yml +1 -0
  134. data/lib/locales/en/lorem.yml +4 -0
  135. data/lib/locales/en/michael_scott.yml +45 -0
  136. data/lib/locales/en/military.yml +8 -0
  137. data/lib/locales/en/music.yml +29 -1
  138. data/lib/locales/en/myst.yml +47 -0
  139. data/lib/locales/en/name.yml +13 -12
  140. data/lib/locales/en/nation.yml +8 -0
  141. data/lib/locales/en/nato_phonetic_alphabet.yml +4 -0
  142. data/lib/locales/en/new_girl.yml +40 -0
  143. data/lib/locales/en/overwatch.yml +3 -2
  144. data/lib/locales/en/parks_and_rec.yml +7 -0
  145. data/lib/locales/en/princess_bride.yml +5 -0
  146. data/lib/locales/en/programming_language.yml +2 -1
  147. data/lib/locales/en/science.yml +1 -0
  148. data/lib/locales/en/silicon_valley.yml +1 -0
  149. data/lib/locales/en/singular_siegler.yml +37 -0
  150. data/lib/locales/en/source.yml +16 -0
  151. data/lib/locales/en/space.yml +2 -1
  152. data/lib/locales/en/star_wars.yml +10 -0
  153. data/lib/locales/en/stranger_thing.yml +36 -0
  154. data/lib/locales/en/stripe.yml +42 -0
  155. data/lib/locales/en/sword_art_online.yml +334 -0
  156. data/lib/locales/en/team.yml +1 -1
  157. data/lib/locales/en/the_it_crowd.yml +155 -0
  158. data/lib/locales/en/vehicle.yml +24 -0
  159. data/lib/locales/en/verbs.yml +8 -0
  160. data/lib/locales/en/world_cup.yml +271 -0
  161. data/lib/locales/es.yml +6 -1
  162. data/lib/locales/fi-FI.yml +9 -4
  163. data/lib/locales/fr-CA.yml +90 -0
  164. data/lib/locales/fr-CH.yml +73 -0
  165. data/lib/locales/fr.yml +4 -2
  166. data/lib/locales/it.yml +1 -1
  167. data/lib/locales/ja.yml +11 -0
  168. data/lib/locales/lv.yml +53 -0
  169. data/lib/locales/nl.yml +1 -1
  170. data/lib/locales/no.yml +7 -0
  171. data/lib/locales/pl.yml +1 -0
  172. data/lib/locales/pt-BR.yml +21 -13
  173. data/lib/locales/pt.yml +1 -0
  174. data/lib/locales/sv.yml +4 -4
  175. data/lib/locales/tr.yml +8 -0
  176. data/lib/locales/uk.yml +9 -0
  177. metadata +167 -3
@@ -28,7 +28,10 @@ module Faker
28
28
  end
29
29
 
30
30
  def zip_code(state_abbreviation = '')
31
- return bothify(fetch('address.postcode')) if state_abbreviation === ''
31
+ if state_abbreviation.empty?
32
+ letterified_string = letterify(fetch('address.postcode'))
33
+ return numerify(letterified_string, leading_zero: true)
34
+ end
32
35
 
33
36
  # provide a zip code that is valid for the state provided
34
37
  # see http://www.fincen.gov/forms/files/us_state_territory_zip_codes.pdf
@@ -39,24 +42,47 @@ module Faker
39
42
  fetch('address.time_zone')
40
43
  end
41
44
 
42
- alias_method :zip, :zip_code
43
- alias_method :postcode, :zip_code
45
+ alias zip zip_code
46
+ alias postcode zip_code
44
47
 
45
- def street_suffix; fetch('address.street_suffix'); end
46
- def city_suffix; fetch('address.city_suffix'); end
47
- def city_prefix; fetch('address.city_prefix'); end
48
- def state_abbr; fetch('address.state_abbr'); end
49
- def state; fetch('address.state'); end
50
- def country; fetch('address.country'); end
51
- def country_code; fetch('address.country_code'); end
52
- def country_code_long; fetch('address.country_code_long'); end
48
+ def street_suffix
49
+ fetch('address.street_suffix')
50
+ end
51
+
52
+ def city_suffix
53
+ fetch('address.city_suffix')
54
+ end
55
+
56
+ def city_prefix
57
+ fetch('address.city_prefix')
58
+ end
59
+
60
+ def state_abbr
61
+ fetch('address.state_abbr')
62
+ end
63
+
64
+ def state
65
+ fetch('address.state')
66
+ end
67
+
68
+ def country
69
+ fetch('address.country')
70
+ end
71
+
72
+ def country_code
73
+ fetch('address.country_code')
74
+ end
75
+
76
+ def country_code_long
77
+ fetch('address.country_code_long')
78
+ end
53
79
 
54
80
  def latitude
55
- ((rand * 180) - 90).to_s
81
+ ((rand * 180) - 90).to_f
56
82
  end
57
83
 
58
84
  def longitude
59
- ((rand * 360) - 180).to_s
85
+ ((rand * 360) - 180).to_f
60
86
  end
61
87
 
62
88
  def full_address
@@ -1,7 +1,6 @@
1
1
  module Faker
2
2
  class App < Base
3
3
  class << self
4
-
5
4
  def name
6
5
  fetch('app.name')
7
6
  end
@@ -15,9 +14,8 @@ module Faker
15
14
  end
16
15
 
17
16
  def semantic_version(major: 0..9, minor: 0..9, patch: 1..9)
18
- [ major, minor, patch ].map {|chunk| sample(Array(chunk)) }.join('.')
17
+ [major, minor, patch].map { |chunk| sample(Array(chunk)) }.join('.')
19
18
  end
20
-
21
19
  end
22
20
  end
23
21
  end
@@ -0,0 +1,13 @@
1
+ module Faker
2
+ class Appliance < Base
3
+ class << self
4
+ def brand
5
+ fetch('appliance.brand')
6
+ end
7
+
8
+ def equipment
9
+ fetch('appliance.equipment')
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,10 +1,10 @@
1
1
  module Faker
2
2
  class Avatar < Base
3
3
  class << self
4
- SUPPORTED_FORMATS = %w(png jpg bmp)
4
+ SUPPORTED_FORMATS = %w[png jpg bmp].freeze
5
5
 
6
6
  def image(slug = nil, size = '300x300', format = 'png', set = 'set1', bgset = nil)
7
- raise ArgumentError, "Size should be specified in format 300x300" unless size.match(/^[0-9]+x[0-9]+$/)
7
+ raise ArgumentError, 'Size should be specified in format 300x300' unless size =~ /^[0-9]+x[0-9]+$/
8
8
  raise ArgumentError, "Supported formats are #{SUPPORTED_FORMATS.join(', ')}" unless SUPPORTED_FORMATS.include?(format)
9
9
  slug ||= Faker::Lorem.words.join
10
10
  bgset_query = "&bgset=#{bgset}" if bgset
@@ -3,34 +3,91 @@ module Faker
3
3
  flexible :bank
4
4
 
5
5
  class << self
6
+ def account_number(digits = 11)
7
+ rand.to_s[2..digits]
8
+ end
9
+
10
+ def iban(country_code = 'GB')
11
+ # Each country has it's own format for bank accounts
12
+ # Many of them use letters in certain parts of the account
13
+ # Using regex patterns we can create virtually any type of bank account
14
+ begin
15
+ pattern = fetch("bank.iban_details.#{country_code.downcase}.bban_pattern")
16
+ rescue I18n::MissingTranslationData
17
+ raise ArgumentError, "Could not find iban details for #{country_code}"
18
+ end
19
+
20
+ # Use Faker::Base.regexify for creating a sample from bank account format regex
21
+ account = Base.regexify(/#{pattern}/)
22
+
23
+ # Add country code and checksum to the generated account to form valid IBAN
24
+ country_code.upcase + iban_checksum(country_code, account) + account
25
+ end
26
+
6
27
  def name
7
28
  fetch('bank.name')
8
29
  end
9
30
 
31
+ def routing_number
32
+ valid_routing_number
33
+ end
34
+
35
+ def routing_number_with_format
36
+ compile_fraction(valid_routing_number)
37
+ end
38
+
10
39
  def swift_bic
11
40
  fetch('bank.swift_bic')
12
41
  end
13
42
 
14
- def iban(country_code = "GB")
15
- [
16
- country_code.upcase,
17
- Array.new(2) { rand(10) },
18
- iban_range(country_code, :letter_code) { (65 + rand(26)).chr },
19
- iban_range(country_code, :digits) { rand(10) }
20
- ].join
43
+ private
44
+
45
+ def checksum(num_string)
46
+ num_array = num_string.split('').map(&:to_i)
47
+ digit = (7 * (num_array[0] + num_array[3] + num_array[6]) + 3 * (num_array[1] + num_array[4] + num_array[7]) + 9 * (num_array[2] + num_array[5])) % 10
48
+ digit == num_array[8]
21
49
  end
22
50
 
23
- private
51
+ def compile_routing_number
52
+ digit_one_two = %w[00 01 02 03 04 05 06 07 08 09 10 11 12]
53
+ ((21..32).to_a + (61..72).to_a + [80]).each { |x| digit_one_two << x.to_s }
54
+ routing_num = digit_one_two.sample + rand_numstring + rand_numstring + rand_numstring + rand_numstring + rand_numstring + rand_numstring + rand_numstring
55
+ routing_num
56
+ end
57
+
58
+ # Calculates the mandatory checksum in 3rd and 4th characters in IBAN format
59
+ # source: https://en.wikipedia.org/wiki/International_Bank_Account_Number#Validating_the_IBAN
60
+ def iban_checksum(country_code, account)
61
+ # Converts letters to numbers according the iban rules, A=10..Z=35
62
+ account_to_number = "#{account}#{country_code}00".upcase.chars.map do |d|
63
+ d =~ /[A-Z]/ ? (d.ord - 55).to_s : d
64
+ end.join.to_i
65
+
66
+ # This is answer to (iban_to_num + checksum) % 97 == 1
67
+ checksum = (1 - account_to_number) % 97
68
+
69
+ # Use leftpad to make the size always to 2
70
+ checksum.to_s.rjust(2, '0')
71
+ end
72
+
73
+ def valid_routing_number
74
+ for _ in 0..50
75
+ micr = compile_routing_number
76
+
77
+ break if checksum(micr)
78
+ end
79
+ micr
80
+ end
24
81
 
25
- def iban_range(country_code, number_type)
26
- array_length = iban_length(country_code, number_type)
27
- Array.new(array_length) { yield }
82
+ def compile_fraction(routing_num)
83
+ prefix = (1..50).to_a.map(&:to_s).sample
84
+ numerator = routing_num.split('')[5..8].join.to_i.to_s
85
+ denominator = routing_num.split('')[0..4].join.to_i.to_s
86
+ prefix + '-' + numerator + '/' + denominator
28
87
  end
29
88
 
30
- def iban_length(country_code, number_type)
31
- fetch("bank.iban_details.#{country_code.downcase}.#{number_type}").to_i
32
- rescue I18n::MissingTranslationData
33
- raise ArgumentError, "Could not find iban details for #{country_code}"
89
+ def rand_numstring
90
+ (0..9).to_a.map(&:to_s).sample
34
91
  end
35
92
  end
36
93
  end
@@ -1,4 +1,3 @@
1
- # encoding: utf-8
2
1
  module Faker
3
2
  class Beer < Base
4
3
  flexible :beer
@@ -4,11 +4,10 @@ require 'securerandom'
4
4
  module Faker
5
5
  class Bitcoin < Base
6
6
  class << self
7
-
8
7
  PROTOCOL_VERSIONS = {
9
8
  main: 0,
10
9
  testnet: 111
11
- }
10
+ }.freeze
12
11
 
13
12
  def address
14
13
  address_for(:main)
@@ -25,16 +24,16 @@ module Faker
25
24
  base = alphabet.size
26
25
 
27
26
  lv = 0
28
- str.split('').reverse.each_with_index { |v,i| lv += v.unpack('C')[0] * 256**i }
27
+ str.split('').reverse.each_with_index { |v, i| lv += v.unpack('C')[0] * 256**i }
29
28
 
30
29
  ret = ''
31
- while lv > 0 do
30
+ while lv > 0
32
31
  lv, mod = lv.divmod(base)
33
32
  ret << alphabet[mod]
34
33
  end
35
34
 
36
35
  npad = str.match(/^#{0.chr}*/)[0].to_s.size
37
- '1'*npad + ret.reverse
36
+ '1' * npad + ret.reverse
38
37
  end
39
38
 
40
39
  def address_for(network)
@@ -0,0 +1,17 @@
1
+ module Faker
2
+ class BojackHorseman < Base
3
+ class << self
4
+ def character
5
+ fetch('bojack_horseman.characters')
6
+ end
7
+
8
+ def tongue_twister
9
+ fetch('bojack_horseman.tongue_twisters')
10
+ end
11
+
12
+ def quote
13
+ fetch('bojack_horseman.quotes')
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,9 +1,7 @@
1
- # encoding: utf-8
2
1
  # frozen_string_literal: true
3
2
 
4
3
  module Faker
5
4
  class BreakingBad < Base
6
-
7
5
  def self.character
8
6
  fetch('breaking_bad.character')
9
7
  end
@@ -11,6 +9,5 @@ module Faker
11
9
  def self.episode
12
10
  fetch('breaking_bad.episode')
13
11
  end
14
-
15
12
  end
16
13
  end
@@ -10,13 +10,12 @@ module Faker
10
10
  end
11
11
 
12
12
  def credit_card_expiry_date
13
- ::Date.today + (365 * (rand(4) + 1))
13
+ ::Date.today + (365 * rand(1..4))
14
14
  end
15
15
 
16
16
  def credit_card_type
17
17
  fetch('business.credit_card_types')
18
18
  end
19
19
  end
20
-
21
20
  end
22
21
  end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faker
4
+ class Cannabis < Base
5
+ def self.strain
6
+ fetch('cannabis.strains')
7
+ end
8
+
9
+ def self.cannabinoid_abbreviation
10
+ fetch('cannabis.cannabinoid_abbreviations')
11
+ end
12
+
13
+ def self.cannabinoid
14
+ fetch('cannabis.cannabinoids')
15
+ end
16
+
17
+ def self.terpene
18
+ fetch('cannabis.terpenes')
19
+ end
20
+
21
+ def self.medical_use
22
+ fetch('cannabis.medical_uses')
23
+ end
24
+
25
+ def self.health_benefit
26
+ fetch('cannabis.health_benefits')
27
+ end
28
+
29
+ def self.category
30
+ fetch('cannabis.categories')
31
+ end
32
+
33
+ def self.type
34
+ fetch('cannabis.types')
35
+ end
36
+
37
+ def self.buzzword
38
+ fetch('cannabis.buzzwords')
39
+ end
40
+
41
+ end
42
+ end
@@ -4,8 +4,9 @@ module Faker
4
4
 
5
5
  class << self
6
6
  # from: https://github.com/jenkinsci/chucknorris-plugin/blob/master/src/main/java/hudson/plugins/chucknorris/FactGenerator.java
7
- def fact; fetch('chuck_norris.fact'); end
8
-
7
+ def fact
8
+ fetch('chuck_norris.fact')
9
+ end
9
10
  end
10
11
  end
11
12
  end
@@ -2,11 +2,10 @@ module Faker
2
2
  class Code < Base
3
3
  flexible :code
4
4
  class << self
5
-
6
5
  # Generates a 10 digit NPI (National Provider Identifier
7
6
  # issued to health care providers in the United States)
8
7
  def npi
9
- rand(10 ** 10).to_s.rjust(10, '0')
8
+ rand(10**10).to_s.rjust(10, '0')
10
9
  end
11
10
 
12
11
  # By default generates 10 sign isbn code in format 123456789-X
@@ -49,20 +48,38 @@ module Faker
49
48
  fetch('code.asin')
50
49
  end
51
50
 
52
- private
51
+ # Generates Social Insurance Number issued in Canada
52
+ # https://en.wikipedia.org/wiki/Social_Insurance_Number
53
+ def sin
54
+ # 1 - province or temporary resident
55
+ # 2-8 - random numbers
56
+ # 9 - checksum
57
+
58
+ # 1st digit. 8,0 are not used
59
+ registry = Faker::Base.sample([1, 2, 3, 4, 5, 6, 7, 9]).to_s
60
+
61
+ # generate 2nd to 8th
62
+ partial = Array.new(7) { Faker::Config.random.rand(0..9) }.join
63
+
64
+ # Generate 9th digit
65
+ check_digit = generate_sin_check_digit(registry + partial + '0').to_s
66
+
67
+ registry + partial + check_digit
68
+ end
69
+
70
+ private
53
71
 
54
72
  # Reporting body identifier
55
- RBI = %w(01 10 30 33 35 44 45 49 50 51 52 53 54 86 91 98 99).freeze
73
+ RBI = %w[01 10 30 33 35 44 45 49 50 51 52 53 54 86 91 98 99].freeze
56
74
 
57
75
  def generate_imei
58
- pos = 0
59
76
  str = Array.new(15, 0)
60
77
  sum = 0
61
78
  t = 0
62
79
  len_offset = 0
63
80
  len = 15
64
81
 
65
- # Fill in the first two values of the string based with the specified prefix.
82
+ # Fill in the first two values of the string based with the specified prefix.
66
83
  # Reporting Body Identifier list: http://en.wikipedia.org/wiki/Reporting_Body_Identifier
67
84
  arr = sample(RBI)
68
85
  str[0] = arr[0].to_i
@@ -78,12 +95,10 @@ module Faker
78
95
  # Calculate the Luhn checksum of the values thus far
79
96
  len_offset = (len + 1) % 2
80
97
  (0..(len - 1)).each do |position|
81
- if (position + len_offset) % 2 != 0
98
+ if (position + len_offset).odd?
82
99
  t = str[position] * 2
83
100
 
84
- if t > 9
85
- t -= 9
86
- end
101
+ t -= 9 if t > 9
87
102
 
88
103
  sum += t
89
104
  else
@@ -110,37 +125,61 @@ module Faker
110
125
  values << "-#{((10 - remainder) % 10)}"
111
126
  end
112
127
 
113
- def sum(values, &block)
128
+ def sum(values)
114
129
  values.split(//).each_with_index.inject(0) do |sum, (value, index)|
115
- sum + block.call(value, index)
130
+ sum + yield(value, index)
116
131
  end
117
132
  end
118
133
 
119
134
  def generate_base8_ean
120
135
  values = regexify(/\d{7}/)
121
- check_digit = 10 - values.split(//).each_with_index.inject(0){ |s, (v, i)| s + v.to_i * EAN_CHECK_DIGIT8[i] } % 10
136
+ check_digit = 10 - values.split(//).each_with_index.inject(0) { |s, (v, i)| s + v.to_i * EAN_CHECK_DIGIT8[i] } % 10
122
137
  values << (check_digit == 10 ? 0 : check_digit).to_s
123
138
  end
124
139
 
125
140
  def generate_base13_ean
126
141
  values = regexify(/\d{12}/)
127
- check_digit = 10 - values.split(//).each_with_index.inject(0){ |s, (v, i)| s + v.to_i * EAN_CHECK_DIGIT13[i] } % 10
142
+ check_digit = 10 - values.split(//).each_with_index.inject(0) { |s, (v, i)| s + v.to_i * EAN_CHECK_DIGIT13[i] } % 10
128
143
  values << (check_digit == 10 ? 0 : check_digit).to_s
129
144
  end
130
145
 
131
- EAN_CHECK_DIGIT8 = [3, 1, 3, 1, 3, 1, 3]
132
- EAN_CHECK_DIGIT13 = [1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3]
146
+ EAN_CHECK_DIGIT8 = [3, 1, 3, 1, 3, 1, 3].freeze
147
+ EAN_CHECK_DIGIT13 = [1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3].freeze
133
148
 
134
149
  def rut_verificator_digit(rut)
135
- total = rut.to_s.rjust(8, '0').split(//).zip(%w(3 2 7 6 5 4 3 2)).collect{|a, b| a.to_i * b.to_i}.inject(:+)
150
+ total = rut.to_s.rjust(8, '0').split(//).zip(%w[3 2 7 6 5 4 3 2]).collect { |a, b| a.to_i * b.to_i }.inject(:+)
136
151
  (11 - total % 11).to_s.gsub(/10/, 'k').gsub(/11/, '0')
137
152
  end
138
153
 
139
154
  def generate_nric_check_alphabet(values, prefix)
140
- weight = %w(2 7 6 5 4 3 2)
155
+ weight = %w[2 7 6 5 4 3 2]
141
156
  total = values.split(//).zip(weight).collect { |a, b| a.to_i * b.to_i }.inject(:+)
142
- total = total + 4 if prefix == 'T'
143
- %w(A B C D E F G H I Z J)[10 - total % 11]
157
+ total += 4 if prefix == 'T'
158
+ %w[A B C D E F G H I Z J][10 - total % 11]
159
+ end
160
+
161
+ def generate_sin_check_digit(digits)
162
+ # Fast Luhn checksum code from luhn.js:
163
+ # https://gist.github.com/ShirtlessKirk/2134376
164
+
165
+ len = 9
166
+ mul = 0
167
+
168
+ luhn_arr = [
169
+ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
170
+ [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
171
+ ]
172
+ sum = 0
173
+
174
+ while len > 0
175
+ len -= 1
176
+ num = digits[len].to_i
177
+ sum += luhn_arr[mul][num]
178
+ mul ^= 1
179
+ end
180
+
181
+ checksum = sum % 10
182
+ checksum.zero? ? checksum : (10 - checksum)
144
183
  end
145
184
  end
146
185
  end