faker 1.6.6 → 1.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +310 -0
  3. data/History.md +176 -0
  4. data/README.md +61 -830
  5. data/lib/faker.rb +18 -2
  6. data/lib/faker/address.rb +7 -3
  7. data/lib/faker/ancient.rb +21 -0
  8. data/lib/faker/artist.rb +9 -0
  9. data/lib/faker/bank.rb +30 -0
  10. data/lib/faker/bitcoin.rb +1 -2
  11. data/lib/faker/code.rb +5 -5
  12. data/lib/faker/color.rb +6 -29
  13. data/lib/faker/commerce.rb +15 -2
  14. data/lib/faker/company.rb +9 -5
  15. data/lib/faker/educator.rb +3 -3
  16. data/lib/faker/esport.rb +25 -0
  17. data/lib/faker/fillmurray.rb +18 -0
  18. data/lib/faker/finance.rb +3 -1
  19. data/lib/faker/food.rb +17 -0
  20. data/lib/faker/game_of_thrones.rb +8 -0
  21. data/lib/faker/hipster.rb +1 -1
  22. data/lib/faker/internet.rb +5 -6
  23. data/lib/faker/job.rb +16 -0
  24. data/lib/faker/lord_of_the_rings.rb +13 -0
  25. data/lib/faker/lorem_pixel.rb +21 -0
  26. data/lib/faker/music.rb +12 -0
  27. data/lib/faker/number.rb +7 -0
  28. data/lib/faker/rock_band.rb +9 -0
  29. data/lib/faker/space.rb +4 -0
  30. data/lib/faker/star_wars.rb +13 -0
  31. data/lib/faker/time.rb +10 -6
  32. data/lib/faker/twin_peaks.rb +17 -0
  33. data/lib/faker/version.rb +1 -1
  34. data/lib/helpers/unique_generator.rb +24 -0
  35. data/lib/locales/de.yml +39 -5
  36. data/lib/locales/en-IND.yml +5 -2
  37. data/lib/locales/en-NG.yml +75 -0
  38. data/lib/locales/en-NZ.yml +26 -9
  39. data/lib/locales/en-US.yml +3 -0
  40. data/lib/locales/en-ZA.yml +51 -0
  41. data/lib/locales/en.yml +150 -10
  42. data/lib/locales/es-MX.yml +12 -5
  43. data/lib/locales/fr.yml +1 -1
  44. data/lib/locales/id.yml +21 -0
  45. data/lib/locales/it.yml +1 -1
  46. data/lib/locales/ja.yml +3 -0
  47. data/lib/locales/nl.yml +7 -1
  48. data/lib/locales/pl.yml +1 -1
  49. data/lib/locales/pt-BR.yml +22 -22
  50. data/lib/locales/tr.yml +26 -0
  51. metadata +21 -4
  52. data/History.txt +0 -180
@@ -90,7 +90,7 @@ module Faker
90
90
  def fetch(key)
91
91
  fetched = translate("faker.#{key}")
92
92
  fetched = fetched.sample if fetched.respond_to?(:sample)
93
- if fetched.match(/^\//) and fetched.match(/\/$/) # A regex
93
+ if fetched && fetched.match(/^\//) and fetched.match(/\/$/) # A regex
94
94
  regexify(fetched)
95
95
  else
96
96
  fetched
@@ -180,7 +180,11 @@ module Faker
180
180
  # Generates a random value between the interval
181
181
  def rand_in_range(from, to)
182
182
  from, to = to, from if to < from
183
- Random.new.rand(from..to)
183
+ rand(from..to)
184
+ end
185
+
186
+ def unique(max_retries = 10_000)
187
+ @unique_generator ||= UniqueGenerator.new(self, max_retries)
184
188
  end
185
189
  end
186
190
  end
@@ -226,11 +230,23 @@ require 'faker/educator'
226
230
  require 'faker/space'
227
231
  require 'faker/yoda'
228
232
  require 'faker/music'
233
+ require 'faker/artist'
229
234
  require 'faker/vehicle'
230
235
  require 'faker/game_of_thrones'
231
236
  require 'faker/pokemon'
237
+ require 'faker/food'
238
+ require 'faker/lorem_pixel'
239
+ require 'faker/esport'
240
+ require 'faker/bank'
241
+ require 'faker/ancient'
242
+ require 'faker/twin_peaks'
243
+ require 'faker/lord_of_the_rings'
244
+ require 'faker/rock_band'
245
+ require 'faker/fillmurray'
246
+ require 'faker/job'
232
247
 
233
248
  require 'extensions/array'
234
249
  require 'extensions/symbol'
235
250
 
236
251
  require 'helpers/char'
252
+ require 'helpers/unique_generator'
@@ -3,8 +3,8 @@ module Faker
3
3
  flexible :address
4
4
 
5
5
  class << self
6
- def city
7
- parse('address.city')
6
+ def city(options = {})
7
+ parse(options[:with_state] ? 'address.city_with_state' : 'address.city')
8
8
  end
9
9
 
10
10
  def street_name
@@ -16,7 +16,7 @@ module Faker
16
16
  end
17
17
 
18
18
  def secondary_address
19
- numerify(fetch('address.secondary_address'))
19
+ bothify(fetch('address.secondary_address'))
20
20
  end
21
21
 
22
22
  def building_number
@@ -53,6 +53,10 @@ module Faker
53
53
  def longitude
54
54
  ((rand * 360) - 180).to_s
55
55
  end
56
+
57
+ def full_address
58
+ parse('address.full_address')
59
+ end
56
60
  end
57
61
  end
58
62
  end
@@ -0,0 +1,21 @@
1
+ module Faker
2
+ class Ancient < Base
3
+ class << self
4
+ def god
5
+ fetch('ancient.god')
6
+ end
7
+
8
+ def primordial
9
+ fetch('ancient.primordial')
10
+ end
11
+
12
+ def titan
13
+ fetch('ancient.titan')
14
+ end
15
+
16
+ def hero
17
+ fetch('ancient.hero')
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ module Faker
2
+ class Artist < Base
3
+ class << self
4
+ def name
5
+ fetch('artist.names')
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,30 @@
1
+ module Faker
2
+ class Bank < Base
3
+ flexible :bank
4
+
5
+ class << self
6
+ def name
7
+ fetch('bank.name')
8
+ end
9
+
10
+ def swift_bic
11
+ fetch('bank.swift_bic')
12
+ end
13
+
14
+ def iban(bank_country_code="GB")
15
+ details = iban_details.find { |country| country["bank_country_code"] == bank_country_code.upcase }
16
+ bcc = details["bank_country_code"] + 2.times.map{ rand(10) }.join
17
+ ilc = (0...details["iban_letter_code"].to_i).map{ (65 + rand(26)).chr }.join
18
+ ib = details["iban_digits"].to_i.times.map{ rand(10) }.join
19
+ bcc + ilc + ib
20
+ end
21
+
22
+ private
23
+
24
+ def iban_details
25
+ fetch_all('bank.iban_details')
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -39,8 +39,7 @@ module Faker
39
39
 
40
40
  def address_for(network)
41
41
  version = PROTOCOL_VERSIONS.fetch(network)
42
- hash = SecureRandom.hex(20)
43
- packed = version.chr + [hash].pack("H*")
42
+ packed = version.chr + Random::DEFAULT.bytes(20)
44
43
  checksum = Digest::SHA2.digest(Digest::SHA2.digest(packed))[0..3]
45
44
  base58(packed + checksum)
46
45
  end
@@ -6,7 +6,7 @@ module Faker
6
6
  # Generates a 10 digit NPI (National Provider Identifier
7
7
  # issued to health care providers in the United States)
8
8
  def npi
9
- Random.new.rand(10 ** 10).to_s.rjust(10, '0')
9
+ rand(10 ** 10).to_s.rjust(10, '0')
10
10
  end
11
11
 
12
12
  # By default generates 10 sign isbn code in format 123456789-X
@@ -77,9 +77,9 @@ module Faker
77
77
 
78
78
  # Calculate the Luhn checksum of the values thus far
79
79
  len_offset = (len + 1) % 2
80
- for pos in (0..(len - 1))
81
- if (pos + len_offset) % 2 != 0
82
- t = str[pos] * 2
80
+ (0..(len - 1)).each do |position|
81
+ if (position + len_offset) % 2 != 0
82
+ t = str[position] * 2
83
83
 
84
84
  if t > 9
85
85
  t -= 9
@@ -87,7 +87,7 @@ module Faker
87
87
 
88
88
  sum += t
89
89
  else
90
- sum += str[pos]
90
+ sum += str[position]
91
91
  end
92
92
  end
93
93
 
@@ -2,7 +2,7 @@ module Faker
2
2
  class Color < Base
3
3
  class << self
4
4
  def hex_color
5
- @hex_color = "#%06x" % (rand * 0xffffff)
5
+ '#%06x' % (rand * 0xffffff)
6
6
  end
7
7
 
8
8
  def color_name
@@ -10,43 +10,20 @@ module Faker
10
10
  end
11
11
 
12
12
  def single_rgb_color
13
- @single_rgb_color = (0..255).to_a.sample
14
- @single_rgb_color
13
+ (0..255).to_a.sample
15
14
  end
16
15
 
17
16
  def rgb_color
18
- @rgb_colors = []
19
- 3.times do
20
- @rgb_colors.push single_rgb_color
21
- end
22
- @rgb_colors
23
- end
24
-
25
- def single_hsl_color
26
- @single_hsla_color = Faker::Base::rand_in_range(0.0, 360.00).round(2)
27
- @single_hsla_color
28
- end
29
-
30
- def alpha_channel
31
- @alpha_channel = rand
32
- @alpha_channel
17
+ 3.times.collect { single_rgb_color }
33
18
  end
34
19
 
20
+ # returns [hue, saturation, lightness]
35
21
  def hsl_color
36
- @hsl_colors = []
37
- 3.times do
38
- @hsl_colors.push single_hsl_color
39
- end
40
- @hsl_colors
22
+ [(0..360).to_a.sample, rand.round(2), rand.round(2)]
41
23
  end
42
24
 
43
25
  def hsla_color
44
- @hsla_colors = []
45
- 3.times do
46
- @hsla_colors.push single_hsl_color
47
- end
48
- @hsla_colors.push alpha_channel
49
- @hsla_colors
26
+ hsl_color << rand.round(1)
50
27
  end
51
28
  end
52
29
  end
@@ -6,6 +6,14 @@ module Faker
6
6
  fetch('color.name')
7
7
  end
8
8
 
9
+ def promotion_code(digits = 6)
10
+ [
11
+ fetch('commerce.promotion_code.adjective'),
12
+ fetch('commerce.promotion_code.noun'),
13
+ Faker::Number.number(digits)
14
+ ].join
15
+ end
16
+
9
17
  def department(max = 3, fixed_amount = false)
10
18
  num = max if fixed_amount
11
19
  num ||= 1 + rand(max)
@@ -27,9 +35,14 @@ module Faker
27
35
  fetch('commerce.product_name.material')
28
36
  end
29
37
 
30
- def price(range=0..100.0)
38
+ def price(range=0..100.0, string=false)
31
39
  random = Random::DEFAULT
32
- (random.rand(range) * 100).floor/100.0
40
+ price = (random.rand(range) * 100).floor/100.0
41
+ if string
42
+ price_parts = price.to_s.split('.')
43
+ price = price_parts[0] + price_parts[-1].ljust(2, "0")
44
+ end
45
+ price
33
46
  end
34
47
 
35
48
  private
@@ -43,8 +43,12 @@ module Faker
43
43
  "https://pigment.github.io/fake-logos/logos/medium/color/#{rand_num}.png"
44
44
  end
45
45
 
46
+ # Get a random Swedish organization number. See more here https://sv.wikipedia.org/wiki/Organisationsnummer
46
47
  def swedish_organisation_number
47
- base = ('%09d' % rand(10 ** 9))
48
+ # Valid leading digit: 1, 2, 3, 5, 6, 7, 8, 9
49
+ # Valid third digit: >= 2
50
+ # Last digit is a control digit
51
+ base = [[1, 2, 3, 5, 6, 7, 8, 9].sample, (0..9).to_a.sample, (2..9).to_a.sample, ('%06d' % rand(10 ** 6))].join
48
52
  base + luhn_algorithm(base).to_s
49
53
  end
50
54
 
@@ -65,10 +69,10 @@ module Faker
65
69
  multiplications = []
66
70
 
67
71
  number.split(//).each_with_index do |digit, i|
68
- if i % 2 == 0
69
- multiplications << digit.to_i * 2
70
- else
71
- multiplications << digit.to_i
72
+ if i.even?
73
+ multiplications << digit.to_i * 2
74
+ else
75
+ multiplications << digit.to_i
72
76
  end
73
77
  end
74
78
 
@@ -4,7 +4,7 @@ module Faker
4
4
 
5
5
  class << self
6
6
  def university
7
- "#{fetch('educator.name')} #{fetch('educator.tertiary.type')}"
7
+ "#{parse('educator.name')} #{fetch('educator.tertiary.type')}"
8
8
  end
9
9
 
10
10
  def course
@@ -12,11 +12,11 @@ module Faker
12
12
  end
13
13
 
14
14
  def secondary_school
15
- "#{fetch('educator.name')} #{fetch('educator.secondary')}"
15
+ "#{parse('educator.name')} #{fetch('educator.secondary')}"
16
16
  end
17
17
 
18
18
  def campus
19
- "#{fetch('educator.name')} Campus"
19
+ "#{parse('educator.name')} Campus"
20
20
  end
21
21
  end
22
22
  end
@@ -0,0 +1,25 @@
1
+ module Faker
2
+ class Esport < Base
3
+ class << self
4
+ def player
5
+ fetch('esports.players')
6
+ end
7
+
8
+ def team
9
+ fetch('esports.teams')
10
+ end
11
+
12
+ def league
13
+ fetch('esports.leagues')
14
+ end
15
+
16
+ def event
17
+ fetch('esports.events')
18
+ end
19
+
20
+ def game
21
+ fetch('esports.games')
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,18 @@
1
+
2
+
3
+ module Faker
4
+ class Fillmurray < Base
5
+ class << self
6
+
7
+ def image(grayscale = false, width = 200, height = 200)
8
+ raise ArgumentError, "Width should be a number in string format" unless width.match(/^[0-9]+$/)
9
+ raise ArgumentError, "Height should be a number in string format" unless height.match(/^[0-9]+$/)
10
+ raise ArgumentError, "Grayscale should be a boolean" unless [true, false].include?(grayscale)
11
+
12
+ fillmurray_url = grayscale == true ? "https://fillmurray.com/g/#{width}/#{height}" : "https://fillmurray.com/#{width}/#{height}"
13
+
14
+ fillmurray_url
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,6 +1,8 @@
1
1
  module Faker
2
2
  class Finance < Base
3
- CREDIT_CARD_TYPES = [ :visa, :mastercard, :discover, :american_express, :diners_club, :jcb, :switch, :solo, :dankort, :maestro, :forbrugsforeningen, :laser ]
3
+ CREDIT_CARD_TYPES = [:visa, :mastercard, :discover, :american_express,
4
+ :diners_club, :jcb, :switch, :solo, :dankort,
5
+ :maestro, :forbrugsforeningen, :laser].freeze
4
6
 
5
7
  class << self
6
8
  def credit_card(*types)
@@ -0,0 +1,17 @@
1
+ module Faker
2
+ class Food < Base
3
+ class << self
4
+ def ingredient
5
+ fetch('food.ingredients')
6
+ end
7
+
8
+ def spice
9
+ fetch('food.spices')
10
+ end
11
+
12
+ def measurement
13
+ fetch('food.measurement_sizes') + ' ' + fetch('food.measurements')
14
+ end
15
+ end
16
+ end
17
+ end
@@ -12,6 +12,14 @@ module Faker
12
12
  def city
13
13
  fetch('game_of_thrones.cities')
14
14
  end
15
+
16
+ def quote
17
+ fetch('game_of_thrones.quotes')
18
+ end
19
+
20
+ def dragon
21
+ fetch('game_of_thrones.dragons')
22
+ end
15
23
  end
16
24
  end
17
25
  end
@@ -50,7 +50,7 @@ module Faker
50
50
  def resolve(value)
51
51
  case value
52
52
  when Array then value[rand(value.size)]
53
- when Range then rand((value.last+1) - value.first) + value.first
53
+ when Range then value.to_a[rand(value.size)]
54
54
  else value
55
55
  end
56
56
  end
@@ -19,14 +19,14 @@ module Faker
19
19
  if specifier.kind_of? String
20
20
  return specifier.scan(/\w+/).shuffle.join(separators.sample).downcase
21
21
  elsif specifier.kind_of? Integer
22
+ # If specifier is Integer and has large value, Argument error exception is raised to overcome memory full error
23
+ raise ArgumentError, "Given argument is too large" if specifier > 10**6
22
24
  tries = 0 # Don't try forever in case we get something like 1_000_000.
23
25
  begin
24
26
  result = user_name nil, separators
25
27
  tries += 1
26
28
  end while result.length < specifier and tries < 7
27
- until result.length >= specifier
28
- result = result * 2
29
- end
29
+ result = result * (specifier/result.length + 1) if specifier > 0
30
30
  return result
31
31
  elsif specifier.kind_of? Range
32
32
  tries = 0
@@ -53,7 +53,6 @@ module Faker
53
53
  diff_rand = rand(diff_length + 1)
54
54
  temp += Lorem.characters(diff_rand)
55
55
  end
56
- temp = temp[0..min_length] if min_length > 0
57
56
 
58
57
  if mix_case
59
58
  temp.chars.each_with_index do |char, index|
@@ -144,8 +143,8 @@ module Faker
144
143
  "#{ip_v6_address}/#{1 + rand(127)}"
145
144
  end
146
145
 
147
- def url(host = domain_name, path = "/#{user_name}")
148
- "http://#{host}#{path}"
146
+ def url(host = domain_name, path = "/#{user_name}", scheme = 'http')
147
+ "#{scheme}://#{host}#{path}"
149
148
  end
150
149
 
151
150
  def slug(words = nil, glue = nil)