faker 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/History.txt +3 -0
  3. data/README.md +42 -6
  4. data/lib/faker.rb +11 -2
  5. data/lib/faker/address.rb +1 -0
  6. data/lib/faker/avatar.rb +2 -2
  7. data/lib/faker/business.rb +4 -4
  8. data/lib/faker/code.rb +33 -1
  9. data/lib/faker/company.rb +6 -0
  10. data/lib/faker/date.rb +34 -0
  11. data/lib/faker/hacker.rb +31 -0
  12. data/lib/faker/internet.rb +7 -4
  13. data/lib/faker/number.rb +41 -3
  14. data/lib/faker/phone_number.rb +3 -3
  15. data/lib/faker/time.rb +48 -0
  16. data/lib/faker/version.rb +1 -1
  17. data/lib/locales/{de-at.yml → de-AT.yml} +1 -1
  18. data/lib/locales/{de-ch.yml → de-CH.yml} +1 -1
  19. data/lib/locales/{en-au.yml → en-AU.yml} +1 -1
  20. data/lib/locales/{en-bork.yml → en-BORK.yml} +1 -1
  21. data/lib/locales/{en-ca.yml → en-CA.yml} +1 -1
  22. data/lib/locales/{en-gb.yml → en-GB.yml} +1 -1
  23. data/lib/locales/{en-ind.yml → en-IND.yml} +1 -1
  24. data/lib/locales/{en-nep.yml → en-NEP.yml} +0 -0
  25. data/lib/locales/{en-us.yml → en-US.yml} +1 -1
  26. data/lib/locales/en.yml +10 -2
  27. data/lib/locales/fr.yml +1 -1
  28. data/lib/locales/ja.yml +2 -0
  29. data/lib/locales/{nb-no.yml → nb-NO.yml} +1 -1
  30. data/lib/locales/pl.yml +1 -1
  31. data/lib/locales/{pt-br.yml → pt-BR.yml} +2 -2
  32. data/lib/locales/ru.yml +9 -0
  33. data/lib/locales/sk.yml +1 -1
  34. data/lib/locales/zh-CN.yml +27 -0
  35. data/test/test_en_us_locale.rb +2 -2
  36. data/test/test_faker_code.rb +12 -0
  37. data/test/test_faker_company.rb +7 -0
  38. data/test/test_faker_date.rb +52 -0
  39. data/test/test_faker_hacker_talk.rb +37 -0
  40. data/test/test_faker_internet.rb +7 -0
  41. data/test/test_faker_number.rb +52 -1
  42. data/test/test_faker_street.rb +1 -1
  43. data/test/test_faker_time.rb +74 -0
  44. data/test/test_helper.rb +7 -0
  45. data/test/test_locale.rb +11 -7
  46. metadata +25 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3d19e0ae93b8e3a78433a69f98761d341068c3b4
4
- data.tar.gz: 36db9a564fafae8bc5a93470dbe2204f5fdc63ea
3
+ metadata.gz: ced120390f0205e9fa10ec66d97c988b01a0ab19
4
+ data.tar.gz: fc059c6b0a2986f815b9420ff3cdc3c01c6954cf
5
5
  SHA512:
6
- metadata.gz: a3e8c0b6aaed5907213c3632fecc7c34d72fb4c38c4c28d67e11c1f2af5c9d205ffd91460ee03b7d8b17b55946394ba4a83b6a219ebdf49251c84608879abcaf
7
- data.tar.gz: b27e7a2d35c183f968a313de2667850e75aff6f849f6846d48f892767b800a7dc788beceb2e462b3295dcccecf52d400342f75bcadb70429a49b46330ded0a1b
6
+ metadata.gz: bed9f65081c40bbf04890929acb89ba1cba7e0fa6ebd3936778e70bb67ca94f32d1ed653462e5161795aff15ea1f3a56f681731a420ae4692443e46d5aa9de35
7
+ data.tar.gz: e22e1878b5b92680fd6dd19803fdf191f32c133a2e32595bbf670edf7b837aded3c5b6522469b7547a12ae8adf67f1dad1700400590b875c1acb433b4f1e23a2
@@ -1,3 +1,6 @@
1
+ == 1.4.0 2014-07-03
2
+ * Many enhancements and bugfixes
3
+
1
4
  == 1.3.0 2014-03-08
2
5
  * Many enhancements and few bugfixes
3
6
 
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  Faker
2
2
  =====
3
- This gem is a port of Perl's Data::Faker library that generates fake data.
3
+ This gem is a port of Perl's Data::Faker library that generates fake data.
4
4
 
5
5
  It comes in very handy for taking screenshots (taking screenshots for my
6
6
  project, [Catch the Best](http://catchthebest.com/) was the original impetus
@@ -92,6 +92,8 @@ Faker::Business.credit_card_type #=> "visa"
92
92
 
93
93
  Faker::Code.isbn #=> "759021701-8"
94
94
 
95
+ Faker::Code.ean #=> "4600051000057"
96
+
95
97
  ```
96
98
 
97
99
  ###Faker::Commerce
@@ -124,6 +126,9 @@ Faker::Company.bs #=> "empower one-to-one web-readiness"
124
126
 
125
127
  Faker::Company.duns_number #=> "08-341-3736"
126
128
 
129
+ # Get a random company logo url in GIF format.
130
+ Faker::Company.logo #=> "http://www.biz-logo.com/examples/007.gif"
131
+
127
132
  ```
128
133
 
129
134
 
@@ -153,10 +158,12 @@ Faker::Internet.user_name('Nancy') #=> "nancy"
153
158
 
154
159
  Faker::Internet.user_name('Nancy Johnson', %w(. _ -)) #=> "johnson-nancy"
155
160
 
156
- # Optional argument min_length=0
157
- Faker::Internet.password #=> "doloremquealiquidrerum"
161
+ # Optional arguments: min_length=8, max_length=16
162
+ Faker::Internet.password #=> "vg5msvy1uerg7"
163
+
164
+ Faker::Internet.password(8) #=> "yfgjik0hgzdqs0"
158
165
 
159
- Faker::Internet.password(8) #=> "consecteturadasperiores"
166
+ Faker::Internet.password(10, 20) #=> "eoc9shwd1hwq4vbgfw"
160
167
 
161
168
  Faker::Internet.domain_name #=> "effertz.info"
162
169
 
@@ -170,6 +177,10 @@ Faker::Internet.ip_v4_address #=> "24.29.18.175"
170
177
 
171
178
  Faker::Internet.ip_v6_address #=> "ac5f:d696:3807:1d72:2eb5:4e81:7d2b:e1df"
172
179
 
180
+ # Optional argument prefix=''
181
+ Faker::Internet.mac_address #=> "e6:0d:00:11:ed:4f"
182
+ Faker::Internet.mac_address('55:44:33') #=> "55:44:33:02:1d:9b"
183
+
173
184
  # Optional arguments: host=domain_name, path="/#{user_name}"
174
185
  Faker::Internet.url #=> "http://thiel.com/chauncey_simonis"
175
186
  Faker::Internet.url('example.com') #=> "http://example.com/clotilde.swift"
@@ -298,6 +309,33 @@ Faker::PhoneNumber.extension #=> "3764"
298
309
 
299
310
  ```
300
311
 
312
+ ###Faker::Hacker
313
+ ---------------------
314
+ Are you having trouble writing tech-savvy dialogue for your latest screenplay?
315
+ Worry not! Hollywood-grade technical talk is ready to fill out any form where you need to look smart.
316
+
317
+ ```ruby
318
+ # Full Phrase
319
+ Faker::Hacker.say_something_smart #=> "Try to compress the SQL interface, maybe it will program the back-end hard drive!"
320
+
321
+ # Short technical abbreviations
322
+ Faker::Hacker.abbreviation #=> "RAM"
323
+
324
+ # Hacker centric adjectives
325
+ Faker::Hacker.adjective #=> "open-source"
326
+
327
+ # Only the best hacker related nouns
328
+ Faker::Hacker.noun #=> "bandwidth"
329
+
330
+ # Actions that hackers take
331
+ Faker::Hacker.verb #=> "bypass"
332
+
333
+ # Verbs that end in -ing
334
+ Faker::Hacker.ingverb #=> "synthesizing"
335
+ ```
336
+
337
+
338
+
301
339
 
302
340
  Customization
303
341
  ------------
@@ -325,5 +363,3 @@ Comments and feedback are welcome. Send an email to Benjamin Curtis via the [goo
325
363
  License
326
364
  -------
327
365
  This code is free to use under the terms of the MIT license.
328
-
329
-
@@ -7,8 +7,8 @@ rescue LoadError
7
7
  end
8
8
 
9
9
  require 'i18n'
10
+ I18n.enforce_available_locales = true
10
11
  I18n.load_path += Dir[File.join(mydir, 'locales', '*.yml')]
11
- I18n.reload!
12
12
 
13
13
 
14
14
  module Faker
@@ -18,7 +18,7 @@ module Faker
18
18
  class << self
19
19
  attr_writer :locale
20
20
  def locale
21
- @locale || I18n.locale.downcase
21
+ @locale || I18n.locale
22
22
  end
23
23
  end
24
24
  end
@@ -144,6 +144,11 @@ module Faker
144
144
  end
145
145
  end
146
146
 
147
+ # Generates a random value between the interval
148
+ def rand_in_range(from, to)
149
+ from, to = to, from if to < from
150
+ Random.new.rand(from..to)
151
+ end
147
152
  end
148
153
  end
149
154
  end
@@ -163,6 +168,10 @@ require 'faker/version'
163
168
  require 'faker/number'
164
169
  require 'faker/bitcoin'
165
170
  require 'faker/avatar'
171
+ require 'faker/date'
172
+ require 'faker/time'
173
+ require 'faker/number'
174
+ require 'faker/hacker'
166
175
 
167
176
  require 'extensions/array'
168
177
  require 'extensions/symbol'
@@ -40,6 +40,7 @@ module Faker
40
40
  def state_abbr; fetch('address.state_abbr'); end
41
41
  def state; fetch('address.state'); end
42
42
  def country; fetch('address.country'); end
43
+ def country_code; fetch('address.country_code'); end
43
44
 
44
45
  def latitude
45
46
  ((rand * 180) - 90).to_s
@@ -2,9 +2,9 @@ module Faker
2
2
  class Avatar < Base
3
3
  class << self
4
4
  def image(slug=nil)
5
- slug ||= Faker::Lorem.words
5
+ slug ||= Faker::Lorem.words.join
6
6
  "http://robohash.org/#{slug}"
7
7
  end
8
8
  end
9
9
  end
10
- end
10
+ end
@@ -8,15 +8,15 @@ module Faker
8
8
  def credit_card_number
9
9
  fetch('business.credit_card_numbers')
10
10
  end
11
-
11
+
12
12
  def credit_card_expiry_date
13
- Date.parse(fetch('business.credit_card_expiry_dates'))
13
+ ::Date.parse(fetch('business.credit_card_expiry_dates'))
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
-
20
+
21
21
  end
22
22
  end
@@ -7,6 +7,18 @@ module Faker
7
7
  base == 13 ? generate_base13_isbn : generate_base10_isbn
8
8
  end
9
9
 
10
+ # By default generates 13 sign ean code in format 1234567890123
11
+ # You can pass 8 to generate ean8 code
12
+ def ean(base = 13)
13
+ base == 8 ? generate_base8_ean : generate_base13_ean
14
+ end
15
+
16
+ def rut
17
+ value = Number.number(8)
18
+ vd = rut_verificator_digit(value)
19
+ value << "-#{vd}"
20
+ end
21
+
10
22
  private
11
23
 
12
24
  def generate_base10_isbn
@@ -26,6 +38,26 @@ module Faker
26
38
  sum + block.call(value, index)
27
39
  end
28
40
  end
41
+
42
+ def generate_base8_ean
43
+ values = regexify(/\d{7}/)
44
+ check_digit = values.split(//).each_with_index.inject(0){ |s, (v, i)| s + v.to_i * EAN_CHECK_DIGIT8[i] } % 10
45
+ values << (10 - check_digit).to_s
46
+ end
47
+
48
+ def generate_base13_ean
49
+ values = regexify(/\d{12}/)
50
+ check_digit = values.split(//).each_with_index.inject(0){ |s, (v, i)| s + v.to_i * EAN_CHECK_DIGIT13[i] } % 10
51
+ values << (10 - check_digit).to_s
52
+ end
53
+
54
+ EAN_CHECK_DIGIT8 = [3, 1, 3, 1, 3, 1, 3, 1]
55
+ EAN_CHECK_DIGIT13 = [1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1]
56
+
57
+ def rut_verificator_digit(rut)
58
+ 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(:+)
59
+ (11 - total % 11).to_s.gsub(/10/, 'k').gsub(/11/, '0')
60
+ end
29
61
  end
30
62
  end
31
- end
63
+ end
@@ -24,6 +24,12 @@ module Faker
24
24
  def duns_number
25
25
  ('%09d' % rand(10 ** 9)).gsub(/(\d\d)(\d\d\d)(\d\d\d\d)/, '\\1-\\2-\\3')
26
26
  end
27
+
28
+ # Get a random company logo url in GIF format.
29
+ def logo
30
+ rand_num = Random.rand(76) + 1
31
+ "http://www.biz-logo.com/examples/#{ rand_num < 10 ? "00" : "0" }#{rand_num}.gif"
32
+ end
27
33
  end
28
34
 
29
35
  end
@@ -0,0 +1,34 @@
1
+ module Faker
2
+ class Date < Base
3
+ class << self
4
+ def between(from, to)
5
+ from = get_date_object(from)
6
+ to = get_date_object(to)
7
+
8
+ Faker::Base::rand_in_range(from, to)
9
+ end
10
+
11
+ def forward(days = 365)
12
+ from = ::Date.today + 1
13
+ to = ::Date.today + days
14
+
15
+ between(from, to).to_date
16
+ end
17
+
18
+ def backward(days = 365)
19
+ from = ::Date.today - days
20
+ to = ::Date.today - 1
21
+
22
+ between(from, to).to_date
23
+ end
24
+
25
+ private
26
+
27
+ def get_date_object(date)
28
+ date = ::Date.parse(date) if date.is_a?(String)
29
+ date = date.to_date if date.respond_to?(:to_date)
30
+ date
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,31 @@
1
+ #Port of http://shinytoylabs.com/jargon/
2
+ module Faker
3
+ class Hacker < Base
4
+ flexible :hacker
5
+
6
+ class << self
7
+ def say_something_smart
8
+ phrases.sample
9
+ end
10
+
11
+ def abbreviation; fetch('hacker.abbreviation'); end
12
+ def adjective; fetch('hacker.adjective'); end
13
+ def noun; fetch('hacker.noun'); end
14
+ def verb; fetch('hacker.verb'); end
15
+ def ingverb; fetch('hacker.ingverb'); end
16
+
17
+ def phrases
18
+ [ "If we #{verb} the #{noun}, we can get to the #{abbreviation} #{noun} through the #{adjective} #{abbreviation} #{noun}!",
19
+ "We need to #{verb} the #{adjective} #{abbreviation} #{noun}!",
20
+ "Try to #{verb} the #{abbreviation} #{noun}, maybe it will #{verb} the #{adjective} #{noun}!",
21
+ "You can't #{verb} the #{noun} without #{ingverb} the #{adjective} #{abbreviation} #{noun}!",
22
+ "Use the #{adjective} #{abbreviation} #{noun}, then you can #{verb} the #{adjective} #{noun}!",
23
+ "The #{abbreviation} #{noun} is down, #{verb} the #{adjective} #{noun} so we can #{verb} the #{abbreviation} #{noun}!",
24
+ "#{ingverb} the #{noun} won't do anything, we need to #{verb} the #{adjective} #{abbreviation} #{noun}!",
25
+ "I'll #{verb} the #{adjective} #{abbreviation} #{noun}, that should #{noun} the #{abbreviation} #{noun}!"
26
+ ]
27
+ end
28
+ end
29
+
30
+ end
31
+ end
@@ -45,11 +45,14 @@ module Faker
45
45
  ].sample.call)
46
46
  end
47
47
 
48
- def password(min_length=0)
49
- temp = Lorem.words.join
50
- while temp.length < min_length
51
- temp += Lorem.word
48
+ def password(min_length = 8, max_length = 16)
49
+ temp = Lorem.characters(min_length)
50
+ diff_length = max_length - min_length
51
+ if diff_length > 0
52
+ diff_rand = rand(diff_length + 1)
53
+ temp += Lorem.characters(diff_rand)
52
54
  end
55
+ temp = temp[0..min_length] if min_length > 0
53
56
  return temp
54
57
  end
55
58
 
@@ -2,17 +2,55 @@ module Faker
2
2
  class Number < Base
3
3
  class << self
4
4
  def number(digits)
5
- rand(10 ** digits).to_s.rjust(digits, '0')
5
+ (1..digits).collect {digit}.join
6
6
  end
7
7
 
8
8
  def decimal(l_digits, r_digits = 2)
9
9
  l_d = self.number(l_digits)
10
- r_d = self.number(r_digits)
10
+ r_d = self.number(r_digits)
11
11
  "#{l_d}.#{r_d}"
12
12
  end
13
13
 
14
14
  def digit
15
- rand(9).to_s
15
+ (rand() * 9).round.to_s
16
+ end
17
+
18
+ def hexadecimal(digits)
19
+ hex = ""
20
+ digits.times { hex += rand(15).to_s(16) }
21
+ hex
22
+ end
23
+
24
+ def between(from = 1.00, to = 5000.00)
25
+ Faker::Base::rand_in_range(from, to)
26
+ end
27
+
28
+ def positive(from = 1.00, to = 5000.00)
29
+ random_number = between(from, to)
30
+ greater_than_zero(random_number)
31
+ end
32
+
33
+ def negative(from = -5000.00, to = -1.00)
34
+ random_number = between(from, to)
35
+ less_than_zero(random_number)
36
+ end
37
+
38
+ private
39
+
40
+ def greater_than_zero(number)
41
+ should_be(number, :>)
42
+ end
43
+
44
+ def less_than_zero(number)
45
+ should_be(number, :<)
46
+ end
47
+
48
+ def should_be(number, method_to_compare)
49
+ if number.send(method_to_compare, 0)
50
+ number
51
+ else
52
+ number * -1
53
+ end
16
54
  end
17
55
  end
18
56
  end
@@ -10,10 +10,10 @@ module Faker
10
10
  end
11
11
 
12
12
  def cell_phone
13
- if (translation = translate(:faker)[:cell_phone]).is_a? Hash
14
- numerify(translation[:formats].sample)
13
+ if parse('cell_phone.formats') == ""
14
+ numerify(fetch('cell_phone.formats'))
15
15
  else
16
- numerify(fetch('phone_number.formats'))
16
+ parse('cell_phone.formats')
17
17
  end
18
18
  end
19
19
 
@@ -0,0 +1,48 @@
1
+ module Faker
2
+ class Time < Date
3
+ TIME_RANGES = {
4
+ :all => (0..23),
5
+ :day => (9..17),
6
+ :night => (18..23),
7
+ :morning => (6..11),
8
+ :afternoon => (12..17),
9
+ :evening => (17..21),
10
+ :midnight => (0..4)
11
+ }
12
+
13
+ class << self
14
+ def between(from, to, period = :all)
15
+ super(from, to).to_time + random_time(period)
16
+ end
17
+
18
+ def forward(days = 365, period = :all)
19
+ super(days).to_time + random_time(period)
20
+ end
21
+
22
+ def backward(days = 365, period = :all)
23
+ super(days).to_time + random_time(period)
24
+ end
25
+
26
+ private
27
+
28
+ def random_time(period)
29
+ hours(period) + minutes + seconds
30
+ end
31
+
32
+ def hours(period)
33
+ raise ArgumentError, 'invalid period' unless TIME_RANGES.has_key? period
34
+ hour_at_period = TIME_RANGES[period].to_a.sample
35
+
36
+ (60 * 60 * hour_at_period)
37
+ end
38
+
39
+ def minutes
40
+ 60 * seconds
41
+ end
42
+
43
+ def seconds
44
+ (0..59).to_a.sample
45
+ end
46
+ end
47
+ end
48
+ end