faker 3.2.0 → 3.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -46,9 +46,23 @@ module Faker
46
46
  end
47
47
 
48
48
  def ssn_valid
49
- ssn = regexify(/[0-8]\d{2}-\d{2}-\d{4}/)
50
- # We could still have all 0s in one segment or another
51
- INVALID_SSN.any? { |regex| regex =~ ssn } ? ssn_valid : ssn
49
+ generate(:string) do |g|
50
+ g.computed(name: :first) do
51
+ range = [1..665, 667..899].sample(random: Faker::Config.random)
52
+ n = Faker::Base.rand(range)
53
+ format('%03d', n)
54
+ end
55
+ g.lit('-')
56
+ g.computed(name: :second) do
57
+ n = Faker::Base.rand(1..99)
58
+ format('%02d', n)
59
+ end
60
+ g.lit('-')
61
+ g.computed(name: :third) do
62
+ n = Faker::Base.rand(1..9999)
63
+ format('%04d', n)
64
+ end
65
+ end
52
66
  end
53
67
 
54
68
  ##
@@ -159,7 +159,8 @@ module Faker
159
159
  #
160
160
  # @faker.version 2.1.3
161
161
  def password(min_length: 8, max_length: 16, mix_case: true, special_characters: false)
162
- raise ArgumentError, 'max_length must be more than min_length' if max_length < min_length
162
+ raise ArgumentError, 'min_length and max_length must be greater than or equal to one' if min_length < 1 || max_length < 1
163
+ raise ArgumentError, 'min_length must be smaller than or equal to max_length' unless min_length <= max_length
163
164
 
164
165
  character_types = []
165
166
  required_min_length = 0
@@ -182,27 +183,27 @@ module Faker
182
183
  character_bag = []
183
184
 
184
185
  # use lower_chars by default and add upper_chars if mix_case
185
- lower_chars = ('a'..'z').to_a
186
- password << lower_chars[rand(lower_chars.count - 1)]
186
+ lower_chars = self::LLetters
187
+ password << sample(lower_chars)
187
188
  character_bag += lower_chars
188
189
 
189
- digits = ('1'..'9').to_a
190
- password << digits[rand(digits.count - 1)]
190
+ digits = ('0'..'9').to_a
191
+ password << sample(digits)
191
192
  character_bag += digits
192
193
 
193
- if character_types.include?(:mix_case)
194
- upper_chars = ('A'..'Z').to_a
195
- password << upper_chars[rand(upper_chars.count - 1)]
194
+ if mix_case
195
+ upper_chars = self::ULetters
196
+ password << sample(upper_chars)
196
197
  character_bag += upper_chars
197
198
  end
198
199
 
199
- if character_types.include?(:special_characters)
200
+ if special_characters
200
201
  special_chars = %w[! @ # $ % ^ & *]
201
- password << special_chars[rand(special_chars.count - 1)]
202
+ password << sample(special_chars)
202
203
  character_bag += special_chars
203
204
  end
204
205
 
205
- password << character_bag[rand(character_bag.count - 1)] while password.length < target_length
206
+ password << sample(character_bag) while password.length < target_length
206
207
 
207
208
  shuffle(password).join
208
209
  end
@@ -10,10 +10,13 @@ module Faker
10
10
  #
11
11
  # @example
12
12
  # Faker::Lorem.word #=> "soluto"
13
+ # Faker::Lorem.word(exclude_words: 'error') #=> "nisi"
14
+ # Faker::Lorem.word(exclude_words: 'id, error') #=> "et"
15
+ # Faker::Lorem.word(exclude_words: ['id', 'error']) #=> "consequatur"
13
16
  #
14
17
  # @faker.version 2.1.3
15
- def word
16
- sample(translate('faker.lorem.words'))
18
+ def word(exclude_words: nil)
19
+ words(number: 1, exclude_words: exclude_words).first
17
20
  end
18
21
 
19
22
  ##
@@ -0,0 +1,59 @@
1
+
2
+
3
+ ## What's Changed
4
+ * Add Final Fantasy XIV by @old-dead-account in https://github.com/faker-ruby/faker/pull/2742
5
+ * Update rubocop requirement from = 1.50.1 to = 1.50.2 by @dependabot in https://github.com/faker-ruby/faker/pull/2752
6
+ * Update South Park by @IvanReyesO7 in https://github.com/faker-ruby/faker/pull/2744
7
+ * add tests for password and fix an edge case by @DeepakRaj228 in https://github.com/faker-ruby/faker/pull/2741
8
+ * Speed up Internet::Password generation using constants by @MicBruz in https://github.com/faker-ruby/faker/pull/2725
9
+ * Add the Ukrainian country calling code by @kyrylo in https://github.com/faker-ruby/faker/pull/2758
10
+ * Update test-unit requirement from = 3.5.7 to = 3.5.8 by @dependabot in https://github.com/faker-ruby/faker/pull/2760
11
+ * Bump i18n from 1.12.0 to 1.13.0 by @dependabot in https://github.com/faker-ruby/faker/pull/2756
12
+ * Update rubocop-minitest requirement from = 0.30.0 to = 0.31.0 by @dependabot in https://github.com/faker-ruby/faker/pull/2759
13
+ * Remove broken chars from minecraft.yml by @ujihisa in https://github.com/faker-ruby/faker/pull/2765
14
+ * Update test-unit requirement from = 3.5.8 to = 3.5.9 by @dependabot in https://github.com/faker-ruby/faker/pull/2766
15
+ * Add `exclude_words` filter to `Faker::Lorem.word` generator by @geophilusd in https://github.com/faker-ruby/faker/pull/2761
16
+ * Improve de-CH locale with new formats and content by @stefnnn in https://github.com/faker-ruby/faker/pull/2768
17
+ * Add Japanese translations for Sports category. by @yamat47 in https://github.com/faker-ruby/faker/pull/2770
18
+ * Fix flaky specs for `name` and `id` by @ruban-thilak in https://github.com/faker-ruby/faker/pull/2782
19
+ * Add type support for Faker::Types.rb_array by @ruban-thilak in https://github.com/faker-ruby/faker/pull/2771
20
+ * Added Archer into tv category. by @lepari23 in https://github.com/faker-ruby/faker/pull/2750
21
+ * Add train station generator by @AngusDSR in https://github.com/faker-ruby/faker/pull/2755
22
+ * Fixes `Faker::Music::Opera.saint_saens` issue by @devashishTaneja in https://github.com/faker-ruby/faker/pull/2792
23
+ * Add custom start date for `Faker::Date.forward` by @luciagirasoles in https://github.com/faker-ruby/faker/pull/2791
24
+ * Fix flaky specs for dota `test_player` by @ruban-thilak in https://github.com/faker-ruby/faker/pull/2798
25
+ * Update minitest requirement from = 5.18.0 to = 5.18.1 by @dependabot in https://github.com/faker-ruby/faker/pull/2781
26
+ * Update test-unit requirement from = 3.5.9 to = 3.6.1 by @dependabot in https://github.com/faker-ruby/faker/pull/2788
27
+ * Introduce PositionalGenerator by @mike-burns in https://github.com/faker-ruby/faker/pull/2710
28
+ * Bump rubocop 1.54.2 + offenses fixes by @stefannibrasil in https://github.com/faker-ruby/faker/pull/2801
29
+ * Add prefixes to french name locale (`Faker::Name.name`) by @thdaraujo in https://github.com/faker-ruby/faker/pull/2800
30
+ * Add `max_rut` option to `Faker::ChileRut.rut` by @hacktivista in https://github.com/faker-ruby/faker/pull/2778
31
+ * Fix locale setting by @mateusdeap in https://github.com/faker-ruby/faker/pull/2734
32
+ * Test default locale + and update README by @stefannibrasil in https://github.com/faker-ruby/faker/pull/2802
33
+ * Bump minitest from 5.18.1 to 5.19.0 by @dependabot in https://github.com/faker-ruby/faker/pull/2804
34
+ * Bump rubocop from 1.54.2 to 1.55.0 by @dependabot in https://github.com/faker-ruby/faker/pull/2803
35
+ * Bump rubocop from 1.55.0 to 1.55.1 by @dependabot in https://github.com/faker-ruby/faker/pull/2805
36
+ * Add Faker::Date.day_of_week_between by @aramvisser in https://github.com/faker-ruby/faker/pull/2713
37
+ * Html generator for Faker by @ruban-thilak in https://github.com/faker-ruby/faker/pull/2769
38
+ * Bump rubocop from 1.55.1 to 1.56.0 by @dependabot in https://github.com/faker-ruby/faker/pull/2807
39
+
40
+ ## New Contributors
41
+ * @old-dead-account made their first contribution in https://github.com/faker-ruby/faker/pull/2742
42
+ * @IvanReyesO7 made their first contribution in https://github.com/faker-ruby/faker/pull/2744
43
+ * @DeepakRaj228 made their first contribution in https://github.com/faker-ruby/faker/pull/2741
44
+ * @MicBruz made their first contribution in https://github.com/faker-ruby/faker/pull/2725
45
+ * @kyrylo made their first contribution in https://github.com/faker-ruby/faker/pull/2758
46
+ * @ujihisa made their first contribution in https://github.com/faker-ruby/faker/pull/2765
47
+ * @geophilusd made their first contribution in https://github.com/faker-ruby/faker/pull/2761
48
+ * @stefnnn made their first contribution in https://github.com/faker-ruby/faker/pull/2768
49
+ * @yamat47 made their first contribution in https://github.com/faker-ruby/faker/pull/2770
50
+ * @ruban-thilak made their first contribution in https://github.com/faker-ruby/faker/pull/2782
51
+ * @lepari23 made their first contribution in https://github.com/faker-ruby/faker/pull/2750
52
+ * @AngusDSR made their first contribution in https://github.com/faker-ruby/faker/pull/2755
53
+ * @devashishTaneja made their first contribution in https://github.com/faker-ruby/faker/pull/2792
54
+ * @mike-burns made their first contribution in https://github.com/faker-ruby/faker/pull/2710
55
+ * @hacktivista made their first contribution in https://github.com/faker-ruby/faker/pull/2778
56
+ * @mateusdeap made their first contribution in https://github.com/faker-ruby/faker/pull/2734
57
+ * @aramvisser made their first contribution in https://github.com/faker-ruby/faker/pull/2713
58
+
59
+ **Full Changelog**: https://github.com/faker-ruby/faker/compare/v3.2.0...v3.2.1
@@ -97,12 +97,13 @@ module Faker
97
97
  # @example
98
98
  # Faker::Types.rb_array #=> ["a"]
99
99
  # Faker::Types.rb_array(len: 4) #=> ["a", 1, 2, "bob"]
100
+ # Faker::Types.rb_array(len: 2, type: -> { Faker::Types.rb_string }) #=> ["cat", "foo"]
100
101
  #
101
102
  # @faker.version 1.8.6
102
- def rb_array(len: 1)
103
+ def rb_array(len: 1, type: -> { random_type })
103
104
  [].tap do |ar|
104
105
  len.times do
105
- ar.push random_type
106
+ ar.push type.is_a?(Proc) ? type.call : type
106
107
  end
107
108
  end
108
109
  end
@@ -22,14 +22,25 @@ module Faker
22
22
  #
23
23
  # @faker.version 1.6.4
24
24
  def vin
25
- front = 8.times.map { VIN_KEYSPACE.sample(random: Faker::Config.random) }.join
26
- back = 8.times.map { VIN_KEYSPACE.sample(random: Faker::Config.random) }.join
27
- checksum = "#{front}A#{back}".chars.each_with_index.map do |char, i|
28
- value = (char =~ /\A\d\z/ ? char.to_i : VIN_TRANSLITERATION[char.to_sym])
29
- value * VIN_WEIGHT[i]
30
- end.inject(:+) % 11
31
- checksum = 'X' if checksum == 10
32
- "#{front}#{checksum}#{back}"
25
+ generate(:string) do |g|
26
+ g.letter(name: :wmi, ranges: ['100'..'199', '400'..'499', '500'..'599', '700'..'799', '7A0'..'7F9'])
27
+ g.letter(name: :vds, length: 5, ranges: [VIN_KEYSPACE])
28
+ g.computed(name: :checksum, deps: %i[wmi vds model_year plant_code vis]) do |wmi, vds, model_year, plant_code, vis|
29
+ checksum = "#{wmi}#{vds}0#{model_year}#{plant_code}#{vis}".chars.each_with_index.map do |char, i|
30
+ value = (char =~ /\A\d\z/ ? char.to_i : VIN_TRANSLITERATION[char.to_sym])
31
+ value * VIN_WEIGHT[i]
32
+ end.inject(:+) % 11
33
+
34
+ if checksum == 10
35
+ 'X'
36
+ else
37
+ checksum
38
+ end
39
+ end
40
+ g.letter(name: :model_year, length: 1, ranges: [VIN_KEYSPACE - %w[U Z 0]])
41
+ g.letter(name: :plant_code, length: 1, ranges: [VIN_KEYSPACE])
42
+ g.int(name: :vis, length: 6)
43
+ end
33
44
  end
34
45
 
35
46
  # Produces a random vehicle manufacturer.
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faker
4
+ class Games
5
+ class FinalFantasyXIV < Base
6
+ class << self
7
+ ##
8
+ # Produces the name of a character from FFXIV.
9
+ #
10
+ # @return [String]
11
+ #
12
+ # @example
13
+ # Faker::Games::FinalFantasyXIV.character #=> "Y'shtola Rhul"
14
+ #
15
+ # @faker.version next
16
+ def character
17
+ fetch('games.final_fantasy_xiv.characters')
18
+ end
19
+
20
+ ##
21
+ # Produces a job name from Final Fantasy XIV. Either a battle or non-battle playable job.
22
+ #
23
+ # @return [String]
24
+ #
25
+ # @example
26
+ # Faker::Games::FinalFantasyXIV.job #=> "Paladin"
27
+ #
28
+ # @faker.version next
29
+ def job
30
+ fetch('games.final_fantasy_xiv.jobs')
31
+ end
32
+
33
+ # Produces the name of a playable race from Final Fantasy XIV.
34
+ #
35
+ # @return [String]
36
+ #
37
+ # @example
38
+ # Faker::Games::FinalFantasyXIV.race #=> "Miqo'te"
39
+ #
40
+ # @faker.version next
41
+ def race
42
+ fetch('games.final_fantasy_xiv.races')
43
+ end
44
+
45
+ ##
46
+ # Produces a data center from Final Fantasy XIV.
47
+ #
48
+ # @return [String]
49
+ #
50
+ # @example
51
+ # Faker::Games::FinalFantasyXIV.data_center #=> "Aether"
52
+ #
53
+ # @faker.version next
54
+ def data_center
55
+ fetch('games.final_fantasy_xiv.data_centers')
56
+ end
57
+
58
+ ##
59
+ # Produces a geographical zone from Final Fantasy XIV.
60
+ #
61
+ # @return [String]
62
+ #
63
+ # @example
64
+ # Faker::Games::FinalFantasyXIV.zone #=> "Eastern La Noscea"
65
+ #
66
+ # @faker.version next
67
+ def zone
68
+ fetch('games.final_fantasy_xiv.zones')
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -11,7 +11,7 @@ module Faker
11
11
  #
12
12
  # @param region [String] airport region, currently available -> united_states or european_union
13
13
  #
14
- # @retrun [String]
14
+ # @return [String]
15
15
  #
16
16
  # @example
17
17
  # Faker::Travel::Airport.name(size: 'large', region: 'united_states') => "Los Angeles International Airport"
@@ -28,7 +28,7 @@ module Faker
28
28
  #
29
29
  # @param region [String] airport region, currently available -> united_states or european_union
30
30
  #
31
- # @retrun [String]
31
+ # @return [String]
32
32
  #
33
33
  # @example
34
34
  # Faker::Travel::Airport.iata(size: 'large', region: 'united_states') => "LAX"
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faker
4
+ class Travel
5
+ class TrainStation < Base
6
+ class << self
7
+ ##
8
+ # Produces random Train Station by name and takes optional arguments for region and type
9
+ #
10
+ # @param region [String] Train station region: germany, spain, united_kingdom, united_states
11
+ #
12
+ # @param type [String] Train station type: metro, railway
13
+ #
14
+ # @return [String]
15
+ #
16
+ # @examples
17
+ # Faker::Travel::TrainStation.name(region: 'united_kingdom', type: 'metro') => "Brockley"
18
+ # Faker::Travel::TrainStation.name(type: 'railway') => "Düsseldorf Hauptbahnhof"
19
+ # Faker::Travel::TrainStation.name(region: 'spain') => "Santa Eulàlia"
20
+ #
21
+ # @faker.version next
22
+ def name(region: nil, type: nil)
23
+ region, type = fill_missing_inputs_with_samples(region, type)
24
+ fetch("train_station.#{region}.#{type}")
25
+ end
26
+
27
+ private
28
+
29
+ def fill_missing_inputs_with_samples(region, type)
30
+ regions = %w[germany spain united_kingdom united_states]
31
+ types = %w[metro railway]
32
+
33
+ if region.nil? && type.nil?
34
+ region = sample(regions)
35
+ type = sample(types)
36
+ elsif region.nil?
37
+ validate_arguments(type, types, 'type')
38
+ region = sample(regions)
39
+ elsif type.nil?
40
+ validate_arguments(region, regions, 'region')
41
+ type = sample(types)
42
+ end
43
+ [region, type]
44
+ end
45
+
46
+ def validate_arguments(argument, correct_values, argument_name)
47
+ return if correct_values.include?(argument)
48
+
49
+ raise ArgumentError, "'#{argument}' not found, #{argument_name} can be blank, or one of the following, as strings: #{correct_values.join(', ')}"
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faker
4
+ class TvShows
5
+ class Archer < Base
6
+ flexible :archer
7
+
8
+ class << self
9
+ ##
10
+ # Produces a character from Archer.
11
+ #
12
+ # @return [String]
13
+ #
14
+ # @example
15
+ # Faker::TvShows::Archer.character #=> "Sterling Archer"
16
+ #
17
+ # @faker.version next
18
+ def character
19
+ fetch('archer.characters')
20
+ end
21
+
22
+ ##
23
+ # Produces a location from Archer.
24
+ #
25
+ # @return [String]
26
+ #
27
+ # @example
28
+ # Faker::TvShows::Archer.location #=> "The Tuntmore Towers"
29
+ #
30
+ # @faker.version next
31
+ def location
32
+ fetch('archer.locations')
33
+ end
34
+
35
+ ##
36
+ # Produces a quote from Archer.
37
+ #
38
+ # @return [String]
39
+ #
40
+ # @example
41
+ # Faker::TvShows::Archer.quote
42
+ # #=> "You're not my supervisor!"
43
+ #
44
+ # @faker.version next
45
+ def quote
46
+ fetch('archer.quotes')
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -32,6 +32,21 @@ module Faker
32
32
  def quote
33
33
  fetch('south_park.quotes')
34
34
  end
35
+
36
+ ##
37
+ # Produces an episode name from South Park.
38
+ #
39
+ # @return [String]
40
+ #
41
+ # @example
42
+ # Faker::TvShows::SouthPark.episode_name
43
+ # #=> "Make Love, Not Warcraft"
44
+ #
45
+ # @faker.version next
46
+
47
+ def episode_name
48
+ fetch('south_park.episodes')
49
+ end
35
50
  end
36
51
  end
37
52
  end
data/lib/faker/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Faker # :nodoc:
4
- VERSION = '3.2.0'
4
+ VERSION = '3.2.1'
5
5
  end
data/lib/faker.rb CHANGED
@@ -11,14 +11,18 @@ I18n.load_path += Dir[File.join(mydir, 'locales', '**/*.yml')]
11
11
 
12
12
  module Faker
13
13
  module Config
14
+ @default_locale = nil
15
+
14
16
  class << self
17
+ attr_writer :default_locale
18
+
15
19
  def locale=(new_locale)
16
20
  Thread.current[:faker_config_locale] = new_locale
17
21
  end
18
22
 
19
23
  def locale
20
24
  # Because I18n.locale defaults to :en, if we don't have :en in our available_locales, errors will happen
21
- Thread.current[:faker_config_locale] || (I18n.available_locales.include?(I18n.locale) ? I18n.locale : I18n.available_locales.first)
25
+ Thread.current[:faker_config_locale] || @default_locale || (I18n.available_locales.include?(I18n.locale) ? I18n.locale : I18n.available_locales.first)
22
26
  end
23
27
 
24
28
  def own_locale
@@ -48,19 +52,23 @@ module Faker
48
52
 
49
53
  ## by default numerify results do not start with a zero
50
54
  def numerify(number_string, leading_zero: false)
51
- return number_string.gsub(/#/) { rand(10).to_s } if leading_zero
55
+ return number_string.gsub('#') { rand(10).to_s } if leading_zero
52
56
 
53
- number_string.sub(/#/) { rand(1..9).to_s }.gsub(/#/) { rand(10).to_s }
57
+ number_string.sub('#') { rand(1..9).to_s }.gsub('#') { rand(10).to_s }
54
58
  end
55
59
 
56
60
  def letterify(letter_string)
57
- letter_string.gsub(/\?/) { sample(ULetters) }
61
+ letter_string.gsub('?') { sample(ULetters) }
58
62
  end
59
63
 
60
64
  def bothify(string)
61
65
  letterify(numerify(string))
62
66
  end
63
67
 
68
+ def generate(as_type, &block)
69
+ PositionalGenerator.new(as_type, &block).generate
70
+ end
71
+
64
72
  # Given a regular expression, attempt to generate a string
65
73
  # that would match it. This is a rather simple implementation,
66
74
  # so don't be shocked if it blows up on you in a spectacular fashion.
@@ -84,7 +92,7 @@ module Faker
84
92
  reg = reg.source if reg.respond_to?(:source) # Handle either a Regexp or a String that looks like a Regexp
85
93
  reg
86
94
  .gsub(%r{^/?\^?}, '').gsub(%r{\$?/?$}, '') # Ditch the anchors
87
- .gsub(/\{(\d+)\}/, '{\1,\1}').gsub(/\?/, '{0,1}') # All {2} become {2,2} and ? become {0,1}
95
+ .gsub(/\{(\d+)\}/, '{\1,\1}').gsub('?', '{0,1}') # All {2} become {2,2} and ? become {0,1}
88
96
  .gsub(/(\[[^\]]+\])\{(\d+),(\d+)\}/) { |_match| Regexp.last_match(1) * sample(Array(Range.new(Regexp.last_match(2).to_i, Regexp.last_match(3).to_i))) } # [12]{1,2} becomes [12] or [12][12]
89
97
  .gsub(/(\([^)]+\))\{(\d+),(\d+)\}/) { |_match| Regexp.last_match(1) * sample(Array(Range.new(Regexp.last_match(2).to_i, Regexp.last_match(3).to_i))) } # (12|34){1,2} becomes (12|34) or (12|34)(12|34)
90
98
  .gsub(/(\\?.)\{(\d+),(\d+)\}/) { |_match| Regexp.last_match(1) * sample(Array(Range.new(Regexp.last_match(2).to_i, Regexp.last_match(3).to_i))) } # A{1,2} becomes A or AA or \d{3} becomes \d\d\d